Check if testing infrastructure is present and refers to the current package, and if function files have a corresponding test file and vice versa.
Usage
check_tests(
path = getwd(),
pattern = "^test_|^test-",
ignore_case = TRUE,
test_vignettes = FALSE
)Arguments
- path
character string containing a valid path to the folder with the package of which the testing infrastructure and test files should be checked. The default is the working directory.
- pattern
character string containing a regular expression used to select test files.
- ignore_case
TRUEorFALSE: ignore case when matching filenames topattern?- test_vignettes
TRUEorFALSE: should vignettes have a corresponding test file?
Value
If all is fine: character(0). Otherwise, a list containing components
missing and extraneous containing character vectors with the names of
function files for which no used test file is present, and the names of test
files for which no function file is present, respectively.
Details
This function looks for R files starting with pattern pattern in folders
inst/tinytest and tests/testthat in the directory indicated by path,
which is where tinytest
and testthat place their
test files, respectively.
It is checked that testing infrastructure referring to the current package is present. Test files are ignored if the corresponding test infrastructure is missing or wrong, with a warning.
It is checked that all test files have a corresponding .R function file in
folder R or an .Rmd vignette in folder vignettes. It is also checked
that all function files and, if test_vignettes is TRUE, all vignettes,
have a corresponding test file. Function files of re-exported functions are
ignored when looking for corresponding test files because those functions
should be tested in the package from which they are re-exported.
Warnings are issued if:
none of the test directories exist
none of the test directories contain test files
test directories contain files that are ignored because their names do not start with pattern
pattern, are not R files, or are template files created by tinytest or testthattest directories used by tinytest or testthat contain test files while their test architecture is not set up
test directories contain test files without corresponding function file in folder
Ror vignette in foldervignettesfolder
Rcontains function files without corresponding test filesfolder
Rcontains files that are not R filesfolder
Rdoes not contain any function filesfolder
vignettescontains files that are not.Rmdfilesfolder
vignettescontains.Rmdfiles without corresponding test files whiletest_vignettesisTRUEfiles
tests\tinytest.Rortests\testthat.Rdetermining which test infrastructure is used are present but do not refer to the current package, or the packages tinytest and testthat are missing from the dependencies of the tested packagefile
tests\tinytest.Rnortests\testthat.Ris presentfile
man\reexports.Rdis present but no re-exported functions are present in folderR
See also
Section
Set up testing infrastructure
in vignette Package setup and section
Add tests
in vignette Package development: vignette("pkg_setup", package = "develcoder")
and vignette("pkg_devel", package = "develcoder").
Other functions to check tests:
check_tests(), diagnose_test_files(), diagnose_test_infra()
Examples
path_develcoder <- find.package("develcoder")
# test files are present
check_tests(path = path_develcoder) # character(0)
#> Warning: No file determining the used testing infrastructure exists:
#> '/home/runner/work/_temp/Library/develcoder/tests/tinytest.R',
#> '/home/runner/work/_temp/Library/develcoder/tests/testthat.R'
#> Warning: None of the test directories exist:
#> '/home/runner/work/_temp/Library/develcoder/inst/tinytest'
#> '/home/runner/work/_temp/Library/develcoder/tests/testthat'
#> Warning: Ignoring non-R files in folder '/home/runner/work/_temp/Library/develcoder/R':
#> 'develcoder',
#> 'develcoder.rdb',
#> 'develcoder.rdx'
#> Warning: No function files found in folder '/home/runner/work/_temp/Library/develcoder/R'.
#> $missing
#> character(0)
#>
#> $extraneous
#> character(0)
#>
if(requireNamespace("withr", quietly = TRUE)) {
# warnings: test infrastructure, directories,
# and function files not present
tempdir_example <- progutils::create_tempdir(
prefix = "check_tests")
withr::with_dir(new = tempdir_example, {
# Set up a package without testing infrastructure
desc <- desc::description$new("!new")
desc$write(file = fs::path(tempdir_example, "DESCRIPTION"))
check_tests(path = tempdir_example)
})
# Remove temporary directory
unlink(tempdir_example, recursive = TRUE)
}
#> Warning: No file determining the used testing infrastructure exists:
#> '/tmp/Rtmp5R1vke/check_tests1a2465bdcf37/tests/tinytest.R',
#> '/tmp/Rtmp5R1vke/check_tests1a2465bdcf37/tests/testthat.R'
#> Warning: None of the test directories exist:
#> '/tmp/Rtmp5R1vke/check_tests1a2465bdcf37/inst/tinytest'
#> '/tmp/Rtmp5R1vke/check_tests1a2465bdcf37/tests/testthat'
#> Warning: No function files found in folder '/tmp/Rtmp5R1vke/check_tests1a2465bdcf37/R'.