expect_known_output {testthat} | R Documentation |
For complex printed output and objects, it is often challenging to describe
exactly what you expect to see. expect_known_value()
and
expect_known_output()
provide a slightly weaker guarantee, simply
asserting that the values have no changed since the last time that you ran
them.
expect_known_output(object, file, update = TRUE, ..., info = NULL, label = NULL, print = FALSE, width = 80) expect_known_value(object, file, update = TRUE, ..., info = NULL, label = NULL) expect_known_hash(object, hash = NULL)
object |
object to test |
file |
File path where known value/output will be stored. |
update |
Should the file be updated? Defaults to |
... |
other values passed to |
info |
extra information to be included in the message (useful when writing tests in loops). |
label |
object label. When |
print |
If |
width |
Number of characters per line of output |
hash |
Known hash value. Leave empty and you'll be informed what to use in the test output. |
These expectations should be used in conjunction with git, as otherwise
there is no way to revert to previous values. Git is particularly useful
in conjunction with expect_known_output()
as the diffs will show you
exactly what has changed.
Note that known values updates will only updated when running tests
interactively. R CMD check
clones the package source so any changes to
the reference files will occured a temporary directory, and will not be
synchronised back to the source package.
tmp <- tempfile() # The first run always succeeds expect_known_output(mtcars[1:10, ], tmp, print = TRUE) # Subsequent runs will suceed only if the file is unchanged # This will succeed: expect_known_output(mtcars[1:10, ], tmp, print = TRUE) ## Not run: # This will fail expect_known_output(mtcars[1:9, ], tmp, print = TRUE) ## End(Not run)