rpurrrtestthat

Test that stops at first fail when used with map


I am pretty sure that this:

test_that("", {
  Parameters <- c(1, 2, 3, 4, 5)
  test_map <- function(Parameter){
    expect_equal(Parameter,
                 2)
}
  map(Parameters, test_map)
})

resulted in 4 failed and 1 passed test in the past. However it seems like since recently this doesnt work anymore and exits immediatly at the first failed test. Is there a way around this? Possibly connected to the new reporters?


Solution

  • You can use Map() instead of purrr::map():

    library(testthat)
    
    test_that("", {
      Parameters <- c(1, 2, 3, 4, 5)
      test_map <- function(Parameter){
        expect_equal(Parameter,
                     2)
      }
      Map(test_map, Parameters)
    })
    #> ── Failure ('<text>:9:3'):  ────────────────────────────────────────────────────
    #> `Parameter` not equal to 2.
    #> 1/1 mismatches
    #> [1] 1 - 2 == -1
    #> Backtrace:
    #>  1. base::Map(test_map, Parameters)
    #>  2. base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
    #>  3. `<fn>`(dots[[1L]][[5L]])
    #>  4. testthat::expect_equal(Parameter, 2)
    #> 
    #> ── Failure ('<text>:9:3'):  ────────────────────────────────────────────────────
    #> `Parameter` not equal to 2.
    #> 1/1 mismatches
    #> [1] 3 - 2 == 1
    #> Backtrace:
    #>  1. base::Map(test_map, Parameters)
    #>  2. base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
    #>  3. `<fn>`(dots[[1L]][[5L]])
    #>  4. testthat::expect_equal(Parameter, 2)
    #> 
    #> ── Failure ('<text>:9:3'):  ────────────────────────────────────────────────────
    #> `Parameter` not equal to 2.
    #> 1/1 mismatches
    #> [1] 4 - 2 == 2
    #> Backtrace:
    #>  1. base::Map(test_map, Parameters)
    #>  2. base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
    #>  3. `<fn>`(dots[[1L]][[5L]])
    #>  4. testthat::expect_equal(Parameter, 2)
    #> 
    #> ── Failure ('<text>:9:3'):  ────────────────────────────────────────────────────
    #> `Parameter` not equal to 2.
    #> 1/1 mismatches
    #> [1] 5 - 2 == 3
    #> Backtrace:
    #>  1. base::Map(test_map, Parameters)
    #>  2. base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
    #>  3. `<fn>`(dots[[1L]][[5L]])
    #>  4. testthat::expect_equal(Parameter, 2)
    #> Error in `reporter$stop_if_needed()`:
    #> ! Test failed
    #> Backtrace:
    #>      ▆
    #>   1. └─testthat::test_that(...)
    #>   2.   └─withr (local) `<fn>`(`<env>`)
    #>   3.     ├─base::tryCatch(...)
    #>   4.     │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
    #>   5.     │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
    #>   6.     │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
    #>   7.     └─base::eval(handler$expr, handler$envir)
    #>   8.       └─base::eval(handler$expr, handler$envir)
    #>   9.         └─reporter$stop_if_needed()
    #>  10.           └─rlang::abort("Test failed")
    

    Created on 2023-04-24 with reprex v2.0.2