Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put external files for testthat tests

Suppose I have test like this:

require(testthat)
context("toy test")
test_that("toy", {
            df = my.read.file("test.txt", header=TRUE)
            expect_true(myfunc(df) == 3.14)
})

and this test relies on a external file test.txt, where should I put this file then?

like image 200
qed Avatar asked May 26 '15 20:05

qed


1 Answers

You put these in the testthat folder (inside tests). There, you include any "external" file that you might use for your tests (or that provides some additional explanation that the user might find informative, such as in a ".txt") file. You also have your .r testfiles here.

Alternatively (or, in addition): you can also load your file from another location, by including the path to the file (e.g., to your data folder--use a relative path). However, this can result in a fragile infrastructure, since you might not be able to rely on that external location to be available at all times, in which case testthat will raise an error when it can not find the file.

An example of linking to a file outside of tests, see here. Beware when you do this, though.

like image 141
Peter Verbeet Avatar answered Oct 17 '22 10:10

Peter Verbeet