R test-单元测试数据和辅助函数约定 [英] R testthat unit test data and helper function conventions

查看:70
本文介绍了R test-单元测试数据和辅助函数约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个R包,并且正在使用testthat进行单元测试.我的许多单元测试都是针对在特定于我的软件包的特定对象上工作的功能进行测试的.对于这些测试,我制作了一个辅助函数来设置模拟对象.我还有其他一些辅助函数,可以减少单元测试中的代码量.

当前,这些帮助器功能位于我的R/文件夹中,因为它们随后可用于我的单元测试文件(位于tests/testthat/中).我发现将仅用于单元测试的函数放在R/文件夹中有点奇怪.如果可以将它们放在tests/文件夹中的某个位置,那将是很好的.但是似乎这样做会使它们在单元测试期间不可用.请注意,这些帮助程序功能用于多个不同的测试文件,因此仅将帮助程序功能放在包含单元测试的一个文件的顶部并不是解决方案.

另一个相关的问题是在哪里放置用于单元测试的数据文件.例如,我的某些功能可用于limma包中的ExpressionSet对象(可在 Bioconductor 上找到),我在使用load()函数进行单元测试期间将其加载到R中.目前,我将它们放在inst/extdata文件夹中,因为这样我可以在单元测试期间使用system.file()对其进行访问.此文件夹还包含其他数据文件,这些数据文件供包的用户使用.我发现将我的测试数据(不供最终用户使用)和软件包数据(供最终用户使用)放在同一位置有点奇怪.

所以我的问题是,是否可以将单元测试助手功能和测试数据全部放置在tests/目录中.如果是这样,我如何从单元测试中访问这些文件?单元测试助手和单元测试数据的最佳做法/惯例是什么?

解决方案

我了解tests/testthat/中以helper开头的文件是在通过testthat运行任何测试之前获取的.因此,您可以在tests/testthat/中的适当命名为helper-*.R的内部放置测试的辅助函数.

testthat(?testthat::source_file)到source_file的R帮助中

 The expectation is that the files can be sourced in alphabetical
 order. Helper scripts are R scripts accompanying test scripts but
 prefixed by ‘helper’. These scripts are once before the tests are
 run.

可以在githubdplyr源代码中看到一个示例./p>

关于测试数据.我从以下问题的一条评论中得到了建议:在哪里将数据用于testthat进行自动测试?,然后使用inst/testdata使用system.file("testdata",...,package="my_package")

访问文件

I am writing a R package, and I'm using testthat for unit testing. Many of my unit tests are for testing functions that work on a certain object specific to my package. For these tests I have made a helper function to set up a mock object. I have a few other helper functions as well to cut down the amount of code in my unit tests.

Currently these helper functions are in my R/ folder, because then they become available to my unit test files (which are in tests/testthat/). I find it a bit weird to place functions that are only used for the unit tests in the R/ folder. It would be nice if could place them somewhere in the tests/ folder. But it seems that doing so makes them unavailable during unit tests. Note that these helper functions are used for several different test files, so just placing the helper functions at the top of one file containing unit tests is not a solution.

Another related question is where to place data files that are used for unit tests. For example some of my functions work on ExpressionSet objects from the limma package (available on Bioconductor), which I load into R during the unit tests with the load() function. Currently I put them in the inst/extdata folder, because then I can acces them with system.file() during my unit tests. This folder also contains other data files which are meant for the users of the package. I find it a bit strange to place my test data, which is not meant for end users, in the same place as the package data, which is meant for the end users.

So my question is, is it possible to place the unit test helper functions and test data all in the tests/ directory. And if so, how do I acces these files from within the unit tests? What are the best practices/conventions for unit test helpers and unit test data?

解决方案

I understand that files in tests/testthat/ that begin with helper are sourced before running any tests by testthat. So you can place helper functions for tests inside suitably named helper-*.R within tests/testthat/.

From R help for source_file from testthat (?testthat::source_file)

 The expectation is that the files can be sourced in alphabetical
 order. Helper scripts are R scripts accompanying test scripts but
 prefixed by ‘helper’. These scripts are once before the tests are
 run.

An example can be seen in the source code for dplyr on github.

As for testdata. I follow the advice from one comment from this question: Where to put data for automated tests with testthat? and use inst/testdata, then access the files with system.file("testdata",...,package="my_package")

这篇关于R test-单元测试数据和辅助函数约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆