Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put data for automated tests with testthat?

I am using Hadley's testthat-based approach for automated testing of my package.

With this approach, what is the most suitable place to put test data files that is files only used by the test scripts in tests/testthat), but not by any other functions in R/?

My current approach is to put them in tests/testdata, and then read.table from there with a relative path rather than with system.file (in order to avoid the need to install the package to run tests).

Is there a standard way to do this?

like image 612
Aditya Avatar asked Sep 01 '15 10:09

Aditya


People also ask

How do you store test data?

If at all possible, store the values in plain text files. The reason for this is so that you can put the data into version control along with the tests. Often a CSV file will do nicely, and there are likely plenty of CSV-reader libraries for whatever language you're using to automate tests.


1 Answers

Lifting from Ben Bolker's comments:

I use inst/testdata and then system.file("testdata",...,package="my_package")

The advantage of this method:

  • You can keep your file structure neat, especially if you have many data files and/or tests.
  • The fact that files in inst are installed is long-standing canonical R practice; it seems safer that system.file("testdata", "some_file") will always work than that ../testdata/some_file will do. I've had bad experiences using relative file paths when doing R CMD check.
  • Unlike Sathish's answer, it doesn't depend on your data being "stored" as R code.
like image 149
user3603486 Avatar answered Oct 09 '22 03:10

user3603486