Difference between devtools::check() and devtools::test() regarding usage of test data

Hello,

in my currently developed package I want to use data for unit testing with testthat.
I read in R Packages that the recommended solution is to put it into the tests directory.
Additionally, I read this thread on Stackoverflow.
I put it in

  • tests/testdata. Later I also tried it in
  • inst/testdata and
  • inst/extdata.

In one of my testfiles I test a function that gets the directory where the test data lives as argument. I provided this directory using

  • "tests/testdata":
test_that("Length of value", {
  expect_length(
    eumohp_clip(
      directory_input = "tests/testdata",
      region_name_spatcov = "france",
      eumohp_version = "v013.1.1"
    ),
    18
  )
})
  • system.file("tests/testdata", package = "my-package")
test_that("Length of value", {
  expect_length(
    eumohp_clip(
      directory_input = system.file("tests/fixtures", package = "eumohpclipr"),
      region_name_spatcov = "france",
      eumohp_version = "v013.1.1"
    ),
    18
  )
})
  • here::here("tests/testdata")
test_that("Length of value", {
  expect_length(
    eumohp_clip(
      directory_input = here::here("tests/testdata"),
      region_name_spatcov = "france",
      eumohp_version = "v013.1.1"
    ),
    18
  )
})

When running the test using devtools::test() everything is fine but when running devtools::check() or using control + shift + E this directory is not found. After printing the root directory during testing it seems that the directory "test" doesn't exist, at least at the time R-CMD-CHECK runs the tests. I know too little about R-CMD-CHECK, but this seems like a little bit too magical :-).
I didn't expect this to be an edge case, but from the usual web research it seems like it.

I would really appreciate any hint. Thanks a lot!

:wave: @MxNl , sorry for the late answer.

I think you were looking for testthat::test_path().

  • put the data in tests/testthat/testdata

  • use

test_that("Length of value", {
  expect_length(
    eumohp_clip(
      directory_input = test_path("testdata"),
      region_name_spatcov = "france",
      eumohp_version = "v013.1.1"
    ),
    18
  )
})

This topic was automatically closed after 45 days. 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.