Skip to content

Commit 86480e2

Browse files
authored
Automatically skip missing packages on CRAN (#2173)
Practically, this should mean that you never need to add `skip_if_not_installed()` for suggested packages. It's already the tidyverse style to not do this, but now this works everywhere. Fixes #1585
1 parent 022b2e1 commit 86480e2

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* On CRAN, `test_that()` now automatically skips if a package is not installed (#1585). Practically, this means that you no longer need to check that suggested packages are installed. (We don't do this in the tidyverse because we think it has limited payoff, but other styles advise differently.)
34
* `expect_snapshot()` no longer skips on CRAN, as that skips the rest of the test. Instead it just returns, neither succeeding nor failing (#1585).
45
* Interrupting a test now prints the test name. This makes it easier to tell where a very slow test might be hanging (#1464)
56
* Parallel testthat now does not ignore test files with syntax errors (#1360).

R/test-that.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
173173
}
174174
},
175175
expectation = handle_expectation,
176+
packageNotFoundError = function(e) {
177+
if (on_cran()) {
178+
skip(paste0("{", e$package, "} is not installed."))
179+
}
180+
},
176181
skip = handle_skip,
177182
warning = handle_warning,
178183
message = handle_message,

tests/testthat/test-test-that.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,20 @@ test_that("no braces required in testthat 2e", {
147147
NA
148148
)
149149
})
150+
151+
test_that("missing packages cause a skip on CRAN", {
152+
local_on_cran(TRUE)
153+
154+
expectations <- capture_expectations(test_that("", {
155+
library(notinstalled)
156+
}))
157+
expect_length(expectations, 1)
158+
expect_s3_class(expectations[[1]], "expectation_skip")
159+
160+
local_on_cran(FALSE)
161+
expectations <- capture_expectations(test_that("", {
162+
library(notinstalled)
163+
}))
164+
expect_length(expectations, 1)
165+
expect_s3_class(expectations[[1]], "expectation_error")
166+
})

0 commit comments

Comments
 (0)