Ben Bolker
Ben Bolker

Reputation: 226801

confused by testthat and skip_on_cran()

tl;dr I want to run devtools::test() on a package and have it skip tests etc. as though it were running on CRAN, but I can't figure out how.

As I understand it, testthat::skip_on_cran() checks for the environment variable NOT_CRAN, which should be set to a value of "true" if the tests are not being run on CRAN (to back this up, the underlying test function testthat:::on_cran() is equal to

!identical(Sys.getenv("NOT_CRAN"), "true")

I am trying to use skip_on_cran() to skip some tests. I want to confirm that these tests will actually be skipped on CRAN. I have a line

cat("ON CRAN:", testthat:::on_cran(), "\n")

in my test file so that I can see what R/testthat thinks is going on.

The environment variables get set the way I want (i.e., the output includes ON CRAN: FALSE)/the tests get skipped properly) if I use

source([testfile], echo = TRUE)

(i.e., without doing anything special to set or unset the NOT_CRAN environment variable beforehand) or

withr::with_envvar(c(NOT_CRAN = "false"), 
    devtools::test_active_file("tests/testthat/test-bootMer.R"))

(if I run test_active_file() without wrapping it, I get ON CRAN: FALSE).

However, I don't see a way to run all the tests (via devtools::test()) in a similar way. In other words, I can't figure out how to run devtools::test() in "ON CRAN" mode. test() doesn't have an explicit argument for this (it has ... which is "additional arguments passed to wrapped functions", but I can't see anything relevant digging downward), and using withr::with_envvar() doesn't seem to help. devtools::check() does have an explicit env_vars argument, but I would like to be able to run the tests without going through the whole package check procedure ...

I'm sorry this isn't fully reproducible; if requested I can try to build a minimal package that will display the behaviour ...

Upvotes: 13

Views: 707

Answers (2)

Matthew Fidler
Matthew Fidler

Reputation: 666

For me, this worked:

  1. Install the package you want to test
  2. Run Sys.setenv("NOT_CRAN"="false");testthat::test_package("rxode2") where rxode2 is the package you are testing.

I know this is old, but others may wish to know too.

As a bonus, if you want to see the typical output with devtools::test() you can use:

Sys.setenv("NOT_CRAN"="false");testthat::test_package("rxode2", reporter = NULL)

Upvotes: 2

pdb
pdb

Reputation: 1677

Not sure if you still have this question but I put this line at the top of my first test file and it works in R 4.2.1.

Sys.setenv(NOT_CRAN='skip')

Upvotes: 1

Related Questions