From fc5d4654e5f6ec11eac5f5fcd5e4ba840414925a Mon Sep 17 00:00:00 2001 From: Christian Fetzer Date: Tue, 5 Feb 2019 17:19:24 +0100 Subject: [PATCH] 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 --- AUTHORS | 1 + changelog/3711.feature.rst | 1 + doc/en/example/pythoncollection.rst | 3 +++ src/_pytest/main.py | 18 ++++++++++++++++++ testing/test_session.py | 15 +++++++++++++++ 5 files changed, 38 insertions(+) create mode 100644 changelog/3711.feature.rst diff --git a/AUTHORS b/AUTHORS index 35e07dcb4..737c07b1b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -50,6 +50,7 @@ Charles Cloud Charnjit SiNGH (CCSJ) Chris Lamb Christian Boelsen +Christian Fetzer Christian Theunert Christian Tismer Christopher Gilling diff --git a/changelog/3711.feature.rst b/changelog/3711.feature.rst new file mode 100644 index 000000000..ba3322dfb --- /dev/null +++ b/changelog/3711.feature.rst @@ -0,0 +1 @@ +Add the ``--ignore-glob`` parameter to exclude test-modules with Unix shell-style wildcards. diff --git a/doc/en/example/pythoncollection.rst b/doc/en/example/pythoncollection.rst index 8dcaa97d7..15eb63187 100644 --- a/doc/en/example/pythoncollection.rst +++ b/doc/en/example/pythoncollection.rst @@ -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 ------------------------------------- diff --git a/src/_pytest/main.py b/src/_pytest/main.py index d0d826bb6..1d0c48c27 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -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 diff --git a/testing/test_session.py b/testing/test_session.py index d64a1a519..6b185f76b 100644 --- a/testing/test_session.py +++ b/testing/test_session.py @@ -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="""