Skip to content

Commit be7de74

Browse files
committed
More improvements
* Better handling for nested tests * Never show locations * Polish emoji spacing
1 parent eed24f0 commit be7de74

File tree

8 files changed

+82
-38
lines changed

8 files changed

+82
-38
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# testthat (development version)
22

3-
* When running a test interactively, testthat now reports the number of succeses.
3+
* When running a test interactively, testthat now reports the number of succeses. The results should also be more useful if you are using nested tests.
44
* 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).
55
* `expect_snapshot_file()` now clearly errors if the `path` doesnt exist (#2191).
66
* `expect_snapshot_file()` now considers `.json` to be a text file (#1593).

R/praise.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ praise_emoji <- function() {
4343
"\U0001f389", # party popper
4444
"\U0001f38a" # confetti ball
4545
)
46-
sample(emoji, 1)
46+
paste0(" ", sample(emoji, 1))
4747
}
4848

4949
encourage <- function() {

R/reporter-progress.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ spinner <- function(frames, i) {
558558
frames[((i - 1) %% length(frames)) + 1]
559559
}
560560

561-
issue_header <- function(x, pad = FALSE) {
561+
issue_header <- function(x, pad = FALSE, location = TRUE) {
562562
type <- expectation_type(x)
563563
if (has_colour()) {
564564
type <- colourise(first_upper(type), type)
@@ -569,11 +569,11 @@ issue_header <- function(x, pad = FALSE) {
569569
type <- strpad(type, 7)
570570
}
571571

572-
paste0(type, expectation_location(x, " (", ")"), ": ", x$test)
572+
paste0(type, if (location) expectation_location(x, " (", ")"), ": ", x$test)
573573
}
574574

575-
issue_summary <- function(x, rule = FALSE) {
576-
header <- cli::style_bold(issue_header(x))
575+
issue_summary <- function(x, rule = FALSE, location = TRUE) {
576+
header <- cli::style_bold(issue_header(x, location = location))
577577
if (rule) {
578578
# Don't truncate long test names
579579
width <- max(cli::ansi_nchar(header) + 6, getOption("width"))

R/reporter-stop.R

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
#'
33
#' @description
44
#' The default reporter used when [expect_that()] is run interactively.
5-
#' It responds by [stop()]ping on failures and doing nothing otherwise. This
6-
#' will ensure that a failing test will raise an error.
7-
#'
8-
#' This should be used when doing a quick and dirty test, or during the final
9-
#' automated testing of R CMD check. Otherwise, use a reporter that runs all
10-
#' tests and gives you more context about the problem.
5+
#' It responds by displaying a summary of the number of successes and faiures
6+
#' and [stop()]ping on if there are any failures.
117
#'
128
#' @export
139
#' @family reporters
@@ -22,6 +18,7 @@ StopReporter <- R6::R6Class(
2218
# Successful expectations
2319
n_success = 0L,
2420
praise = TRUE,
21+
depth = 0,
2522

2623
initialize = function(praise = TRUE) {
2724
super$initialize()
@@ -30,9 +27,12 @@ StopReporter <- R6::R6Class(
3027
},
3128

3229
start_test = function(context, test) {
33-
self$issues <- Stack$new()
34-
self$n_fail <- 0L
35-
self$n_success <- 0L
30+
if (self$depth == 0) {
31+
self$n_fail <- 0L
32+
self$n_success <- 0L
33+
self$issues <- Stack$new()
34+
}
35+
self$depth <- self$depth + 1
3636
},
3737

3838
add_result = function(context, test, result) {
@@ -48,16 +48,21 @@ StopReporter <- R6::R6Class(
4848
},
4949

5050
end_test = function(context, test) {
51+
self$depth <- self$depth - 1
52+
if (self$depth > 0) {
53+
return()
54+
}
55+
5156
self$local_user_output()
5257

5358
for (issue in self$issues$as_list()) {
54-
self$cat_line(issue_summary(issue, rule = TRUE), "\n")
59+
self$cat_line(issue_summary(issue, rule = TRUE, location = FALSE))
5560
}
5661

5762
if (self$praise && self$n_fail == 0 && self$n_success > 0) {
5863
emoji <- praise_emoji()
5964
self$cat_line(cli::format_inline(
60-
"Test passed with {self$n_success} success{?es} {emoji}."
65+
"{.strong Test passed with {self$n_success} success{?es}{emoji}}."
6166
))
6267
}
6368

man/StopReporter.Rd

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/reporter-stop.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
# produces useful output
22

3-
Test passed with 1 success .
4-
-- Failure ('reporters/tests.R:13:3'): Failure:1 -------------------------------
5-
Expected `x` to be TRUE.
6-
Differences:
7-
`actual`: FALSE
8-
`expected`: TRUE
9-
10-
3+
Code
4+
with_reporter("stop", run_tests())
5+
Output
6+
Test passed with 1 success.
7+
-- Failure: Failure:1 ----------------------------------------------------------
8+
Expected `x` to be TRUE.
9+
Differences:
10+
`actual`: FALSE
11+
`expected`: TRUE
12+
13+
Condition
14+
Error:
15+
! Test failed with 1 failure and 0 successes.
1116

12-
# can suppress praise
17+
# works nicely with nested tests
1318

14-
19+
Code
20+
with_reporter("stop", run_tests())
21+
Output
22+
Test passed with 2 successes.
23+
-- Failure: failed then succeeded / failed-1 -----------------------------------
24+
Expected FALSE to be TRUE.
25+
Differences:
26+
`actual`: FALSE
27+
`expected`: TRUE
28+
29+
-- Failure: failed then succeeded / failed-2 -----------------------------------
30+
Expected FALSE to be TRUE.
31+
Differences:
32+
`actual`: FALSE
33+
`expected`: TRUE
34+
35+
Condition
36+
Error:
37+
! Test failed with 2 failures and 1 success.
1538

1639
# errors when needed
1740

tests/testthat/reporters/nested.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
describe("succeeded", {
2+
it("succeeded-1", expect_true(TRUE))
3+
it("succeeded-2", expect_true(TRUE))
4+
})
5+
6+
describe("failed then succeeded", {
7+
it("failed-1", expect_true(FALSE))
8+
it("failed-2", expect_true(FALSE))
9+
it("succeeded", expect_true(TRUE))
10+
})

tests/testthat/test-reporter-stop.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1+
# We can't use expect_snapshot_reporter() here because it uses test_one_file()
2+
# which wraps code in `test_code()` which turns the error into a test failure
3+
# It also only captures the output, but we also want to see the error
4+
15
test_that("produces useful output", {
2-
expect_snapshot_reporter(StopReporter$new())
6+
run_tests <- \() source(test_path("reporters/tests.R"))
7+
expect_snapshot(with_reporter("stop", run_tests()), error = TRUE)
38
})
49

510
test_that("can suppress praise", {
6-
expect_snapshot_reporter(
7-
StopReporter$new(praise = FALSE),
8-
test_path("reporters/successes.R")
9-
)
11+
run_tests <- \() source(test_path("reporters/successes.R"))
12+
expect_silent(with_reporter(StopReporter$new(praise = FALSE), run_tests()))
13+
})
14+
15+
test_that("works nicely with nested tests", {
16+
run_tests <- \() source(test_path("reporters/nested.R"))
17+
expect_snapshot(with_reporter("stop", run_tests()), error = TRUE)
1018
})
1119

1220
test_that("errors when needed", {
1321
r <- StopReporter$new()
22+
r$start_test()
1423
expect_no_error(r$end_test())
1524

25+
r$start_test()
1626
r$n_fail <- 1
1727
r$n_success <- 0
1828
expect_snapshot(error = TRUE, r$end_test())

0 commit comments

Comments
 (0)