Using paths relative to the root of a project in tests

I'm working on a research project structured as a package.

I've a script datasets.R that generates .parquet files.

├── scripts
│   └── datasets.R
├── data
│   ├── foo.parquet
│   └── bar.parquet

I'd like to test those files with testthat to ensure they have the right structure.

Currently, I can only read those files during the tests if I specify the full path ("~/path_to_project/project/data/foo.parquet"). I'm wondering if there's a way to use paths relative to the project instead (i.e. "data/foo.parquet"). I use devtools::test() to run the tests. I want to keep the parquet files outside of testthat/ directory.

If your tests are in tests/testthat, then testthat::test_path() should always refer to that directory, so if you use devtools::test() then you should be able to find your data files with



testthat is aimed at packages, so this might not work without adding a top level DESCRIPTION file, that has a Package and a Version entry.


Hi @arangaca ,

testthat sets the working directory to the path "~/path_to_project/project/test/tests/testthat/".

So i would propose to add a file named helper.R

parquetPath <- "~/path_to_project/project/data"

this will load the helper.R file before every test (Special files • testthat). So you would call your parquet files inside the tests as

file.path(parquetPath ,"foo.parquet")

or you can even add a helper function into helper.R

parquetPath <- "~/path_to_project/project/data"
makeParquetPath <- function(filePath){
file.path(parquetPath, filePath)

then you could just call


Thanks, that works perfectly.

I've just learned something new. I thought ../ could only be used at the beginning of the path to access the parent directory and that tests/testthat/../../data/foo.parquet would just look for folders called ...

I do have a DESCRIPTION file. The project is actually a package with additional (non-conventional) directories and files. I created a post about that a few days ago. I ended up experimenting.

The main problem with using absolute paths here is that the tests will break whenever I run them on a different machine or different file structures. I could probably get away with getwd() but I find the test_path() and relative paths solution more elegant.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.