Skip to contents

Use these functions to create standardized fixture directories for unit tests. They automatically detect whether code is running inside a test (via testthat::is_testing()) and adjust paths accordingly. This ensures consistent fixture management across both interactive test development and automated test runs.

Usage

path_mocks(...)

path_extra(...)

Arguments

...

Character. Optional subdirectory name within the fixture path. If NULL, returns the base fixture directory. Default is NULL.

Value

Character. Sanitized absolute path to the fixture directory. The directory is created if it doesn't exist.

Details

Both functions create directories if they don't exist and return sanitized paths. The fixture structure follows a convention where:

  • _mocks/ subdirectory stores mock objects, API responses, or stubbed data

  • _extra/ subdirectory stores supplementary test data, fixtures, or resources

When testthat::is_testing() returns TRUE (inside test execution), paths are relative to the test directory. Otherwise, testthat::test_path() is used to resolve the absolute path, supporting interactive test development.

Functions

  • path_mocks(): Get path to mock fixture directory

  • path_extra(): Get path to extra fixture directory

Examples

if (FALSE) { # \dontrun{
path_mocks("some-mock")
path_mocks()
} # }
if (FALSE) { # \dontrun{
path_extra("some-data")
path_extra()
} # }