Add ability to exclude files matching glob patterns in conftest.py

This adds the `collect_ignore_glob` option for `conftest.py` to allow
Unix-style wildcards for excluding files.
This commit is contained in:
Christian Fetzer 2019-02-06 10:50:46 +01:00
parent fc5d4654e5
commit 2dc2a19db5
4 changed files with 39 additions and 1 deletions

View File

@ -1 +1,2 @@
Add the ``--ignore-glob`` parameter to exclude test-modules with Unix shell-style wildcards.
Add the ``collect_ignore_glob`` for ``conftest.py`` to exclude test-modules with Unix shell-style wildcards.

View File

@ -269,3 +269,17 @@ file will be left out:
collected 0 items
======================= no tests ran in 0.12 seconds =======================
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::
# content of conftest.py
import sys
collect_ignore = ["setup.py"]
if sys.version_info[0] > 2:
collect_ignore_glob = ["*_py2.py"]

View File

@ -303,7 +303,10 @@ def pytest_ignore_collect(path, config):
if py.path.local(path) in ignore_paths:
return True
ignore_globs = []
ignore_globs = config._getconftest_pathlist(
"collect_ignore_glob", path=path.dirpath()
)
ignore_globs = ignore_globs or []
excludeglobopt = config.getoption("ignore_glob")
if excludeglobopt:
ignore_globs.extend([py.path.local(x) for x in excludeglobopt])

View File

@ -374,6 +374,26 @@ class TestCustomConftests(object):
assert result.ret == 0
assert "passed" in result.stdout.str()
def test_collectignoreglob_exclude_on_option(self, testdir):
testdir.makeconftest(
"""
collect_ignore_glob = ['*w*l[dt]*']
def pytest_addoption(parser):
parser.addoption("--XX", action="store_true", default=False)
def pytest_configure(config):
if config.getvalue("XX"):
collect_ignore_glob[:] = []
"""
)
testdir.makepyfile(test_world="def test_hello(): pass")
testdir.makepyfile(test_welt="def test_hallo(): pass")
result = testdir.runpytest()
assert result.ret == EXIT_NOTESTSCOLLECTED
result.stdout.fnmatch_lines("*collected 0 items*")
result = testdir.runpytest("--XX")
assert result.ret == 0
result.stdout.fnmatch_lines("*2 passed*")
def test_pytest_fs_collect_hooks_are_seen(self, testdir):
testdir.makeconftest(
"""