test_pkg: Run the tests of a package in its namespace

View source: R/testit.R

test_pkgR Documentation

Run the tests of a package in its namespace

Description

The tests are executed in a clean environment with the namespace of the package to be tested as the parent environment, which means you can use non-exported objects in the package without having to resort to the triple colon ::: trick.

Usage

test_pkg(package = pkg_name(), dir = c("testit", "tests/testit"), update = NA)

Arguments

package

The package name. By default, it is detected from the ‘DESCRIPTION’ file if exists.

dir

The directory of the test files; by default, it is the directory ‘testit/’ or ‘tests/testit/’ under the current working directory, whichever exists. You can also specify a custom directory.

update

If TRUE, update snapshot files with actual output instead of comparing. If NA (the default), update snapshot files only if they are tracked by GIT (so you can view the diffs in GIT and decide whether to accept or discard the changes). If FALSE, never update snapshot files and always compare. For NA and FALSE, if the snapshot test fails, it will throw an error with a message showing the location of the failed test. For TRUE, it will update the snapshot file and never throw an error.

Details

The tests are assumed to be under the ‘testit/’ or ‘tests/testit/’ directory by default (depending on your working directory is the package root directory or the ‘tests/’ directory). The test scripts must be named of the form ‘⁠test-*.R⁠’ (or ‘⁠test-*.md⁠’ for snapshot tests); other files will not be treated as test files (but may also be useful, e.g. you can source() other scripts in tests).

When a test is executed, the working directory is the same as the directory containing this test, and all existing objects in the test environment will be removed before the code is executed.

See https://pkg.yihui.org/testit/#snapshot-testing for more details about snapshot testing.

Value

NULL. All test files are executed, unless an error occurs.

Note

All test scripts must be encoded in UTF-8 if they contain any multibyte characters.

Examples

## Not run: 
test_pkg("testit")

## End(Not run)

testit documentation built on March 6, 2026, 9:06 a.m.

Related to test_pkg in testit...