Changing standard (Python) test discovery¶
Ignore paths during test collection¶
You can easily ignore certain test directories and modules during collection by passing the --ignore=path option on the cli. pytest allows multiple --ignore options. Example:
Now if you invoke pytest with --ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/, you will see that pytest only collects test-modules, which do not match the patterns specified:
The --ignore-glob option allows to ignore test file paths based on Unix shell-style wildcards. If you want to exclude test-modules that end with _01.py, execute pytest with --ignore-glob='*_01.py'.
Deselect tests during test collection¶
Tests can individually be deselected during collection by passing the --deselect=item option. For example, say tests/foobar/test_foobar_01.py contains test_a and test_b. You can run all of the tests within tests/ except for tests/foobar/test_foobar_01.py::test_a by invoking pytest with --deselect=tests/foobar/test_foobar_01.py::test_a. pytest allows multiple --deselect options.
Keeping duplicate paths specified from command line¶
Default behavior of pytest is to ignore duplicate paths specified from the command line. Example:
Just collect tests once.
To collect duplicate tests, use the --keep-duplicates option on the cli. Example:
Changing directory recursion¶
You can set the norecursedirs option in a configuration file:
This would tell pytest to not recurse into typical subversion or sphinx-build directories or into any tmp prefixed directory.
Changing naming conventions¶
You can configure different naming conventions by setting the python_files, python_classes and python_functions in your configuration file. Here is an example:
This would make pytest look for tests in files that match the check_* .py glob-pattern, Check prefixes in classes, and functions and methods that match *_check. For example, if we have:
The test collection would look like this:
You can check for multiple glob patterns by adding a space between the patterns:
Note
the python_functions and python_classes options have no effect for unittest.TestCase test discovery because pytest delegates discovery of test case methods to unittest code.
Interpreting cmdline arguments as Python packages¶
You can use the --pyargs option to make pytest try interpreting arguments as python package names, deriving their file system path and then running the test. For example if you have unittest2 installed you can type:
which would run the respective test module. Like with other options, through a configuration file and the addopts option you can make this change more permanently:
Now a simple invocation of pytest NAME will check if NAME exists as an importable package/module and otherwise treat it as a filesystem path.
Finding out what is collected¶
You can always peek at the collection tree without running tests like this:
Customizing test collection¶
You can easily instruct pytest to discover tests from every Python file:
However, many projects will have a setup.py which they don’t want to be imported. Moreover, there may be files only importable by a specific python version. For such cases you can dynamically define files to be ignored by listing them in a conftest.py file:
and then if you have a module file like this:
and a setup.py dummy file like this:
If you run with a Python 2 interpreter then you will find the one test and will leave out the setup.py file:
If you run with a Python 3 interpreter both the one test and the setup.py file will be left out:
It’s also possible to ignore files based on Unix shell-style wildcards by adding patterns to collect_ignore_glob.
The following example conftest.py ignores the file setup.py and in addition all files that end with *_py2.py when executed with a Python 3 interpreter:
Since Pytest 2.6, users can prevent pytest from discovering classes that start with Test by setting a boolean __test__ attribute to False.
Note
If you are working with abstract test classes and want to avoid manually setting the __test__ attribute for subclasses, you can use a mixin class to handle this automatically. For example:
This approach ensures that subclasses of abstract test classes are automatically collected without needing to explicitly set the __test__ attribute.