|
1 | 1 | #' Does a figure look like its expected output? |
2 | 2 | #' |
3 | | -#' If the test has never been validated yet, the test is skipped. If |
4 | | -#' the test has previously been validated but \code{fig} does not look |
5 | | -#' like its expected output, an error is issued. Use |
6 | | -#' [validate_cases()] or [manage_cases()] to (re)validate |
7 | | -#' the test. |
| 3 | +#' @description |
8 | 4 | #' |
9 | | -#' `fig` can be a ggplot object, a recordedplot, a function to be |
10 | | -#' called, or more generally any object with a `print` method. |
| 5 | +#' `expect_doppelganger()` takes a figure to check visually. |
| 6 | +#' |
| 7 | +#' * If the figure has yet to be validated, the test is skipped. Call |
| 8 | +#' [manage_cases()] to validate the new figure, so vdiffr knows what |
| 9 | +#' to compare against. |
| 10 | +#' |
| 11 | +#' * If the test has been validated, `fig` is compared to the |
| 12 | +#' validated figure. If the plot differs, a failure is issued |
| 13 | +#' (except on CRAN, see section on regression testing below). |
| 14 | +#' |
| 15 | +#' Either fix the problem, or call [manage_cases()] to validate the |
| 16 | +#' new figure appearance. |
11 | 17 | #' |
12 | 18 | #' @param title A brief description of what is being tested in the |
13 | 19 | #' figure. For instance: "Points and lines overlap". |
|
17 | 23 | #' |
18 | 24 | #' The title is also used as file name for storing SVG (in a |
19 | 25 | #' sanitzed form, with special characters converted to `"-"`). |
20 | | -#' @param fig A figure to test. |
| 26 | +#' @param fig A figure to test. This can be a ggplot object, a |
| 27 | +#' recordedplot, or more generally any object with a `print` method. |
| 28 | +#' |
| 29 | +#' For plots that can't be represented as printable objects, you can |
| 30 | +#' pass a function. This function must construct the plot and print |
| 31 | +#' it. |
21 | 32 | #' @param path The path where the test case should be stored, relative |
22 | 33 | #' to the `tests/figs/` folder. If `NULL` (the default), the current |
23 | 34 | #' testthat context is used to create a subfolder. Supply an empty |
|
31 | 42 | #' in a deterministic way and write it to the target file. See |
32 | 43 | #' [write_svg()] (the default) for an example. |
33 | 44 | #' |
| 45 | +#' @section Regression testing versus Unit testing: |
| 46 | +#' |
| 47 | +#' Failures to match a validated appearance are only reported when the |
| 48 | +#' tests are run locally, on Travis, Appveyor, or any environment |
| 49 | +#' where the `Sys.getenv("CI")` or `Sys.getenv("NOT_CRAN")` variables |
| 50 | +#' are set. Because vdiffr is more of a monitoring than a unit testing |
| 51 | +#' tool, it shouldn't cause R CMD check failures on the CRAN machines. |
| 52 | +#' |
| 53 | +#' Checking the appearance of a figure is inherently fragile. It is |
| 54 | +#' similar to testing for errors by matching exact error messages: |
| 55 | +#' these messages are susceptible to change at any time. Similarly, |
| 56 | +#' the appearance of plots depends on a lot of upstream code, such as |
| 57 | +#' the way margins and spacing are computed. vdiffr uses a special |
| 58 | +#' ggplot2 theme that should change very rarely, but there are just |
| 59 | +#' too many upstream factors that could cause breakages. For this |
| 60 | +#' reason, figure mismatches are not necessarily representative of |
| 61 | +#' actual failures. |
| 62 | +#' |
| 63 | +#' Visual testing is not an alternative to writing unit tests for the |
| 64 | +#' internal data transformations performed during the creation of your |
| 65 | +#' figure. It is more of a monitoring tool that allows you to quickly |
| 66 | +#' check how the appearance of your figures changes over time, and to |
| 67 | +#' manually assess whether changes reflect actual problems in your |
| 68 | +#' package. |
| 69 | +#' |
34 | 70 | #' @section Debugging: |
35 | 71 | #' |
36 | 72 | #' It is sometimes difficult to understand the cause of a failure. |
|
0 commit comments