Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions R/try-again.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
7 changes: 5 additions & 2 deletions man/try_again.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/try-again.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)...

13 changes: 13 additions & 0 deletions tests/testthat/test-try-again.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})