R testthat unit test data and helper function conventions

后端 未结 2 1222
孤街浪徒
孤街浪徒 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:28

    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.

    0 讨论(0)
  • 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")

    0 讨论(0)
提交回复
热议问题