Skip to content

Commit 0ce7295

Browse files
authored
Ensure interactive tests error appropriately (#1443)
Fixes #1430
1 parent d8e51f2 commit 0ce7295

10 files changed

+56
-64
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# testthat (development version)
22

3+
* When run interactively, `test_that()` now correctly errors when an expectation
4+
fails (#1430).
5+
36
* Uncaught errors now show their class (#1426).
47

58
* `local_reproducible_output()` now sets the `max.print` option to 99999

R/local.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ local_test_context <- function(.env = parent.frame()) {
7575
#' The test is skipped if `` l10n_info()$`UTF-8` `` is `FALSE`.
7676
#' @rdname local_test_context
7777
#' @examples
78-
#'
79-
#' ellipsis <- cli::symbol$ellipsis
8078
#' test_that("test ellipsis", {
81-
#' expect_equal(ellipsis, cli::symbol$ellipsis)
79+
#' local_reproducible_output(unicode = FALSE)
80+
#' expect_equal(cli::symbol$ellipsis, "...")
8281
#'
8382
#' local_reproducible_output(unicode = TRUE)
84-
#' expect_equal(ellipsis, cli::symbol$ellipsis)
83+
#' expect_equal(cli::symbol$ellipsis, "\u2026")
8584
#' })
8685
local_reproducible_output <- function(width = 80,
8786
crayon = FALSE,
@@ -163,8 +162,9 @@ local_interactive_reporter <- function(.env = parent.frame()) {
163162
local_edition(find_edition("."), .env = .env)
164163

165164
# Use StopReporter
166-
reporter <- StopReporter$new(stop_reporter = FALSE)
165+
reporter <- StopReporter$new()
167166
old <- set_reporter(reporter)
167+
withr::defer(reporter$stop_if_needed(), envir = .env)
168168
withr::defer(set_reporter(old), envir = .env)
169169

170170
reporter

R/reporter-stop.R

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#' Test reporter: stop on error.
1+
#' Test reporter: stop on error
22
#'
3-
#' The default reporter, executed when `expect_that` is run interactively.
3+
#' The default reporter used when [expect_that()] is run interactively.
44
#' It responds by [stop()]ping on failures and doing nothing otherwise. This
55
#' will ensure that a failing test will raise an error.
66
#'
@@ -50,9 +50,10 @@ StopReporter <- R6::R6Class("StopReporter",
5050

5151
messages <- vapply(failures, issue_summary, rule = TRUE, character(1))
5252
self$cat_line(messages, "\n")
53-
54-
if (self$stop_reporter && self$n_fail > 1) {
55-
stop_reporter("Test failed")
53+
},
54+
stop_if_needed = function() {
55+
if (self$stop_reporter && self$n_fail > 0) {
56+
abort("Test failed")
5657
}
5758
}
5859
)

R/test-that.R

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
#' Create a test.
1+
#' Run a test
22
#'
3-
#' A test encapsulates a series of expectations about small, self-contained
4-
#' set of functionality. Each test is contained in a \link{context} and
5-
#' contains multiple expectations.
3+
#' @description
4+
#' A test encapsulates a series of expectations about a small, self-contained
5+
#' set of functionality. Each test lives in a file and contains multiple
6+
#' expectations, like [expect_equal()] or [expect_error()].
67
#'
78
#' Tests are evaluated in their own environments, and should not affect
89
#' global state.
910
#'
10-
#' When run from the command line, tests return `NULL` if all
11-
#' expectations are met, otherwise it raises an error.
12-
#'
13-
#' @param desc test name. Names should be kept as brief as possible, as they
14-
#' are often used as line prefixes.
15-
#' @param code test code containing expectations. Braces (`{}`) should always be
16-
#' used in order to get accurate location data for test failures.
11+
#' @param desc Test name. Names should be brief, but evocative. They are
12+
#' only used by humans, so do you
13+
#' @param code Test code containing expectations. Braces (`{}`) should always
14+
#' be used in order to get accurate location data for test failures.
15+
#' @return When run interactively, returns `invisible(TRUE)` if all tests
16+
#' pass, otherwise throws an error.
1717
#' @export
1818
#' @examples
1919
#' test_that("trigonometric functions match identities", {
2020
#' expect_equal(sin(pi / 4), 1 / sqrt(2))
2121
#' expect_equal(cos(pi / 4), 1 / sqrt(2))
2222
#' expect_equal(tan(pi / 4), 1)
2323
#' })
24-
#' # Failing test:
24+
#'
2525
#' \dontrun{
2626
#' test_that("trigonometric functions match identities", {
2727
#' expect_equal(sin(pi / 4), 1)

man/StopReporter.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/local_test_context.Rd

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/test_that.Rd

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/reporter-stop.md

-16
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,3 @@
4141
Reason: empty test
4242

4343

44-
# stop stops at first failure
45-
46-
-- Failure (fail.R:4:3): two failures ------------------------------------------
47-
FALSE is not TRUE
48-
49-
`actual`: FALSE
50-
`expected`: TRUE
51-
52-
-- Failure (fail.R:5:3): two failures ------------------------------------------
53-
TRUE is not FALSE
54-
55-
`actual`: TRUE
56-
`expected`: FALSE
57-
58-
Test failed
59-

tests/testthat/test-reporter-stop.R

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
test_that("produces useful output", {
2-
expect_snapshot_reporter(StopReporter$new(stop_reporter = FALSE))
2+
expect_snapshot_reporter(StopReporter$new())
33
})
44

5-
test_that("stop stops at first failure", {
6-
expect_snapshot_reporter(StopReporter$new(), test_path("reporters/fail.R"))
5+
test_that("stop if needed errors when needed",{
6+
r <- StopReporter$new()
7+
expect_error(r$stop_if_needed(), NA)
8+
r$n_fail <- 1
9+
expect_error(r$stop_if_needed(), "Test failed")
10+
r$stop_reporter <- FALSE
11+
expect_error(r$stop_if_needed(), NA)
712
})

vignettes/snapshotting.Rmd

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ snapper$start_file("snapshotting.Rmd", "test")
112112

113113
But if you change the underlying code, say to tweak the indenting, the test will fail:
114114

115-
```{r}
115+
```{r, error = TRUE}
116116
bullets <- function(text, id = NULL) {
117117
paste0(
118118
"<ul", if (!is.null(id)) paste0(" id=\"", id, "\""), ">\n",
@@ -138,7 +138,7 @@ Reviewers often don't run your code but still want to understand the changes.
138138

139139
Here's the snapshot file generated by the test above:
140140

141-
``` {.md}
141+
``` md
142142
# bullets
143143

144144
<ul>
@@ -184,7 +184,7 @@ test_that("f() makes lots of noice", {
184184

185185
Capturing errors is *slightly* more difficult because `expect_snapshot()` will fail when there's an error:
186186

187-
```{r}
187+
```{r, error = TRUE}
188188
test_that("you can't add a number and a letter", {
189189
expect_snapshot(1 + "a")
190190
})

0 commit comments

Comments
 (0)