BrodieG BrodieG - 4 months ago 30
R Question

Is there something in testthat like expect_no_warnings()?

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

. I guess I could do something like:

my.result <- 25
my.func <- function() my.result
my.func(), warning=function() stop("A Warning!")

or use
, but I was hoping there would be something like:


Am I missing something obvious?


In even more recent versions of ┬┤testthat┬┤ (from 0.11.0) you can do:

expect_warning(my.func(), regexp = NA)

From the documentation of expect_error

regexp: regular expression to test against. If omitted, just asserts that code produces some output, messsage, warning or error. Alternatively, you can specify NA to indicate that there should be no output, messages, warnings or errors.

So in the same way you can test that there are no messages, errors and output.