Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# testthat (development version)

* 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.)
* `expect_snapshot()` no longer skips on CRAN, as that skips the rest of the test. Instead it just returns, neither succeeding nor failing (#1585).
* Interrupting a test now prints the test name. This makes it easier to tell where a very slow test might be hanging (#1464)
* Parallel testthat now does not ignore test files with syntax errors (#1360).
Expand Down
5 changes: 5 additions & 0 deletions R/test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
}
},
expectation = handle_expectation,
packageNotFoundError = function(e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like a great & reasonable compromise -- minimal implementation & fits well with existing approach that always sets !on_cran in package GHA.

one possible concern is that it doesn't make any effort to distinguish "package missing in current environment" from "you forgot to add a package to your Suggests". I guess R CMD check is inspecting tests/ files for pkg:: usage so it should be caught anyway.

if (on_cran()) {
skip(paste0("{", e$package, "} is not installed."))
}
},
skip = handle_skip,
warning = handle_warning,
message = handle_message,
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,20 @@ test_that("no braces required in testthat 2e", {
NA
)
})

test_that("missing packages cause a skip on CRAN", {
local_on_cran(TRUE)

expectations <- capture_expectations(test_that("", {
library(notinstalled)
}))
expect_length(expectations, 1)
expect_s3_class(expectations[[1]], "expectation_skip")

local_on_cran(FALSE)
expectations <- capture_expectations(test_that("", {
library(notinstalled)
}))
expect_length(expectations, 1)
expect_s3_class(expectations[[1]], "expectation_error")
})
Loading