Add ability to exclude files matching glob patterns with --ignore-glob

This adds the `--ignore-glob` option to allow Unix-style wildcards so
that `--ignore-glob=integration*` excludes all tests that reside in
files starting with `integration`.

Fixes: #3711
This commit is contained in:
Christian Fetzer 2019-02-05 17:19:24 +01:00
parent 2461a43e00
commit fc5d4654e5
5 changed files with 38 additions and 0 deletions

View File

@ -50,6 +50,7 @@ Charles Cloud
Charnjit SiNGH (CCSJ)
Chris Lamb
Christian Boelsen
Christian Fetzer
Christian Theunert
Christian Tismer
Christopher Gilling

View File

@ -0,0 +1 @@
Add the ``--ignore-glob`` parameter to exclude test-modules with Unix shell-style wildcards.

View File

@ -41,6 +41,9 @@ you will see that ``pytest`` only collects test-modules, which do not match the
========================= 5 passed in 0.02 seconds =========================
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
-------------------------------------

View File

@ -4,6 +4,7 @@ from __future__ import division
from __future__ import print_function
import contextlib
import fnmatch
import functools
import os
import pkgutil
@ -117,6 +118,12 @@ def pytest_addoption(parser):
metavar="path",
help="ignore path during collection (multi-allowed).",
)
group.addoption(
"--ignore-glob",
action="append",
metavar="path",
help="ignore path pattern during collection (multi-allowed).",
)
group.addoption(
"--deselect",
action="append",
@ -296,6 +303,17 @@ def pytest_ignore_collect(path, config):
if py.path.local(path) in ignore_paths:
return True
ignore_globs = []
excludeglobopt = config.getoption("ignore_glob")
if excludeglobopt:
ignore_globs.extend([py.path.local(x) for x in excludeglobopt])
if any(
fnmatch.fnmatch(six.text_type(path), six.text_type(glob))
for glob in ignore_globs
):
return True
allow_in_venv = config.getoption("collect_in_virtualenv")
if not allow_in_venv and _in_venv(path):
return True

View File

@ -253,6 +253,21 @@ def test_exclude(testdir):
result.stdout.fnmatch_lines(["*1 passed*"])
def test_exclude_glob(testdir):
hellodir = testdir.mkdir("hello")
hellodir.join("test_hello.py").write("x y syntaxerror")
hello2dir = testdir.mkdir("hello2")
hello2dir.join("test_hello2.py").write("x y syntaxerror")
hello3dir = testdir.mkdir("hallo3")
hello3dir.join("test_hello3.py").write("x y syntaxerror")
subdir = testdir.mkdir("sub")
subdir.join("test_hello4.py").write("x y syntaxerror")
testdir.makepyfile(test_ok="def test_pass(): pass")
result = testdir.runpytest("--ignore-glob=*h[ea]llo*")
assert result.ret == 0
result.stdout.fnmatch_lines(["*1 passed*"])
def test_deselect(testdir):
testdir.makepyfile(
test_a="""