| expect_message {testthat} | R Documentation |
Use expect_message() and expect_warning() to check if the messages or
warnings match the given regular expression.
expect_message( object, regexp = NULL, ..., all = FALSE, info = NULL, label = NULL ) expect_warning( object, regexp = NULL, ..., all = FALSE, info = NULL, label = NULL )
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. |
regexp |
Regular expression to test against.
|
... |
Arguments passed on to
|
all |
Do messages/warnings need to match the |
info |
Extra information to be included in the message. This argument is soft-deprecated and should not be used in new code. Instead see alternatives in quasi_label. |
label |
Used to customise failure messages. For expert use only. |
The first argument, invisibly.
Other expectations:
comparison-expectations,
equality-expectations,
expect_error(),
expect_length(),
expect_match(),
expect_named(),
expect_null(),
expect_output(),
expect_silent(),
inheritance-expectations,
logical-expectations
# Messages ------------------------------------------------------------------
f <- function(x) {
if (x < 0) {
message("*x* is already negative")
return(x)
}
-x
}
expect_message(f(-1))
expect_message(f(-1), "already negative")
expect_message(f(1), NA)
# To test message and output, store results to a variable
expect_message(out <- f(-1), "already negative")
expect_equal(out, -1)
# You can use the arguments of grepl to control the matching
expect_message(f(-1), "*x*", fixed = TRUE)
expect_message(f(-1), "NEGATIVE", ignore.case = TRUE)
# Warnings ------------------------------------------------------------------
f <- function(x) {
if (x < 0) {
warning("*x* is already negative")
return(x)
}
-x
}
expect_warning(f(-1))
expect_warning(f(-1), "already negative")
expect_warning(f(1), NA)
# To test message and output, store results to a variable
expect_warning(out <- f(-1), "already negative")
expect_equal(out, -1)
# You can use the arguments of grepl to control the matching
expect_warning(f(-1), "*x*", fixed = TRUE)
expect_warning(f(-1), "NEGATIVE", ignore.case = TRUE)