Skip to content

Commit b1abe5d

Browse files
authored
Merge pull request #3201 from uSpike/deselect_cli
Add "--deselect" command line option
2 parents 063e2da + 774c539 commit b1abe5d

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-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: 20 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="nodeid_prefix",
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,24 @@ def pytest_ignore_collect(path, config):
208210
return False
209211

210212

213+
def pytest_collection_modifyitems(items, config):
214+
deselect_prefixes = tuple(config.getoption("deselect") or [])
215+
if not deselect_prefixes:
216+
return
217+
218+
remaining = []
219+
deselected = []
220+
for colitem in items:
221+
if colitem.nodeid.startswith(deselect_prefixes):
222+
deselected.append(colitem)
223+
else:
224+
remaining.append(colitem)
225+
226+
if deselected:
227+
config.hook.pytest_deselected(items=deselected)
228+
items[:] = remaining
229+
230+
211231
@contextlib.contextmanager
212232
def _patched_find_module():
213233
"""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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ 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(3))
248+
def test_a2(b): pass
249+
""")
250+
result = testdir.runpytest("-v", "--deselect=test_a.py::test_a2[1]", "--deselect=test_a.py::test_a2[2]")
251+
assert result.ret == 0
252+
result.stdout.fnmatch_lines(["*2 passed, 2 deselected*"])
253+
for line in result.stdout.lines:
254+
assert not line.startswith(('test_a.py::test_a2[1]', 'test_a.py::test_a2[2]'))
255+
256+
243257
def test_sessionfinish_with_start(testdir):
244258
testdir.makeconftest("""
245259
import os

0 commit comments

Comments
 (0)