Skip to content

Commit

Permalink
Don't emit success if test doesn't succeed (#2010)
Browse files Browse the repository at this point in the history
Fixes #1997
  • Loading branch information
hadley authored Nov 5, 2024
1 parent f5ba8a8 commit 3f70084
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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)

* `expect_no_*()` expectations no longer incorrectly emit a passing test result if they in fact fail (#1997).
* Require the latest version of waldo (0.6.0) in order to get the latest goodies (#1955).
* `expect_visible()` and `expect_invisible()` have improved failure messages (#1966).
* `expect_snapshot()` now strips line breaks in test descriptions (@LDSamson, #1900).
Expand Down
6 changes: 4 additions & 2 deletions R/expect-no-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ expect_no_ <- function(base_class,

capture <- function(code) {
try_fetch(
code,
{
code
succeed()
},
!!base_class := function(cnd) {
if (!matcher(cnd)) {
return(zap())
Expand All @@ -119,7 +122,6 @@ expect_no_ <- function(base_class,
}

act <- quasi_capture(enquo(object), NULL, capture)
succeed()
invisible(act$val)
}

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-expect-no-condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ test_that("expect_no_* conditions behave as expected", {

})

test_that("expect_no_* don't emit success when they fail", {

catch_cnds <- function(code) {
cnds <- list()

withCallingHandlers(code, condition = function(cnd) {
cnds[[length(cnds) + 1]] <<- cnd
invokeRestart("continue_test")
})
cnds
}

cnds <- catch_cnds(expect_no_error(stop("!")))
expect_length(cnds, 1)
expect_s3_class(cnds[[1]], "expectation_failure")
})

test_that("unmatched conditions bubble up", {
expect_error(expect_no_error(abort("foo"), message = "bar"), "foo")
expect_warning(expect_no_warning(warn("foo"), message = "bar"), "foo")
Expand Down

0 comments on commit 3f70084

Please sign in to comment.