I'm writing tests for a function that under some conditions will generate warnings. I want to ensure that under the other conditions it does not produce warnings. I don't see an obvious way to easily test that with testthat
. I guess I could do something like:
my.result <- 25
my.func <- function() my.result
expect_equal(
withCallingHandlers(
my.func(), warning=function() stop("A Warning!")
),
my.result
)
or use options(warn=2)
, but I was hoping there would be something like:
expect_no_warnings(my.func())
Am I missing something obvious?
options(warning=2)
– Currtestthat
stops execution on error, instead of continuing and reporting any other failed tests; 2. I can't think of an easy systematic mechanism of undoing this that will survive non-handled errors in atest_that
block. Normally I would doon.exit(options(warn=old.warn))
but to do this I'd have to wrap a function around thetest_that
block. Addingoptions(warn=old.warn)
at the end wouldn't get run with the warning failure and my options would be left changed. – Ricardaricardama