A pytest plugin for filtering unittest-based test classes
unittest-based tests support out of the box. It eases incremental transition from legacy test suite to
pytest for existing projects, but it has one drawback: there is no way to exclude some test cases (
unittest.TestCase subclasses) from collection.
python_classes config option, but, as noted in documentation, this option doesn't affect
Note that unittest.TestCase derived classes are always collected regardless of this option, as unittest‘s own collection framework is used to collect those tests.
It's probably OK for projects that uses builtin
unittest test runner (
python -m unittest) because the latter has no any include/exclude option either, but it can be inconvenient if a project test suite is used with
nose test runner and relies on hard-coded leading underscore filter.
The plugin provides two new config options:
This option works like the
python_classes option mentioned above but for
One or more name prefixes or glob-style patterns determining which classes are considered for test collection. Search for multiple glob patterns by adding a space between patterns.
A default value is none (no value), i.e. all
unittest.TestCase subclasses are collected by default.
to include only classes with names starting with
[pytest] python_unittest_classes = Test* Check*
[pytest] python_unittest_classes = Test Check
to include only classes with names ending with
[pytest] python_unittest_classes = *TestCase
to exclude classes with names starting with underscore:
[pytest] python_unittest_classes = [!_]*
This boolean option can be used to mimic hard-coded nose selector behaviour. If the option set to
true, all classes with names that start with
_ will be excluded. Thus, the last example above can be rewritten as following:
[pytest] python_unittest_exclude_underscore = true
A default value is
Both options can be combined. The following two examples are equivalent:
[pytest] python_unittest_classes = [!_]*Test [!_]*Check
[pytest] python_unittest_classes = *Test *Check python_unittest_exclude_underscore = true
- Fixed bug where
python_unittest_exclude_underscorewas applied to any collected item, not only unittest classes
- Initial release