R testthat unit test data and helper function conventions

后端 未结 2 1221
孤街浪徒
孤街浪徒 2021-02-12 17:42

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 the

2条回答
  •  伪装坚强ぢ
    2021-02-12 18:36

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

提交回复
热议问题