Skip to content

Commit 3f70084

Browse files
authored
Don't emit success if test doesn't succeed (#2010)
Fixes #1997
1 parent f5ba8a8 commit 3f70084

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
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+
* `expect_no_*()` expectations no longer incorrectly emit a passing test result if they in fact fail (#1997).
34
* Require the latest version of waldo (0.6.0) in order to get the latest goodies (#1955).
45
* `expect_visible()` and `expect_invisible()` have improved failure messages (#1966).
56
* `expect_snapshot()` now strips line breaks in test descriptions (@LDSamson, #1900).

R/expect-no-condition.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ expect_no_ <- function(base_class,
9696

9797
capture <- function(code) {
9898
try_fetch(
99-
code,
99+
{
100+
code
101+
succeed()
102+
},
100103
!!base_class := function(cnd) {
101104
if (!matcher(cnd)) {
102105
return(zap())
@@ -119,7 +122,6 @@ expect_no_ <- function(base_class,
119122
}
120123

121124
act <- quasi_capture(enquo(object), NULL, capture)
122-
succeed()
123125
invisible(act$val)
124126
}
125127

tests/testthat/test-expect-no-condition.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ test_that("expect_no_* conditions behave as expected", {
1313

1414
})
1515

16+
test_that("expect_no_* don't emit success when they fail", {
17+
18+
catch_cnds <- function(code) {
19+
cnds <- list()
20+
21+
withCallingHandlers(code, condition = function(cnd) {
22+
cnds[[length(cnds) + 1]] <<- cnd
23+
invokeRestart("continue_test")
24+
})
25+
cnds
26+
}
27+
28+
cnds <- catch_cnds(expect_no_error(stop("!")))
29+
expect_length(cnds, 1)
30+
expect_s3_class(cnds[[1]], "expectation_failure")
31+
})
32+
1633
test_that("unmatched conditions bubble up", {
1734
expect_error(expect_no_error(abort("foo"), message = "bar"), "foo")
1835
expect_warning(expect_no_warning(warn("foo"), message = "bar"), "foo")

0 commit comments

Comments
 (0)