Skip to content

Commit 87fcdf2

Browse files
committed
Add --deselect command line option
Fixes pytest-dev#3198
1 parent 063e2da commit 87fcdf2

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

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

210212

213+
def pytest_collection_modifyitems(items, config):
214+
excludeopt = config.getoption("deselect")
215+
if excludeopt is None:
216+
return
217+
218+
remaining = []
219+
deselected = []
220+
for colitem in items:
221+
if colitem.nodeid in excludeopt:
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ def test_exclude(testdir):
240240
result.stdout.fnmatch_lines(["*1 passed*"])
241241

242242

243+
def test_deselect(testdir):
244+
testdir.makepyfile(test_a="""
245+
def test_a1(): pass
246+
def test_a2(): pass
247+
""")
248+
result = testdir.runpytest("--deselect=test_a.py::test_a2")
249+
assert result.ret == 0
250+
result.stdout.fnmatch_lines(["*1 passed, 1 deselected*"])
251+
252+
243253
def test_sessionfinish_with_start(testdir):
244254
testdir.makeconftest("""
245255
import os

0 commit comments

Comments
 (0)