Skip to content

Commit 31f92e7

Browse files
committed
Better feedback when testing interactively
When a test passes, say so. Includes some refactoring to eliminate a vestigial method.
1 parent e7e44a3 commit 31f92e7

File tree

5 files changed

+33
-62
lines changed

5 files changed

+33
-62
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+
* When running a test interactively, testthat now reports the number of succeses.
34
* The hints generated by `expect_snapshot()` and `expect_snapshot_file()` now include the path to the package, if its not in the current working directory (#1577).
45
* `expect_snapshot_file()` now clearly errors if the `path` doesnt exist (#2191).
56
* `expect_snapshot_file()` now considers `.json` to be a text file (#1593).

R/local.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ local_interactive_reporter <- function(.env = parent.frame()) {
201201
reporter <- StopReporter$new()
202202
old <- set_reporter(reporter)
203203
withr::defer(reporter$end_reporter(), envir = .env)
204-
withr::defer(reporter$stop_if_needed(), envir = .env)
205204
withr::defer(set_reporter(old), envir = .env)
206205

207206
reporter

R/reporter-stop.R

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ StopReporter <- R6::R6Class(
2121
n_fail = 0L,
2222
# Successful expectations
2323
n_success = 0L,
24-
stop_reporter = TRUE,
2524
praise = TRUE,
2625

27-
initialize = function(stop_reporter = TRUE, praise = TRUE) {
26+
initialize = function(praise = TRUE) {
2827
super$initialize()
2928
self$issues <- Stack$new()
3029
self$praise <- praise
31-
self$stop_reporter <- stop_reporter
3230
},
3331

3432
start_test = function(context, test) {
3533
self$issues <- Stack$new()
34+
self$n_fail <- 0L
35+
self$n_success <- 0L
3636
},
3737

3838
add_result = function(context, test, result) {
@@ -45,25 +45,32 @@ StopReporter <- R6::R6Class(
4545
self$n_fail <- self$n_fail + 1
4646
}
4747
self$issues$push(result)
48-
49-
self$local_user_output()
50-
self$cat_line(issue_summary(result, rule = TRUE), "\n")
5148
},
5249

53-
end_reporter = function(context, test) {
50+
end_test = function(context, test) {
5451
self$local_user_output()
5552

56-
if (self$issues$size() == 0) {
57-
if (self$praise && self$n_success > 0) {
58-
emoji <- praise_emoji()
59-
self$cat_line(colourise("Test passed", "success"), " ", emoji)
60-
}
53+
for (issue in self$issues$as_list()) {
54+
self$cat_line(issue_summary(issue, rule = TRUE), "\n")
55+
}
56+
57+
if (self$praise && self$n_fail == 0 && self$n_success > 0) {
58+
emoji <- praise_emoji()
59+
self$cat_line(
60+
"Test ",
61+
colourise("passed", "success"),
62+
" with ",
63+
self$n_success,
64+
" successes ",
65+
emoji
66+
)
6167
}
62-
},
6368

64-
stop_if_needed = function() {
65-
if (self$stop_reporter && self$n_fail > 0) {
66-
cli::cli_abort("Test failed.", call = NULL)
69+
if (self$n_fail > 0) {
70+
cli::cli_abort(
71+
"Test failed with {self$n_fail} failure{?s} and {self$n_success} success{?es}.",
72+
call = NULL
73+
)
6774
}
6875
}
6976
)
Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,23 @@
11
# produces useful output
22

3+
Test passed with 1 successes
34
-- Failure ('reporters/tests.R:13:3'): Failure:1 -------------------------------
45
Expected `x` to be TRUE.
56
Differences:
67
`actual`: FALSE
78
`expected`: TRUE
89

910

10-
-- Failure ('reporters/tests.R:17:8'): Failure:2a ------------------------------
11-
Expected FALSE to be TRUE.
12-
Differences:
13-
`actual`: FALSE
14-
`expected`: TRUE
15-
16-
Backtrace:
17-
x
18-
1. \-f()
19-
2. \-testthat::expect_true(FALSE)
20-
21-
-- Error ('reporters/tests.R:24:3'): Error:1 -----------------------------------
22-
Error in `eval(code, test_env)`: stop
23-
24-
-- Error ('reporters/tests.R:30:8'): errors get tracebacks ---------------------
25-
Error in `h()`: !
26-
Backtrace:
27-
x
28-
1. \-f()
29-
2. \-g()
30-
3. \-h()
31-
32-
-- Skip ('reporters/tests.R:38:3'): explicit skips are reported ----------------
33-
Reason: skip
34-
35-
-- Skip ('reporters/tests.R:41:1'): empty tests are implicitly skipped ---------
36-
Reason: empty test
37-
38-
-- Warning ('reporters/tests.R:47:5'): warnings get backtraces -----------------
39-
def
40-
Backtrace:
41-
x
42-
1. \-f()
43-
44-
-- Skip ('reporters/tests.R:45:1'): warnings get backtraces --------------------
45-
Reason: empty test
46-
4711

4812
# can suppress praise
4913

5014

5115

52-
# stop if needed errors when needed
16+
# errors when needed
5317

5418
Code
55-
r$stop_if_needed()
19+
r$end_test()
5620
Condition
5721
Error:
58-
! Test failed.
22+
! Test failed with 1 failure and 0 successes.
5923

tests/testthat/test-reporter-stop.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ test_that("can suppress praise", {
99
)
1010
})
1111

12-
test_that("stop if needed errors when needed", {
12+
test_that("errors when needed", {
1313
r <- StopReporter$new()
14-
expect_no_error(r$stop_if_needed())
14+
expect_no_error(r$end_test())
15+
1516
r$n_fail <- 1
16-
expect_snapshot(error = TRUE, r$stop_if_needed())
17-
r$stop_reporter <- FALSE
18-
expect_no_error(r$stop_if_needed())
17+
r$n_success <- 0
18+
expect_snapshot(error = TRUE, r$end_test())
1919
})

0 commit comments

Comments
 (0)