discover(start_dir, pattern='test*.py', top_level_dir=None)
Find all the test modules by recursing into subdirectories from the specified start directory, and return a TestSuite object containing them. Only test files that match pattern will be loaded. (Using shell style pattern matching.) Only module names that are importable (i.e. are valid Python identifiers) will be loaded.
All test modules must be importable from the top level of the project. If the start directory is not the top level directory then the top level directory must be specified separately.
If importing a module fails, for example due to a syntax error, then this will be recorded as a single error and discovery will continue. If the import failure is due to SkipTest
being raised, it will be recorded as a skip instead of an error.
If a package (a directory containing a file named __init__.py
) is found, the package will be checked for a load_tests
function. If this exists then it will be called package.load_tests(loader, tests, pattern)
. Test discovery takes care to ensure that a package is only checked for tests once during an invocation, even if the load_tests function itself calls loader.discover
.
If load_tests
exists then discovery does not recurse into the package, load_tests
is responsible for loading all tests in the package.
The pattern is deliberately not stored as a loader attribute so that packages can continue discovery themselves. top_level_dir is stored so load_tests
does not need to pass this argument in to loader.discover()
.
start_dir can be a dotted module name as well as a directory.
New in version 3.2.
Changed in version 3.4: Modules that raise SkipTest
on import are recorded as skips, not errors. Discovery works for namespace packages. Paths are sorted before being imported so that execution order is the same even if the underlying file system’s ordering is not dependent on file name.
Changed in version 3.5: Found packages are now checked for load_tests
regardless of whether their path matches pattern, because it is impossible for a package name to match the default pattern.
Please login to continue.