run\_test\_dir runs all test files in a directory.
test_all is a convenience function for package development, that
wraps run_test_dir. By default, it runs all files starting with
test in ./inst/tinytest/. It is assumed that all functions to
be tested are loaded.
Usage
run_test_dir(
dir = "inst/tinytest",
pattern = "^test.*\\.[rR]$",
at_home = TRUE,
verbose = getOption("tt.verbose", 2),
color = getOption("tt.pr.color", TRUE),
remove_side_effects = TRUE,
cluster = NULL,
lc_collate = getOption("tt.collate", NA),
...
)
test_all(pkgdir = "./", testdir = "inst/tinytest", ...)Arguments
- dir
[character]path to directory- pattern
[character]A regular expression that is used to find scripts indircontaining tests (by default.Ror.rfiles starting withtest).- at_home
[logical]toggle local tests.- verbose
[logical]toggle verbosity during execution- color
[logical]toggle colorize output- remove_side_effects
[logical]toggle remove user-defined side effects. Environment variables (Sys.setenv()) and options (options()) defined in a test file are reset before running the next test file (see details).- cluster
A
makeClusterobject.- lc_collate
[character]Locale setting used to sort the test files into the order of execution. The defaultNAensures current locale is used. Set this e.g. to"C"to ensure bytewise and more platform-independent sorting (see details).- ...
Arguments passed to
run_test_file- pkgdir
[character]scalar. Root directory of the package (i.e. direcory whereDESCRIPTIONandNAMESPACEreside).- testdir
[character]scalar. Subdirectory where test files are stored.
Details
We cannot guarantee that files will be run in any particular order accross
all platforms, as it depends on the available collation charts (a chart that
determines how alphabets are sorted). For this reason it is a good idea to
create test files that run independent of each other so their order of
execution does not matter. In tinytest, test files cannot share variables.
The default behavior of test runners further discourages interdependence by
resetting environment variables and options that are set in a test file
after the file is executed. If an environment variable needs to survive a
single file, use base::Sys.setenv() explicitly. Similarly, if an
option setting needs to survive, use base::options()
Parallel tests
If inherits(cluster, "cluster") the tests are paralellized over a
cluster of worker nodes. tinytest will be loaded onto each cluster
node. All other preparation, including loading code from the tested package,
must be done by the user. It is also up to the user to clean up the cluster
after running tests. See the 'using tinytest' vignette for examples:
vignette("using_tinytest").
See also
makeCluster,
clusterEvalQ, clusterExport
Other test-files:
build_install_test(),
exit_file(),
run_test_file(),
summary.tinytests(),
test_package()
Examples
# create a test file in tempdir
tests <- "
addOne <- function(x) x + 2
expect_true(addOne(0) > 0)
expect_equal(2, addOne(1))
"
testfile <- tempfile(pattern="test_", fileext=".R")
write(tests, testfile)
# extract testdir
testdir <- dirname(testfile)
# run all files starting with 'test' in testdir
out <- run_test_dir(testdir)
#>
test_35396055711e62.R......... 0 tests
test_35396055711e62.R......... 1 tests OK
test_35396055711e62.R......... 2 tests 1 fails 25ms
print(out)
#> ----- FAILED[data]: test_35396055711e62.R<5--5>
#> call| expect_equal(2, addOne(1))
#> diff| Expected '3', got '2'
#>
#> Showing 1 out of 2 results: 1 fails, 1 passes (26ms)
dat <- as.data.frame(out)