diff --git a/R/try-again.R b/R/try-again.R index 977b166ab..668c66ea0 100644 --- a/R/try-again.R +++ b/R/try-again.R @@ -1,4 +1,4 @@ -#' Evaluate an expression multiple times until it succeeds +#' Evaluate an expectation multiple times until it succeeds #' #' If you have a flaky test, you can use `try_again()` to run it a few times #' until it succeeds. In most cases, you are better fixing the underlying @@ -17,7 +17,10 @@ #' expect_equal(usually_return_1(), 1) #' #' # 1% chance of failure: -#' try_again(3, expect_equal(usually_return_1(), 1)) +#' try_again(1, expect_equal(usually_return_1(), 1)) +#' +#' # 0.1% chance of failure: +#' try_again(2, expect_equal(usually_return_1(), 1)) #' } try_again <- function(times, code) { check_number_whole(times, min = 1) @@ -28,9 +31,16 @@ try_again <- function(times, code) { while (i <= times) { tryCatch( return(eval(get_expr(code), get_env(code))), - expectation_failure = function(cnd) NULL + expectation_failure = function(cnd) { + cli::cli_inform(c(i = "Expectation failed; trying again ({i})...")) + NULL + }, + error = function(cnd) { + cli::cli_inform(c(i = "Expectation errored; trying again ({i})...")) + NULL + } ) - cli::cli_inform(c(i = "Expectation failed; trying again ({i})...")) + i <- i + 1 } diff --git a/man/try_again.Rd b/man/try_again.Rd index 2d9d88462..06ff60abd 100644 --- a/man/try_again.Rd +++ b/man/try_again.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/try-again.R \name{try_again} \alias{try_again} -\title{Evaluate an expression multiple times until it succeeds} +\title{Evaluate an expectation multiple times until it succeeds} \usage{ try_again(times, code) } @@ -26,6 +26,9 @@ usually_return_1 <- function(i) { expect_equal(usually_return_1(), 1) # 1\% chance of failure: -try_again(3, expect_equal(usually_return_1(), 1)) +try_again(1, expect_equal(usually_return_1(), 1)) + +# 0.1\% chance of failure: +try_again(2, expect_equal(usually_return_1(), 1)) } } diff --git a/tests/testthat/_snaps/try-again.md b/tests/testthat/_snaps/try-again.md index 39c519f79..fe223221e 100644 --- a/tests/testthat/_snaps/try-again.md +++ b/tests/testthat/_snaps/try-again.md @@ -19,3 +19,11 @@ `actual`: 1.0 `expected`: 0.0 +# handles errors + + Code + result <- try_again(3, expect_equal(fails_twice(), 1)) + Message + i Expectation errored; trying again (1)... + i Expectation errored; trying again (2)... + diff --git a/tests/testthat/test-try-again.R b/tests/testthat/test-try-again.R index 7ef55364b..867705fd9 100644 --- a/tests/testthat/test-try-again.R +++ b/tests/testthat/test-try-again.R @@ -13,3 +13,16 @@ test_that("tries multiple times", { third_try <- succeed_after(3) expect_snapshot(try_again(1, third_try()), error = TRUE) }) + +test_that("handles errors", { + fails_twice <- local({ + i <- 0 + function() { + i <<- i + 1 + if (i <= 2) stop("fail") else 1 + } + }) + + expect_snapshot(result <- try_again(3, expect_equal(fails_twice(), 1))) + expect_equal(result, 1) +})