"contents": "\nThis is for vdiffr version 1.0.2.9000. The package is in development so syntax and behavior is likely to change. Read the testing chapter in the R packages book for background on writing tests for R packages.\nTests to compare two images\nhttps://vdiffr.r-lib.org/ is a package that integrates with the testthat package to allow tests that compare plot outputs.\nSetting up your package for unit testing\nusethis::use_testthat()\nMakes the tests folder and testthat subfolder.\nWithin the testthat folder,\nusethis::use_test(name=\"xyz\")\nThis will make a test file test-xyz.R in the testthis subfolder. When you make your own test files, replace “xyz” with your test name.\nTo run the tests, you can open test-xyz.R in RStudio and it will recognize that this is a test file. “Run tests” will show up on the upper right of the file.\nsnapshot workflow\nThe first time that you run expect_doppelganger(\"xyz\", plt1) within a test_that() call, a snapshot expectation will run and a svg of plt1 will be made in tests/testthat/_snaps/xyz called xyz.svg. The next time expect_doppelganger(\"xyz\", plt2) is run within a test_that() call, a svg of plt2 will be made and compared to xyz.svg. If it is different, the file xyz.new.svg will be made in tests/testthat/_snaps/xyz.\nBecause of this workflow, we want to make sure tests/testthat/_snaps/xyz is empty before we start because we have to run expect_doppelganger(\"xyz\", plt1) to create xyz.svg but if xyz.svg is already there from previous tests, we might end up creating xyz.new.svg if plt1 is a different plot than xyz.svg.\nThe full test-xyz.R test file is below. Here I break down the parts of that file.\nHeader\nGive some info about the tests (context), load packages, and then clean up the _snaps folder. Having old svgs there can mess up your tests.\ncontext(\"xyz\")\nlibrary(ggplot2)\nlibrary(vdiffr)\n\n# Clean up the _snaps folder for this test file\n# Note this won't be necessary in some situations\nfils <- dir(file.path(here::here(), \"tests\", \"testthat\", \"_snaps\", \"xyz\"), full.names = TRUE)\nfile.remove(fils)\nCompare two ggplots plot\nHere is the contents of test-xyz.R to compare two plots. This test should fail because the plots are different.\nStep 1 make first plot and run expect_doppelganger() to create a svg file in _snaps that is used to compare against. The name of the file is the first argument and must be the same in the expect_doppelganger() calls.\nplt <- ggplot(mtcars, aes(mpg)) + geom_histogram()\n# Save as ggplot-test1.svg in _snaps folder\n# This will appear as a successful test, i.e. plot successfully created\ntest_that(\"setup\", {\n expect_doppelganger(\"ggplot-test1\", plt)\n})\nStep 2. Create 2nd plot and compare to ggplot-test1.svg already in _snaps folder.\nplt <- ggplot(mtcars, aes(disp)) + geom_histogram()\ntest_that(\"plots are different\", {\n expect_doppelganger(\"ggplot-test1\", plt)\n})\nSince they are different, you will see ggplot-test1.new.svg in the _snaps folder.\nCompare two base plots\nThe syntax here is a little different. The object that you pass into expect_doppelganger() as the second argument is a function that creates the base plot. Otherwise the steps are the same.\nplt <- function(){ hist(mtcars$mpg) }\n# Step 1. Create base-test.svg\n# This will appear as a successful test, i.e. plot successfully created\ntest_that(\"setup\", {\n expect_doppelganger(\"base-test\", plt)\n})\n# Step 2. Test new plot against base-test.svg\n# Test will fail since they are different\nplt <- function(){ hist(mtcars$disp) }\ntest_that(\"plots are different\", {\n expect_doppelganger(\"base-test\", plt)\n})\nFinal test-xyz.R file\nThis should be in the testthat folder in tests folder. Running this will show 2 Fails and 4 Passes. To run, you can open the file in RStudio and look for the “Run Tests” button in top right of file. Or run this code.\nfil <- file.path(here::here(), \"tests\", \"testthat\", \"test-xyz.R\")\ntestthat::test_file(fil)\nOr open the file in RStudio and run this code:\ndevtools::test_active_file()\nThe full test file.\ncontext(\"xyz\")\nlibrary(ggplot2)\nlibrary(vdiffr)\n\n# Clean up the _snaps folder for this test file\n# Note this won't be necessary in some situations\nfils <- dir(file.path(here::here(), \"tests\", \"testthat\", \"_snaps\", \"xyz\"), full.names = TRUE)\nfile.remove(fils)\n\n# This test should fail. The plots are different\n# Step 1 make first plot\nplt <- ggplot(mtcars, aes(mpg)) + geom_histogram()\n# Save as ggplot-test1.svg in _snaps folder\n# This will appear as a successful test, i.e. plot successfully created\ntest_that(\"setup\", {\n expect_doppelganger(\"ggplot-test1\", plt)\n})\n# Step 2. Create 2nd plot and compare to ggplot-test1.svg in _snaps folder\nplt <- ggplot(mtcars, aes(disp)) + geom_histogram()\ntest_that(\"plots are different\", {\n expect_doppelganger(\"ggplot-test1\", plt)\n})\n# Since they are different, you will see ggplot-test1.new.svg in the _snaps folder\n\n# This test should not fail. The plots should be the same.\nfun <- function(dat){ return(ggplot(dat, aes(mpg)) + geom_histogram()) }\nplt <- fun(mtcars)\n# Step 1. Create ggplot-test2.svg\n# This will appear as a successful test, i.e. plot successfully created\ntest_that(\"setup\", {\n expect_doppelganger(\"ggplot-test2\", plt)\n})\n# Step 2. Test new plot against ggplot-test2.svg\nplt <- fun(na.omit(mtcars))\ntest_that(\"plots are the same\", {\n expect_doppelganger(\"ggplot-test2\", plt)\n})\n# Since they are the same, you will NOT see ggplot-test2.new.svg in the _snaps folder\n\n# Comparing plots made with base graphics; Create a function that makes the plot\nplt <- function(){ hist(mtcars$mpg) }\n# Step 1. Create base-test.svg\n# This will appear as a successful test, i.e. plot successfully created\ntest_that(\"setup\", {\n expect_doppelganger(\"base-test\", plt)\n})\n# Step 2. Test new plot against base-test.svg\n# Test will fail since they are different\nplt <- function(){ hist(mtcars$disp) }\ntest_that(\"plots are different\", {\n expect_doppelganger(\"base-test\", plt)\n})\n\n\n\n",
0 commit comments