When developing R packages, it’s good practice to build a good set of unit tests that will notify you when something breaks in the future. For this, the testthat
package is commonly used. Often though, we end up writing a similar set of tests again and again, which can be both tedious and time-consuming.
Main functions
Generator functions
These functions are used to generate expectations (gxs).
gxs_selection() |
Generates testthat::expect_* statements from a selection (string of code) |
gxs_function() |
Generates testthat::expect_* statements for combinations of supplied argument values |
Functions for use in tests
strip() |
Strips strings of non-alphanumeric characters |
strip_msg() |
Strips side-effect messages of non-alphanumeric characters and rethrows them |
suppress_mw() |
Suppresses warnings and messages |
capture_side_effects() |
Captures errors, warnings, and messages from an expression |
smpl() |
Samples a vector, factor or data frame with internal random seed |
simplified_formals() |
Formats formals as easily testable character vector |
element_lengths() , element_types() , element_classes() |
Gets the length/type/class of each element |
num_total_elements() |
Unlists recursively and finds the total number of elements |
set_test_seed() |
Set random seed for unit tests compatible with R < 3.6.0 |
Helper functions
prepare_insertion() |
Collapses a vector of expectation strings and adds indentation |
capture_parse_eval_side_effects() |
Wraps string in capture_side_effects() before parsing and evaluating it |
stop_if() , warn_if() , message_if() |
If TRUE , generate error/warning/message with the supplied message |