Skip to content

Commit 42c7ed0

Browse files
committed
Automatically skip missing packages on CRAN
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 6099802 commit 42c7ed0

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()` will now automatically skip 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
* Parallel testthat now does not ignore test files with syntax errors (#1360).
45
* `expect_lt()`, `expect_gt()`, and friends have a refined display that is more likely to display the correct number of digits and shows you the actual values compared.
56
* `describe()`, `it()`, and `test_that()` now have a shared stack of descriptions so that if you nest any inside of each other, any resulting failures will show you the full path.

R/test-that.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
167167
}
168168
},
169169
expectation = handle_expectation,
170+
packageNotFoundError = function(e) {
171+
if (on_cran()) {
172+
skip(paste0(e$package, " is not installed."))
173+
}
174+
},
170175
skip = handle_skip,
171176
warning = handle_warning,
172177
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)