R testthat unit test data and helper function conventions

风格不统一 提交于 2019-12-03 23:33:41
waferthin

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")

Maybe it will help you: https://github.com/gmum/gmum.r/tree/master/tests/testthat

There is helper function called combinations in the file combinations.R Then it is included in test_cec_centroid_assignment.R using source('combinations.R') I'm not sure it is a good convention but it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!