Skip to content

Commit

Permalink
Add --deselect command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
uSpike committed Feb 12, 2018
1 parent 063e2da commit d320da3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Jon Sonesen
Jonas Obrist
Jordan Guymon
Jordan Moldow
Jordan Speicher
Joshua Bronson
Jurko Gospodnetić
Justyna Janczyszyn
Expand Down
21 changes: 21 additions & 0 deletions _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def pytest_addoption(parser):
help="try to interpret all arguments as python packages.")
group.addoption("--ignore", action="append", metavar="path",
help="ignore path during collection (multi-allowed).")
group.addoption("--deselect", action="append", metavar="item",
help="deselect item during collection (multi-allowed).")
# when changing this to --conf-cut-dir, config.py Conftest.setinitial
# needs upgrading as well
group.addoption('--confcutdir', dest="confcutdir", default=None,
Expand Down Expand Up @@ -208,6 +210,25 @@ def pytest_ignore_collect(path, config):
return False


def pytest_collection_modifyitems(items, config):
deselectopt = config.getoption("deselect")
if deselectopt is None:
return

remaining = []
deselected = []
for colitem in items:
for opt in deselectopt:
if colitem.nodeid.startswith(opt):
deselected.append(colitem)
else:
remaining.append(colitem)

if deselected:
config.hook.pytest_deselected(items=deselected)
items[:] = remaining


@contextlib.contextmanager
def _patched_find_module():
"""Patch bug in pkgutil.ImpImporter.find_module
Expand Down
1 change: 1 addition & 0 deletions changelog/3198.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add command line option ``--deselect`` to allow deselection of individual tests at collection time.
8 changes: 8 additions & 0 deletions doc/en/example/pythoncollection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ you will see that ``pytest`` only collects test-modules, which do not match the

======= 5 passed in 0.02 seconds =======

Deselect tests during test collection
-------------------------------------

Tests can individually be deselected during collection by passing the ``--deselect=item`` option.
For example, say ``tests/foobar/test_foobar_01.py`` contains ``test_a`` and ``test_b``.
You can run all of the tests within ``tests/`` *except* for ``tests/foobar/test_foobar_01.py::test_a``
by invoking ``pytest`` with ``--deselect tests/foobar/test_foobar_01.py::test_a``.
``pytest`` allows multiple ``--deselect`` options.

Keeping duplicate paths specified from command line
----------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions testing/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ def test_exclude(testdir):
result.stdout.fnmatch_lines(["*1 passed*"])


def test_deselect(testdir):
testdir.makepyfile(test_a="""
import pytest
def test_a1(): pass
@pytest.mark.parametrize('b', range(10))
def test_a2(b): pass
""")
result = testdir.runpytest("--deselect=test_a.py::test_a2[1]")
assert result.ret == 0
result.stdout.fnmatch_lines(["*11 passed, 1 deselected*"])


def test_sessionfinish_with_start(testdir):
testdir.makeconftest("""
import os
Expand Down

0 comments on commit d320da3

Please sign in to comment.