diff --git a/R/expect-comparison.R b/R/expect-comparison.R index 985c065aa..90df837cf 100644 --- a/R/expect-comparison.R +++ b/R/expect-comparison.R @@ -39,7 +39,10 @@ expect_compare_ <- function( cmp <- op(act$val, exp$val) if (length(cmp) != 1 || !is.logical(cmp)) { - abort("Result of comparison must be a single logical value") + abort( + "Result of comparison must be a single logical value", + call = trace_env + ) } if (!isTRUE(cmp)) { digits <- max( diff --git a/R/skip.R b/R/skip.R index 3903a1c1d..3abc8ae1a 100644 --- a/R/skip.R +++ b/R/skip.R @@ -138,10 +138,12 @@ package_version <- function(x) { #' should only be run on R versions 4.1.0 and later. #' @rdname skip skip_unless_r <- function(spec) { + check_string(spec) + parts <- unlist(strsplit(spec, " ", fixed = TRUE)) if (length(parts) != 2L) { cli::cli_abort( - "{.arg spec} should be a comparison like '>=' and an R version separated by a space." + "{.arg spec} must be an valid version specification, like {.str >= 4.0.0}, not {.str {spec}}." ) } comparator <- match.fun(parts[1L]) diff --git a/tests/testthat/_snaps/examples.md b/tests/testthat/_snaps/examples.md new file mode 100644 index 000000000..ad79089ce --- /dev/null +++ b/tests/testthat/_snaps/examples.md @@ -0,0 +1,8 @@ +# test_examples fails if no examples + + Code + test_examples("asdf") + Condition + Error: + ! Could not find examples + diff --git a/tests/testthat/_snaps/expect-comparison.md b/tests/testthat/_snaps/expect-comparison.md index ad689bbba..8b7000271 100644 --- a/tests/testthat/_snaps/expect-comparison.md +++ b/tests/testthat/_snaps/expect-comparison.md @@ -18,3 +18,11 @@ `x` is not strictly less than 0.001. 100.000 - 0.001 = 99.999 >= 0 +# comparison must yield a single logical + + Code + expect_lt(1:10, 5) + Condition + Error in `expect_lt()`: + ! Result of comparison must be a single logical value + diff --git a/tests/testthat/_snaps/expect-setequal.md b/tests/testthat/_snaps/expect-setequal.md index 50d57a319..14d806e2d 100644 --- a/tests/testthat/_snaps/expect-setequal.md +++ b/tests/testthat/_snaps/expect-setequal.md @@ -1,3 +1,11 @@ +# warns if both inputs are named + + Code + expect_setequal(c(a = 1), c(b = 1)) + Condition + Warning: + expect_setequal() ignores names + # checks inputs Code diff --git a/tests/testthat/_snaps/reporter-stop.md b/tests/testthat/_snaps/reporter-stop.md index c57c94761..febb63df5 100644 --- a/tests/testthat/_snaps/reporter-stop.md +++ b/tests/testthat/_snaps/reporter-stop.md @@ -48,3 +48,11 @@ +# stop if needed errors when needed + + Code + r$stop_if_needed() + Condition + Error: + ! Test failed + diff --git a/tests/testthat/_snaps/reporter-zzz.md b/tests/testthat/_snaps/reporter-zzz.md new file mode 100644 index 000000000..dbe7b5c13 --- /dev/null +++ b/tests/testthat/_snaps/reporter-zzz.md @@ -0,0 +1,8 @@ +# useful error message if can't find reporter + + Code + find_reporter(c("summary", "blah")) + Condition + Error: + ! Can not find test reporter blah + diff --git a/tests/testthat/_snaps/skip.md b/tests/testthat/_snaps/skip.md index 98959d007..2bc5545a9 100644 --- a/tests/testthat/_snaps/skip.md +++ b/tests/testthat/_snaps/skip.md @@ -78,6 +78,14 @@ Reason: On Windows i386 +# skip_unless_r works as expected + + Code + skip_unless_r("idfjdij") + Condition + Error in `skip_unless_r()`: + ! `spec` must be an valid version specification, like ">= 4.0.0", not "idfjdij". + # skip_unless_r gives the expected output Reason: Current R version (4.5.0) does not satisfy requirement (>= 999.999.999) diff --git a/tests/testthat/_snaps/snapshot-file.md b/tests/testthat/_snaps/snapshot-file.md index 6b2f9e65b..a97a546e2 100644 --- a/tests/testthat/_snaps/snapshot-file.md +++ b/tests/testthat/_snaps/snapshot-file.md @@ -1,3 +1,19 @@ +# warns on first creation + + Code + out <- snapshot_file_equal(tempdir(), "test.txt", NULL, path) + Condition + Warning: + Adding new file snapshot: 'tests/testthat/_snaps/test.txt' + +--- + + Code + expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, "doesnt-exist.txt")) + Condition + Error in `snapshot_file_equal()`: + ! `doesnt-exist.txt` not found + # snapshot_hint output differs in R CMD check Code diff --git a/tests/testthat/_snaps/test-compiled-code.md b/tests/testthat/_snaps/test-compiled-code.md index 0b64e5a0e..67f4c2e43 100644 --- a/tests/testthat/_snaps/test-compiled-code.md +++ b/tests/testthat/_snaps/test-compiled-code.md @@ -1,3 +1,11 @@ +# get_routine() fails when no routine exists + + Code + get_routine("utils", "no_such_routine") + Condition + Error: + ! failed to locate routine 'no_such_routine' in package 'utils' + # validates inputs Code diff --git a/tests/testthat/_snaps/test-files.md b/tests/testthat/_snaps/test-files.md index 13e011d73..e23707c0c 100644 --- a/tests/testthat/_snaps/test-files.md +++ b/tests/testthat/_snaps/test-files.md @@ -1,3 +1,11 @@ +# stops on failure + + Code + test_dir(test_path("test_dir"), reporter = "silent") + Condition + Error: + ! Test failures + # runs all tests and records output file context test nb failed skipped error warning passed @@ -19,3 +27,35 @@ 16 test-helper.R helper test 1 0 FALSE FALSE 0 1 17 test-skip.R Skips skip 1 0 TRUE FALSE 0 0 +# complains if no files + + Code + test_dir(path) + Condition + Error in `test_dir()`: + ! No test files found + +# can control if failures generate errors + + Code + test_error(stop_on_failure = TRUE) + Condition + Error: + ! Test failures + +# can control if warnings errors + + Code + test_warning(stop_on_warning = TRUE) + Condition + Error: + ! Tests generated warnings + +# complains if file doesn't exist + + Code + test_file("DOESNTEXIST") + Condition + Error: + ! `path` does not exist + diff --git a/tests/testthat/_snaps/verify-output.md b/tests/testthat/_snaps/verify-output.md new file mode 100644 index 000000000..222181b75 --- /dev/null +++ b/tests/testthat/_snaps/verify-output.md @@ -0,0 +1,8 @@ +# can't record plots + + Code + verify_output(tempfile(), plot(1:10)) + Condition + Error in `FUN()`: + ! Plots are not supported + diff --git a/tests/testthat/test-edition.R b/tests/testthat/test-edition.R index 24556f7e8..51e4d9bdc 100644 --- a/tests/testthat/test-edition.R +++ b/tests/testthat/test-edition.R @@ -15,7 +15,7 @@ test_that("checks its inputs", { test_that("deprecation only fired for newer edition", { local_edition(2) - expect_warning(edition_deprecate(3, "old stuff"), NA) + expect_no_warning(edition_deprecate(3, "old stuff")) local_edition(3) expect_snapshot(edition_deprecate(3, "old stuff")) @@ -28,7 +28,7 @@ test_that("required only fired for older edition", { withr::local_options(testthat.edition_ignore = FALSE) local_edition(3) - expect_error(edition_require(3, "new stuff"), NA) + expect_no_error(edition_require(3, "new stuff")) }) test_that("edition for testthat is 3", { diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R index b584857c5..964d4141a 100644 --- a/tests/testthat/test-examples.R +++ b/tests/testthat/test-examples.R @@ -7,5 +7,5 @@ test_that("test_examples works with installed packages", { test_that("test_examples fails if no examples", { withr::local_envvar(TESTTHAT_PKG = "") - expect_error(test_examples("asdf"), "Could not find examples") + expect_snapshot(error = TRUE, test_examples("asdf")) }) diff --git a/tests/testthat/test-expect-comparison.R b/tests/testthat/test-expect-comparison.R index 89df396b7..b1ea3e279 100644 --- a/tests/testthat/test-expect-comparison.R +++ b/tests/testthat/test-expect-comparison.R @@ -61,7 +61,7 @@ test_that("comparisons with more complicated objects work", { }) test_that("comparison must yield a single logical", { - expect_error(expect_lt(1:10, 5), "single logical") + expect_snapshot(error = TRUE, expect_lt(1:10, 5)) }) test_that("wordy versions are deprecated", { diff --git a/tests/testthat/test-expect-setequal.R b/tests/testthat/test-expect-setequal.R index dc87b223d..4dd41b0de 100644 --- a/tests/testthat/test-expect-setequal.R +++ b/tests/testthat/test-expect-setequal.R @@ -22,7 +22,7 @@ test_that("can compare data frames", { }) test_that("warns if both inputs are named", { - expect_warning(expect_setequal(c(a = 1), c(b = 1)), "ignores names") + expect_snapshot(expect_setequal(c(a = 1), c(b = 1))) }) test_that("checks inputs", { diff --git a/tests/testthat/test-reporter-list.R b/tests/testthat/test-reporter-list.R index e0bd3df3e..0c22d0dfa 100644 --- a/tests/testthat/test-reporter-list.R +++ b/tests/testthat/test-reporter-list.R @@ -1,7 +1,7 @@ # regression test: test_file() used to crash with a NULL reporter test_that("ListReporter with test_file and NULL reporter", { test_file_path <- 'test-list-reporter/test-exercise-list-reporter.R' - expect_error(test_file(test_path(test_file_path), reporter = NULL), NA) + expect_no_error(test_file(test_path(test_file_path), reporter = NULL)) }) # regression: check that an exception is reported if it is raised in the test file outside diff --git a/tests/testthat/test-reporter-stop.R b/tests/testthat/test-reporter-stop.R index b5ca1ac07..2b05e2b3f 100644 --- a/tests/testthat/test-reporter-stop.R +++ b/tests/testthat/test-reporter-stop.R @@ -11,9 +11,9 @@ test_that("can suppress praise", { test_that("stop if needed errors when needed", { r <- StopReporter$new() - expect_error(r$stop_if_needed(), NA) + expect_no_error(r$stop_if_needed()) r$n_fail <- 1 - expect_error(r$stop_if_needed(), "Test failed") + expect_snapshot(error = TRUE, r$stop_if_needed()) r$stop_reporter <- FALSE - expect_error(r$stop_if_needed(), NA) + expect_no_error(r$stop_if_needed()) }) diff --git a/tests/testthat/test-reporter-zzz.R b/tests/testthat/test-reporter-zzz.R index 056fa6eef..6bbf9c1a1 100644 --- a/tests/testthat/test-reporter-zzz.R +++ b/tests/testthat/test-reporter-zzz.R @@ -4,10 +4,9 @@ test_that("can locate reporter from name", { }) test_that("useful error message if can't find reporter", { - expect_error( - find_reporter(c("summary", "blah")), - "Can not find test reporter blah" - ) + expect_snapshot(error = TRUE, { + find_reporter(c("summary", "blah")) + }) }) test_that("character vector yields multi reporter", { diff --git a/tests/testthat/test-skip.R b/tests/testthat/test-skip.R index 032475aff..9ed8887fa 100644 --- a/tests/testthat/test-skip.R +++ b/tests/testthat/test-skip.R @@ -170,11 +170,7 @@ test_that("skip_unless_r works as expected", { expect_skip(skip_unless_r("== 0.0.0")) expect_skip(skip_unless_r("<= 0.0.0")) - expect_error( - skip_unless_r("idfjdij"), - "should be a comparison like '>='", - fixed = TRUE - ) + expect_snapshot(error = TRUE, skip_unless_r("idfjdij")) }) test_that("skip_unless_r gives the expected output", { diff --git a/tests/testthat/test-snapshot-file.R b/tests/testthat/test-snapshot-file.R index 2a0d46c66..193761087 100644 --- a/tests/testthat/test-snapshot-file.R +++ b/tests/testthat/test-snapshot-file.R @@ -38,10 +38,7 @@ test_that("expect_snapshot_file works in a different directory", { brio::write_lines("a", "a.txt", eol = "\r\n") # expect no warning - expect_warning( - expect_snapshot_file("a.txt"), - regexp = NA - ) + expect_no_warning(expect_snapshot_file("a.txt")) }) test_that("expect_snapshot_file works with variant", { @@ -99,21 +96,18 @@ test_that("warns on first creation", { withr::defer(unlink(file.path(tempdir(), "test.txt"))) # Warns on first run - expect_warning( - expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path)), - "new file snapshot" - ) + expect_snapshot(out <- snapshot_file_equal(tempdir(), "test.txt", NULL, path)) + expect_true(out) # Errors on non-existing file - expect_error( + expect_snapshot(error = TRUE, { expect_true(snapshot_file_equal( tempdir(), "test.txt", NULL, "doesnt-exist.txt" - )), - "`doesnt-exist.txt` not found" - ) + )) + }) # Unchanged returns TRUE expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path)) diff --git a/tests/testthat/test-test-compiled-code.R b/tests/testthat/test-test-compiled-code.R index a02aacf23..d9fba9b2b 100644 --- a/tests/testthat/test-test-compiled-code.R +++ b/tests/testthat/test-test-compiled-code.R @@ -4,7 +4,7 @@ test_that("get_routine() finds own 'run_testthat_tests'", { }) test_that("get_routine() fails when no routine exists", { - expect_error(get_routine("utils", "no_such_routine")) + expect_snapshot(error = TRUE, get_routine("utils", "no_such_routine")) }) test_that("validates inputs", { diff --git a/tests/testthat/test-test-env.R b/tests/testthat/test-test-env.R index cbce82157..f106256bb 100644 --- a/tests/testthat/test-test-env.R +++ b/tests/testthat/test-test-env.R @@ -5,7 +5,7 @@ test_that("environment has package name", { setClass("MyClass") test_that("Cannot create S4 class without special behaviour", { - expect_error(setClass("MyClass2"), NA) + expect_no_error(setClass("MyClass2")) }) test_that("is_checking respects env var", { diff --git a/tests/testthat/test-test-files.R b/tests/testthat/test-test-files.R index b5f226bd2..3243f59f9 100644 --- a/tests/testthat/test-test-files.R +++ b/tests/testthat/test-test-files.R @@ -2,9 +2,9 @@ test_that("stops on failure", { withr::local_envvar(TESTTHAT_PARALLEL = "FALSE") - expect_error( + expect_snapshot(error = TRUE, { test_dir(test_path("test_dir"), reporter = "silent") - ) + }) }) test_that("runs all tests and records output", { @@ -27,7 +27,7 @@ test_that("complains if no files", { path <- withr::local_tempfile() dir.create(path) - expect_error(test_dir(path), "test files") + expect_snapshot(error = TRUE, test_dir(path)) }) test_that("can control if failures generate errors", { @@ -36,8 +36,8 @@ test_that("can control if failures generate errors", { test_dir(test_path("test-error"), reporter = "silent", ...) } - expect_error(test_error(stop_on_failure = TRUE), "Test failures") - expect_error(test_error(stop_on_failure = FALSE), NA) + expect_snapshot(error = TRUE, test_error(stop_on_failure = TRUE)) + expect_no_error(test_error(stop_on_failure = FALSE)) }) test_that("can control if warnings errors", { @@ -46,8 +46,8 @@ test_that("can control if warnings errors", { test_dir(test_path("test-warning"), reporter = "silent", ...) } - expect_error(test_warning(stop_on_warning = TRUE), "Tests generated warnings") - expect_error(test_warning(stop_on_warning = FALSE), NA) + expect_snapshot(error = TRUE, test_warning(stop_on_warning = TRUE)) + expect_no_error(test_warning(stop_on_warning = FALSE)) }) # test_file --------------------------------------------------------------- @@ -58,7 +58,7 @@ test_that("can test single file", { }) test_that("complains if file doesn't exist", { - expect_error(test_file("DOESNTEXIST"), "does not exist") + expect_snapshot(error = TRUE, test_file("DOESNTEXIST")) }) diff --git a/tests/testthat/test-verify-output.R b/tests/testthat/test-verify-output.R index 577ca9cff..79f54cac4 100644 --- a/tests/testthat/test-verify-output.R +++ b/tests/testthat/test-verify-output.R @@ -49,7 +49,7 @@ test_that("can record all types of output", { test_that("can't record plots", { skip_if(interactive()) - expect_error(verify_output(tempfile(), plot(1:10)), "Plots") + expect_snapshot(error = TRUE, verify_output(tempfile(), plot(1:10))) }) test_that("verify_output() splits condition messages on newlines", {