devtools::test() succeeds, while devtools::check() fails

> devtools::test()
Loading pcai.api
Testing pcai.api
✔ |  OK F W S | Context
✔ |   1       | test_model [0.4 s]

══ Results ═════════════════════════════════════════════════════════════════════════════════════
Duration: 0.4 s

OK:       1
Failed:   0
Warnings: 0
Skipped:  0

It succeeds, however when I run devtools::check():

── Test failures ───────────────────────────────────────────────────────────────── testthat ────

> Sys.setenv("R_TESTS" = "")
> library(testthat)
> library(pcai.api)
> test_check("pcai.api")
── 1. Error: Test if a model can be correctly loaded from AWS (@test_model.R#5)  ───────────────
cannot open the connection
1: source("plumber.R") at testthat/test_model.R:5
2: file(filename, "r", encoding = encoding)

══ testthat results  ═══════════════════════════════════════════════════════════════════════════
[ OK: 0 | SKIPPED: 0 | WARNINGS: 1 | FAILED: 1 ]
1. Error: Test if a model can be correctly loaded from AWS (@test_model.R#5) 

Error: testthat unit tests failed
Execution halted

1 error ✖ | 1 warning ✖ | 4 notes ✖

It fails because it cannot find the plumber.R file. But how am I supposed to access the functions of that file if this doesn't work? I am not too familiar with the R working directory.

I think the issue might be that R CMD CHECK cannot find "plumber.R". Take a look if rprojroot::find_package_root_file() might be able to help you, and remember that stuff from the /inst dir gets moved to the package root on installation (so mypackage/inst/plumber.r becomes mypackage/plumber.r).

I tried rprojroot::find_package_root_file(), and it points to the right dir, however when using R CMD CHECK, it moves the tests to a new location. What is the correct way to access the plumber.R file from the test? The file structure is as follows:

├── tests/
│   └── testthat
│       ├── test_model.R
├── R/
│   └── plumber.R

I don't understand why you're calling source('plumber.R') from your tests. The package should be loaded and the functions defined in plumber.R can be called directly without having to source them.

Wow. Thank you. I am not very familiar with R, so this is very good to know. It works now.

No worries. Most testing frameworks automagically do a significant chunk of work behind the scenes so you don't have to load all the stuff yourself. The easily found overview ( doesn't make it clear that your package is loaded before the tests are run.

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