EVOLUTION-MANAGER
Edit File: expect_snapshot.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: Snapshot testing</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_snapshot {testthat}"><tr><td>expect_snapshot {testthat}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Snapshot testing</h2> <h3>Description</h3> <p>Snapshot tests (aka golden tests) are similar to unit tests except that the expected result is stored in a separate file that is managed by testthat. Snapshot tests are useful for when the expected value is large, or when the intent of the code is something that can only be verified by a human (e.g. this is a useful error message). Learn more in <code>vignette("snapshotting")</code>. </p> <p><code>expect_snapshot()</code> runs code as if you had executed it at the console, and records the results, including output, messages, warnings, and errors. If you just want to compare the result, try <code><a href="expect_snapshot_value.html">expect_snapshot_value()</a></code>. </p> <h3>Usage</h3> <pre> expect_snapshot( x, cran = FALSE, error = FALSE, transform = NULL, variant = NULL, cnd_class = FALSE ) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>Code to evaluate.</p> </td></tr> <tr valign="top"><td><code>cran</code></td> <td> <p>Should these expectations be verified on CRAN? By default, they are not, because snapshot tests tend to be fragile because they often rely on minor details of dependencies.</p> </td></tr> <tr valign="top"><td><code>error</code></td> <td> <p>Do you expect the code to throw an error? The expectation will fail (even on CRAN) if an unexpected error is thrown or the expected error is not thrown.</p> </td></tr> <tr valign="top"><td><code>transform</code></td> <td> <p>Optionally, a function to scrub sensitive or stochastic text from the output. Should take a character vector of lines as input and return a modified character vector as output.</p> </td></tr> <tr valign="top"><td><code>variant</code></td> <td> <p>If non-<code>NULL</code>, results will be saved in <code style="white-space: pre;">_snaps/{variant}/{test.md}</code>, so <code>variant</code> must be a single string suitable for use as a directory name. </p> <p>You can use variants to deal with cases where the snapshot output varies and you want to capture and test the variations. Common use cases include variations for operating system, R version, or version of key dependency. Variants are an advanced feature. When you use them, you'll need to carefully think about your testing strategy to ensure that all important variants are covered by automated tests, and ensure that you have a way to get snapshot changes out of your CI system and back into the repo.</p> </td></tr> <tr valign="top"><td><code>cnd_class</code></td> <td> <p>Whether to include the class of messages, warnings, and errors in the snapshot. Only the most specific class is included, i.e. the first element of <code>class(cnd)</code>.</p> </td></tr> </table> <h3>Workflow</h3> <p>The first time that you run a snapshot expectation it will run <code>x</code>, capture the results, and record them in <code style="white-space: pre;">tests/testthat/_snaps/{test}.md</code>. Each test file gets its own snapshot file, e.g. <code>test-foo.R</code> will get <code style="white-space: pre;">_snaps/foo.md</code>. </p> <p>It's important to review the Markdown files and commit them to git. They are designed to be human readable, and you should always review new additions to ensure that the salient information has been captured. They should also be carefully reviewed in pull requests, to make sure that snapshots have updated in the expected way. </p> <p>On subsequent runs, the result of <code>x</code> will be compared to the value stored on disk. If it's different, the expectation will fail, and a new file <code style="white-space: pre;">_snaps/{test}.new.md</code> will be created. If the change was deliberate, you can approve the change with <code><a href="snapshot_accept.html">snapshot_accept()</a></code> and then the tests will pass the next time you run them. </p> <p>Note that snapshotting can only work when executing a complete test file (with <code><a href="test_file.html">test_file()</a></code>, <code><a href="test_dir.html">test_dir()</a></code>, or friends) because there's otherwise no way to figure out the snapshot path. If you run snapshot tests interactively, they'll just display the current value. </p> <hr /><div style="text-align: center;">[Package <em>testthat</em> version 3.1.5 <a href="00Index.html">Index</a>]</div> </body></html>