r-lib / devtools

Tools to make an R developer's life easier
https://devtools.r-lib.org
Other
2.39k stars 760 forks source link

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

Closed MxNl closed 2 years ago

MxNl commented 2 years ago

Hello,

in my currently developed package I want to use data (several .tif files) 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

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

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. 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!

MxNl commented 2 years ago

I have now posted it also here: https://community.rstudio.com/t/difference-between-devtools-check-and-devtools-test-regarding-usage-of-test-data/130561 I will close this issue, if it gets answered in RStudio community.

jennybc commented 2 years ago

I believe testthat::test_path() is the solution you seek.

jennybc commented 2 years ago

This GitHub search is rich in examples of how people use it:

https://github.com/search?q=test_path+path%3Atests+org%3Acran

MxNl commented 2 years ago

I believe testthat::test_path() is the solution you seek.

Thanks a lot!!! I'll try it out. It would be great to have an example of that in the R Packages book. After an average web search I didn't come across that function. According to the lack of solutions in relevant questions on SO, others have difficulties to find it, too. But thanks a lot so far! Greetings from Berlin!

jennybc commented 2 years ago

The testing chapter has not been revised yet for the 2nd edition but will be.

KrishyV commented 2 years ago

In my case, the contents of inst/testdata was moved to a folder Package.Rcheck/Package/test-data/ when running devtools::check(). This difference in working directory broke the absolute path to the test data. I did a pattern match with grepl to handle this case. Not elegant but it worked.

MxNl commented 2 years ago

In my case, the contents of inst/testdata was moved to a folder Package.Rcheck/Package/test-data/ when running devtools::check(). This difference in working directory broke the absolute path to the test data. I did a pattern match with grepl to handle this case. Not elegant but it worked.

Ok, but why not using jennybc's solution. It's easy and works. Data in subfolder of tests/testthat and then refer to it with testthat::test_path().