qed
qed

Reputation: 23114

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?

Upvotes: 23

Views: 2833

Answers (2)

Peter Verbeet
Peter Verbeet

Reputation: 1816

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.

Upvotes: 24

qed
qed

Reputation: 23114

The right place seems to be (somewhat intuitively) /path/to/project/tests/testthat.

Upvotes: -2

Related Questions