API Reference
- pytest_discover_tests(NAME ARGV)
Automatically create CTest tests for Python tests collected with Pytest within a controlled environment:
pytest_discover_tests(NAME [TEST_PATHS path1 path2...] [WORKING_DIRECTORY dir] [TRIM_FROM_NAME pattern] [TRIM_FROM_FULL_NAME pattern] [LIBRARY_PATH_PREPEND path1 path2...] [PYTHON_PATH_PREPEND path1 path2...] [ENVIRONMENT env1 env2...] [PROPERTIES prop1 prop2...] [DEPENDS target1 target2...] [INCLUDE_FILE_PATH] [STRIP_PARAM_BRACKETS] [BUNDLE_TESTS] [EXTRA_ARGS arg1 arg2...] [DISCOVERY_EXTRA_ARGS arg1 arg2...] )
The options are:
NAMEIndicate the name of the target that will be created. It will also be used as a prefix for each test created, or as an identifier the bundled test.
TEST_PATHSSpecifies a list of files or directories to search when executing Pytest from the current source directory (or from the
WORKING_DIRECTORYvalue if provided):pytest_discover_tests( ... TEST_PATHS path1 path2/test.py )
WORKING_DIRECTORYSpecify the directory in which to run the Pytest command. If this option is not provided, the current source directory is used.
INCLUDE_FILE_PATHInclude the file path of each collected Pytest test into its generated test name. This helps distinguish tests with the same class or function names by including their source file for clearer identification:
pytest_discover_tests( ... INCLUDE_FILE_PATH )
TRIM_FROM_NAMESpecify a regular expression to trim part of each class, method and function name discovered before creating the test. This option can be used to trim the convention prefix required by Pytest for discovery:
pytest_discover_tests( ... TRIM_FROM_NAME "^(Test|test_)" )
TRIM_FROM_FULL_NAMESpecify a regular expression to trim parts of the full test name generated:
pytest_discover_tests( ... TRIM_FROM_FULL_NAME "(Test|test_)" )
STRIP_PARAM_BRACKETSRemove square brackets added to the test identifier when using parametrized tests:
pytest_discover_tests( ... STRIP_PARAM_BRACKETS )
Tests identifiers will be reformatted as follows:
test.foo[bar] becomes test.foo.bar
test.foo[1-3] becomes test.foo.1-3
test.foo[bar[nested]] becomes test.foo.bar[nested]
LIBRARY_PATH_PREPENDList of library paths to prepend to the corresponding environment variable (
LD_LIBRARY_PATHon Linux,DYLD_LIBRARY_PATHon macOS, andPATHon Windows) when running the tests. Each path can be defined literally or as a CMake expression generator for convenience:pytest_discover_tests( ... LIBRARY_PATH_PREPEND $<TARGET_FILE_DIR:lib1> $<TARGET_FILE_DIR:lib2> /path/to/libs/ )
PYTHON_PATH_PREPENDList of Python paths to prepend to the
PYTHONPATHenvironment variable when running the tests. Each path can be defined literally or as a CMake expression generator for convenience:pytest_discover_tests( ... PYTHON_PATH_PREPEND $<TARGET_FILE_DIR:lib1> $<TARGET_FILE_DIR:lib2> /path/to/python/ )
ENVIRONMENTList of custom environment variables with associated values to set when running the tests:
pytest_discover_tests( ... ENVIRONMENT "ENV_VAR1=VALUE1" "ENV_VAR2=VALUE2" "ENV_VAR3=VALUE3" )
PROPERTIESList of custom test properties to apply for all generated tests:
pytest_discover_tests( ... PROPERTIES LABELS "python;unit" TIMEOUT 120 )
DEPENDSList of dependent targets that need to be executed before running the tests:
pytest_discover_tests( ... DEPENDS lib1 lib2 )
The Python files containing Pytest tests can also be explicitly set as dependencies, ensuring that the target rebuilds whenever these files are modified:
pytest_discover_tests( ... DEPENDS /path/to/test_foo.py /path/to/test_bar.py )
For convenience, you can use the GLOB_RECURSE command to dynamically gather all test files:
file(GLOB_RECURSE test_files "/path/to/tests/*.py") pytest_discover_tests( ... DEPENDS ${test_files} )
BUNDLE_TESTSIndicate whether Python tests should be bundled under a single CTest test. This option can also be set dynamically by using the
BUNDLE_PYTHON_TESTSenvironment variable.Bundled tests generally run faster because Pytest can use caching and fixtures with a broader scope.
EXTRA_ARGSList of extra arguments to pass on the Pytest command line for each test case:
pytest_discover_tests( ... EXTRA_ARGS "--capture=no" "--cmdopt=demo" )
See also
DISCOVERY_EXTRA_ARGSList of extra arguments to pass on the Pytest command line for the test discovery:
pytest_discover_tests( ... DISCOVERY_EXTRA_ARGS "-k=expression" )
See also
Warning
This option may affect test discovery. Some arguments can change output format and break the test collection logic. Use with caution!
Note
This function works similarly to the gtest_discover_tests function, which creates CTest tests for each GTest tests discovered within a single C++ executable test file.