Suppress warnings in testthat functions introduces error

Viewed 246

I have a function in my package that uses tryCatch for condition handling.

f <- function(){
  tryCatch(expr =  warning("?!"), error = function(c) {message(c); 3},
           warning = function(c) {message(c); 4})
}

When I try to write a unit test for this with testthat, I always get an error related to restart or muffleWarning. Any idea how to solve this? I want to match output to 4 during warning, in this case.

test_that("test_f", {
  suppressWarnings(output <- f())
  expect_equal(output, 4)
})

> devtools::test()
 error: test_f
no 'restart' 'muffleWarning' found
Backtrace:
  1. base::suppressWarnings(output <- f()) tests/testthat/test_file.R:27:2
 14. base::invokeRestart("muffleWarning")

version R version 3.6.3 (2020-02-29) os Ubuntu 16.04.6 LTS
system x86_64, linux-gnu
testthat * 2.3.2

0 Answers
Related