Helper, setup, and teardown files located in the same directory as the test will also be run.

test_file(path, reporter = default_compact_reporter(), package = NULL, ...)

Arguments

path

Path to file.

reporter

Reporter to use to summarise output. Can be supplied as a string (e.g. "summary") or as an R6 object (e.g. SummaryReporter$new()).

See Reporter for more details and a list of built-in reporters.

package

If these tests belong to a package, the name of the package.

...

Additional parameters passed on to test_dir()

Value

A list (invisibly) containing data about the test results.

Special files

There are two types of .R file that have special behaviour:

  • Test files start with test and are executed in alphabetical order.

  • Setup files start with setup and are executed before tests. If clean up is needed after all tests have been run, you can use withr::defer(clean_up(), teardown_env()). See vignette("test-fixtures") for more details.

There are two other types of special file that we no longer recommend using:

  • Helper files start with helper and are executed before tests are run. They're also loaded by devtools::load_all(), so there's no real point to them and you should just put your helper code in R/.

  • Teardown files start with teardown and are executed after the tests are run. Now we recommend interleave setup and cleanup code in setup- files, making it easier to check that you automatically clean up every mess that you make.

All other files are ignored by testthat.

Environments

Each test is run in a clean environment to keep tests as isolated as possible. For package tests, that environment that inherits from the package's namespace environment, so that tests can access internal functions and objects.

Examples

path <- testthat_example("success")
test_file(path)
#> 
#> ══ Testing test-success.R ══════════════════════════════════════════════════════
#> 
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 0 ]
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]
[ FAIL 0 | WARN 0 | SKIP 1 | PASS 1 ]
[ FAIL 0 | WARN 1 | SKIP 1 | PASS 1 ]
[ FAIL 0 | WARN 1 | SKIP 1 | PASS 2 ]
[ FAIL 0 | WARN 1 | SKIP 1 | PASS 3 ]
[ FAIL 0 | WARN 1 | SKIP 1 | PASS 4 ]
#> 
#> ── Skip (test-success.R:6:3): you can skip tests if needed ─────────────────────
#> Reason: This test hasn't been written yet
#> 
#> ── Warning (test-success.R:10:3): some tests have warnings ─────────────────────
#> NaNs produced
#> Backtrace:
#>  1. testthat::expect_equal(log(-1), NaN) test-success.R:10:2
#>  2. testthat::quasi_label(enquo(object), label, arg = "object")
#>  3. rlang::eval_bare(expr, quo_get_env(quo))
#> 
#> 
[ FAIL 0 | WARN 1 | SKIP 1 | PASS 4 ]
test_file(path, reporter = "minimal")
#> .SW...