Skip to content

Resolving differences in test plot snapshots

Chris Peak edited this page Mar 20, 2023 · 2 revisions

Testing with the vdiffr package

vdiffr is a testing library that creates plots and compares them against previously-stored versions of the same plots, checking for consistency.

Run your tests

Running devtools::test() will execute all the test.that tests in the repository, including the plot snapshot tests. If any differences are found, the before-and-after versions can be viewed later using comparison tools provided by the vdiffr package.

Viewing discrepancies

If any vdiffr snapshot tests fail, execute testthat::snapshot_review() in the RStudio console. This should bring up a window in the "Plots" pane (usually on the bottom-right quadrant of RStudio) showing you a pulsating before-and-after snapshot of your plot, highlighting in red any pixels that have changed. Just above the plot on the right (if your RStudio window is large enough) it should allow you to accept the new plot version (in which case the new plot will be used as the basis for future comparisons) or reject it. If you choose to reject, you can go back to adjust your code to try to get the test to pass the next time around. If more than one snapshot comparison failed, you will automatically be shown the next one as soon as you accept/reject the first one, and so on.

See the vdiffr documentation for more details about its usage.