diff --git a/changelog/3711.feature.rst b/changelog/3711.feature.rst index ba3322dfb..1503bac60 100644 --- a/changelog/3711.feature.rst +++ b/changelog/3711.feature.rst @@ -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. diff --git a/doc/en/example/pythoncollection.rst b/doc/en/example/pythoncollection.rst index 15eb63187..750bc58d8 100644 --- a/doc/en/example/pythoncollection.rst +++ b/doc/en/example/pythoncollection.rst @@ -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"] diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 1d0c48c27..9d4a9f962 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -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]) diff --git a/testing/test_collection.py b/testing/test_collection.py index 36e8a69ce..8ed5caa3a 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -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( """