Skip to content

Commit d320da3

Browse files
committed
Add --deselect command line option
Fixes #3198
1 parent 063e2da commit d320da3

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Jon Sonesen
9797
Jonas Obrist
9898
Jordan Guymon
9999
Jordan Moldow
100+
Jordan Speicher
100101
Joshua Bronson
101102
Jurko Gospodnetić
102103
Justyna Janczyszyn

_pytest/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def pytest_addoption(parser):
6666
help="try to interpret all arguments as python packages.")
6767
group.addoption("--ignore", action="append", metavar="path",
6868
help="ignore path during collection (multi-allowed).")
69+
group.addoption("--deselect", action="append", metavar="item",
70+
help="deselect item during collection (multi-allowed).")
6971
# when changing this to --conf-cut-dir, config.py Conftest.setinitial
7072
# needs upgrading as well
7173
group.addoption('--confcutdir', dest="confcutdir", default=None,
@@ -208,6 +210,25 @@ def pytest_ignore_collect(path, config):
208210
return False
209211

210212

213+
def pytest_collection_modifyitems(items, config):
214+
deselectopt = config.getoption("deselect")
215+
if deselectopt is None:
216+
return
217+
218+
remaining = []
219+
deselected = []
220+
for colitem in items:
221+
for opt in deselectopt:
222+
if colitem.nodeid.startswith(opt):
223+
deselected.append(colitem)
224+
else:
225+
remaining.append(colitem)
226+
227+
if deselected:
228+
config.hook.pytest_deselected(items=deselected)
229+
items[:] = remaining
230+
231+
211232
@contextlib.contextmanager
212233
def _patched_find_module():
213234
"""Patch bug in pkgutil.ImpImporter.find_module

changelog/3198.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add command line option ``--deselect`` to allow deselection of individual tests at collection time.

doc/en/example/pythoncollection.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ you will see that ``pytest`` only collects test-modules, which do not match the
3939

4040
======= 5 passed in 0.02 seconds =======
4141

42+
Deselect tests during test collection
43+
-------------------------------------
44+
45+
Tests can individually be deselected during collection by passing the ``--deselect=item`` option.
46+
For example, say ``tests/foobar/test_foobar_01.py`` contains ``test_a`` and ``test_b``.
47+
You can run all of the tests within ``tests/`` *except* for ``tests/foobar/test_foobar_01.py::test_a``
48+
by invoking ``pytest`` with ``--deselect tests/foobar/test_foobar_01.py::test_a``.
49+
``pytest`` allows multiple ``--deselect`` options.
4250

4351
Keeping duplicate paths specified from command line
4452
----------------------------------------------------

testing/test_session.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ def test_exclude(testdir):
240240
result.stdout.fnmatch_lines(["*1 passed*"])
241241

242242

243+
def test_deselect(testdir):
244+
testdir.makepyfile(test_a="""
245+
import pytest
246+
def test_a1(): pass
247+
@pytest.mark.parametrize('b', range(10))
248+
def test_a2(b): pass
249+
""")
250+
result = testdir.runpytest("--deselect=test_a.py::test_a2[1]")
251+
assert result.ret == 0
252+
result.stdout.fnmatch_lines(["*11 passed, 1 deselected*"])
253+
254+
243255
def test_sessionfinish_with_start(testdir):
244256
testdir.makeconftest("""
245257
import os

0 commit comments

Comments
 (0)