EVOLUTION-MANAGER
Edit File: expect_error.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Does code throw an error, warning, message, or other...</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for expect_error {testthat}"><tr><td>expect_error {testthat}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Does code throw an error, warning, message, or other condition?</h2> <h3>Description</h3> <p><code>expect_error()</code>, <code>expect_warning()</code>, <code>expect_message()</code>, and <code>expect_condition()</code> check that code throws an error, warning, message, or condition with a message that matches <code>regexp</code>, or a class that inherits from <code>class</code>. See below for more details. </p> <p>In the 3rd edition, these functions match (at most) a single condition. All additional and non-matching (if <code>regexp</code> or <code>class</code> are used) conditions will bubble up outside the expectation. If these additional conditions are important you'll need to catch them with additional <code>expect_message()</code>/<code>expect_warning()</code> calls; if they're unimportant you can ignore with <code><a href="../../base/html/message.html">suppressMessages()</a></code>/<code><a href="../../base/html/warning.html">suppressWarnings()</a></code>. </p> <p>It can be tricky to test for a combination of different conditions, such as a message followed by an error. <code><a href="expect_snapshot.html">expect_snapshot()</a></code> is often an easier alternative for these more complex cases. </p> <h3>Usage</h3> <pre> expect_error( object, regexp = NULL, class = NULL, ..., inherit = TRUE, info = NULL, label = NULL ) expect_warning( object, regexp = NULL, class = NULL, ..., inherit = TRUE, all = FALSE, info = NULL, label = NULL ) expect_message( object, regexp = NULL, class = NULL, ..., inherit = TRUE, all = FALSE, info = NULL, label = NULL ) expect_condition( object, regexp = NULL, class = NULL, ..., inherit = TRUE, info = NULL, label = NULL ) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>object</code></td> <td> <p>Object to test. </p> <p>Supports limited unquoting to make it easier to generate readable failures within a function or for loop. See <a href="quasi_label.html">quasi_label</a> for more details.</p> </td></tr> <tr valign="top"><td><code>regexp</code></td> <td> <p>Regular expression to test against. </p> <ul> <li><p> A character vector giving a regular expression that must match the error message. </p> </li> <li><p> If <code>NULL</code>, the default, asserts that there should be an error, but doesn't test for a specific value. </p> </li> <li><p> If <code>NA</code>, asserts that there should be no errors, but we now recommend using <code><a href="expect_no_error.html">expect_no_error()</a></code> and friends instead. </p> </li></ul> <p>Note that you should only use <code>message</code> with errors/warnings/messages that you generate. 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 <code>skip_on_cran()</code> or use <code>expect_snapshot()</code>.</p> </td></tr> <tr valign="top"><td><code>class</code></td> <td> <p>Instead of supplying a regular expression, you can also supply a class name. This is useful for "classed" conditions.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Arguments passed on to <code><a href="expect_match.html">expect_match</a></code> </p> <dl> <dt><code>fixed</code></dt><dd><p>If <code>TRUE</code>, treats <code>regexp</code> as a string to be matched exactly (not a regular expressions). Overrides <code>perl</code>.</p> </dd> <dt><code>perl</code></dt><dd><p>logical. Should Perl-compatible regexps be used?</p> </dd> </dl> </td></tr> <tr valign="top"><td><code>inherit</code></td> <td> <p>Whether to match <code>regexp</code> and <code>class</code> across the ancestry of chained errors.</p> </td></tr> <tr valign="top"><td><code>info</code></td> <td> <p>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 <a href="quasi_label.html">quasi_label</a>.</p> </td></tr> <tr valign="top"><td><code>label</code></td> <td> <p>Used to customise failure messages. For expert use only.</p> </td></tr> <tr valign="top"><td><code>all</code></td> <td> <p><em>DEPRECATED</em> If you need to test multiple warnings/messages you now need to use multiple calls to <code>expect_message()</code>/ <code>expect_warning()</code></p> </td></tr> </table> <h3>Value</h3> <p>If <code>regexp = NA</code>, the value of the first argument; otherwise the captured condition. </p> <h3>Testing <code>message</code> vs <code>class</code></h3> <p>When checking that code generates an error, it's important to check that the error is the one you expect. There are two ways to do this. The first way is the simplest: you just provide a <code>regexp</code> that match some fragment of the error message. This is easy, but fragile, because the test will fail if the error message changes (even if its the same error). </p> <p>A more robust way is to test for the class of the error, if it has one. You can learn more about custom conditions at <a href="https://adv-r.hadley.nz/conditions.html#custom-conditions">https://adv-r.hadley.nz/conditions.html#custom-conditions</a>, but in short, errors are S3 classes and you can generate a custom class and check for it using <code>class</code> instead of <code>regexp</code>. </p> <p>If you are using <code>expect_error()</code> to check that an error message is formatted in such a way that it makes sense to a human, we recommend using <code><a href="expect_snapshot.html">expect_snapshot()</a></code> instead. </p> <h3>See Also</h3> <p><code><a href="expect_no_error.html">expect_no_error()</a></code>, <code>expect_no_warning()</code>, <code>expect_no_message()</code>, and <code>expect_no_condition()</code> to assert that code runs without errors/warnings/messages/conditions. </p> <p>Other expectations: <code><a href="comparison-expectations.html">comparison-expectations</a></code>, <code><a href="equality-expectations.html">equality-expectations</a></code>, <code><a href="expect_length.html">expect_length</a>()</code>, <code><a href="expect_match.html">expect_match</a>()</code>, <code><a href="expect_named.html">expect_named</a>()</code>, <code><a href="expect_null.html">expect_null</a>()</code>, <code><a href="expect_output.html">expect_output</a>()</code>, <code><a href="expect_reference.html">expect_reference</a>()</code>, <code><a href="expect_silent.html">expect_silent</a>()</code>, <code><a href="inheritance-expectations.html">inheritance-expectations</a></code>, <code><a href="logical-expectations.html">logical-expectations</a></code> </p> <h3>Examples</h3> <pre> # Errors ------------------------------------------------------------------ f <- function() stop("My error!") expect_error(f()) expect_error(f(), "My error!") # You can use the arguments of grepl to control the matching expect_error(f(), "my error!", ignore.case = TRUE) # Note that `expect_error()` returns the error object so you can test # its components if needed err <- expect_error(rlang::abort("a", n = 10)) expect_equal(err$n, 10) # 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) # 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) </pre> <hr /><div style="text-align: center;">[Package <em>testthat</em> version 3.1.5 <a href="00Index.html">Index</a>]</div> </body></html>