Search code examples
rtestthat

Is there a way to make testthat search recursively?


When my R package directory structure has R files directly in the tests folder

.
+--Projroot
+---- R
|     -- routine1.R
|     -- routine2.R
+---- tests
      -- test_routine1.R
      -- test_routine2.R

testthat catches the test_*.R files, but when the tests themselves depend on a multitude of files, it's cleaner to have test subdirectories

.
+--Projroot
+---- R
|     -- routine1.R
|     -- routine2.R
+---- tests
      |
      +---- test_routine1
      |     -- test_routine1.R
      |     -- help_file11
      |     -- help_file12
      +---- test_routine2
            -- test_routine2.R
            -- help_file21
            -- help_file22

Just running devtools::test() does not catch the test_*.R files in inner directories.

Is there a way to make testthat search recursively?


Solution

  • devtools::test() calls testthat::test_dir.

    testthat::test_dir finds which are the files to test with testthat:::find_test_scripts. And this function does not look recursively inside the folder, so natively no, testthat won't be able to test inside inner directories.

    You can still run your tests this way, though :

    my_test <- list.files(path = "tests", pattern = "test_|help_", recursive = TRUE, full.names = TRUE)
    lapply(my_test, test_file)