1

This question is a near duplicate of Q32328802 - testdata for testthat. The difference is that I'm looking for a more specific answer as it relates to devtools::check. Specifically, how do I get devtools::check() to recognize and load my test data?

My package structure for tests/testthat.R and tests/testthat/setup-testdata.R is described below (and github is linked at the bottom of the post). I have tried the following:

  • Following the above linked question, I have placed the test data in inst/testdata. As this is not working, I have also tried other locations (eg. tests/, tests/testthat). None of these locations work.
  • I have also tried renaming helper-testdata.R as setup-testdata.R and get the same failing results.
  • devtools::check() does not appear to accurately recognize the load(system.file(...)) command. It similarly does not recognize if I spell the file name out (ie. do not use system.file()). Omitting the load command also doesn't work.

tests/:

testthat.R

library(synthACS)
library(testthat)

test_check("synthACS")

testthat/setup-testdata.R has a single line in it:

load(system.file("testdata", 'dat-acsdata.Rda', package= "synthACS"))
# run interactively, this line of code loads the data accurately.
# within devtools::check() it appears to return an empty string ("") for
# file location

Checking my package

R> devtools::test(synthACS)
══ Results ═════════════════════════

OK:       388
Failed:   0
Warnings: 0
Skipped:  0

R> devtools::check(synthACS)
...
─  checking tests ...
E  Running ‘testthat.R’ (1.5s)

── Test failures ─────────────────────

> library(testthat)
> 
> load(system.file("testdata", 'acsdata.rda', package= "synthACS"))
> test_check("synthACS")
 ----------- FAILURE REPORT -------------- 
... indicates that the data is not loaded ...

Any help appreciated!

Edit links to full code:

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59

2 Answers2

0

R CMD check doesn't care what is in your /tests directory, as long as there's a file it can run to execute your tests (and they pass). Similarly, testthat doesn't care about other directories under /tests. So you can put your file in a directory there, say in /tests/resources.

Example here: https://github.com/jeroen/openssl/tree/master/tests

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • Thanks for the response. I have tried moving the file `sysdata.rda` to multiple different directories. This does not resolve how to `load()` the file as part of test_check() – alexwhitworth May 28 '20 at 03:56
  • Create a `setup.R` file under tests/testthat to do any pre-testing setup, see the help for testthat – Hong Ooi May 28 '20 at 03:59
  • While I appreciate the help, this does not resolve for me. Per the testthat documentation i have tried both `helper-` and `setup-` files (see my OP). The file is not working properly – alexwhitworth May 28 '20 at 04:05
  • Put the file in tests/resources/whatver.rda, and have a tests/testthat/setup.R file containing the line `load("../resources/whatever.rda")` – Hong Ooi May 28 '20 at 04:21
  • This is essentially what I'm already doing, except using `load("../resources/whatever.rda")` instead of `load(system.file(...))`. Regardless of approach, it does not work. I get the same error with your suggested approach – alexwhitworth May 28 '20 at 04:44
  • DON'T USE `system.file`. Use the relative path I gave there. – Hong Ooi May 28 '20 at 04:55
  • Try it again 123 – Hong Ooi May 28 '20 at 05:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214842/discussion-between-alex-w-and-hong-ooi). – alexwhitworth May 28 '20 at 16:51
0

The correct answer is to use readRDS, which means saving each object separately via saveRDS

eg.

my_data <- readRDS(file= system.file("testdata", 'dat-mydata.rds', package= "synthACS"))

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59
  • so, just to follow up... the actual location of the files wasn't the issue with CHECK, it was something about using RDA vs RDS? Any clarity on what the problem was? – seth127 Nov 23 '20 at 19:04
  • For me, this solution is not working. I have the following `my_data <- readRDS(file= system.file("tests", "testthat", "testdata", "my_data.rds", package = "my_package"))`. The error message: `Error: cannot open the connection Backtrace: x 1. \-base::readRDS(...) test_user_input.R:3:2 2. \-base::gzfile(file, "rb")`. What is wrong here? – Faustin Gashakamba Oct 25 '21 at 18:02