expect_no_error: Does code run without error, warning, message, or other...

View source: R/expect-no-condition.R

expect_no_errorR Documentation

Does code run without error, warning, message, or other condition?

Description

These expectations are the opposite of expect_error(), expect_warning(), expect_message(), and expect_condition(). They assert the absence of an error, warning, or message, respectively.

Usage

expect_no_error(object, ..., message = NULL, class = NULL)

expect_no_warning(object, ..., message = NULL, class = NULL)

expect_no_message(object, ..., message = NULL, class = NULL)

expect_no_condition(object, ..., message = NULL, class = NULL)

Arguments

object

Object to test.

Supports limited unquoting to make it easier to generate readable failures within a function or for loop. See quasi_label for more details.

...

These dots are for future extensions and must be empty.

message, class

The default, ⁠message = NULL, class = NULL⁠, will fail if there is any error/warning/message/condition.

In many cases, particularly when testing warnings and messages, you will want to be more specific about the condition you are hoping not to see, i.e. the condition that motivated you to write the test. Similar to expect_error() and friends, you can specify the message (a regular expression that the message of the condition must match) and/or the class (a class the condition must inherit from). This ensures that the message/warnings you don't want never recur, while allowing new messages/warnings to bubble up for you to deal with.

Note that you should only use message with errors/warnings/messages that you generate, or that base R generates (which tend to be stable). Avoid tests that rely on the specific text generated by another package since this can easily change. If you do need to test text generated by another package, either protect the test with skip_on_cran() or use expect_snapshot().

Examples

expect_no_warning(1 + 1)

foo <- function(x) {
  warning("This is a problem!")
}

# warning doesn't match so bubbles up:
expect_no_warning(foo(), message = "bananas")

# warning does match so causes a failure:
try(expect_no_warning(foo(), message = "problem"))

testthat documentation built on May 29, 2024, 9:38 a.m.