Skip to content

Commit adebfd0

Browse files
committed
pdb: add option to skip pdb.set_trace()
1 parent 6b5cddc commit adebfd0

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

changelog/4854.feature.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The ``--pdb-skip`` option can now be used to ignore calls to
2+
``pdb.set_trace()`` (and ``pytest.set_trace()``).
3+
4+
This is meant to help while debugging, where you want to use e.g. ``--pdb`` or
5+
``--trace`` only, or just run the tests again without any interruption.

src/_pytest/debugging.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ def pytest_addoption(parser):
5959
action="store_true",
6060
help="Immediately break when running each test.",
6161
)
62+
group._addoption(
63+
"--pdb-skip",
64+
"--pdb-ignore-set_trace",
65+
dest="pdb_ignore_set_trace",
66+
action="store_true",
67+
help="Ignore calls to pdb.set_trace().",
68+
)
6269

6370

6471
def pytest_configure(config):
@@ -202,6 +209,9 @@ def setup(self, f, tb):
202209
@classmethod
203210
def set_trace(cls, *args, **kwargs):
204211
"""Invoke debugging via ``Pdb.set_trace``, dropping any IO capturing."""
212+
if pytestPDB._config: # Might not be available when called directly.
213+
if pytestPDB._config.getoption("pdb_ignore_set_trace"):
214+
return
205215
frame = sys._getframe().f_back
206216
_pdb = cls._init_pdb(*args, **kwargs)
207217
_pdb.set_trace(frame)

testing/test_pdb.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import _pytest._code
1111
import pytest
1212
from _pytest.debugging import _validate_usepdb_cls
13+
from _pytest.main import EXIT_NOTESTSCOLLECTED
1314

1415
try:
1516
breakpoint
@@ -1112,3 +1113,16 @@ def test_inner({fixture}):
11121113
assert child.exitstatus == 0
11131114
assert "= 1 passed in " in rest
11141115
assert "> PDB continue (IO-capturing resumed for fixture %s) >" % (fixture) in rest
1116+
1117+
1118+
def test_pdb_skip_option(testdir):
1119+
p = testdir.makepyfile(
1120+
"""
1121+
print("before_set_trace")
1122+
__import__('pdb').set_trace()
1123+
print("after_set_trace")
1124+
"""
1125+
)
1126+
result = testdir.runpytest_inprocess("--pdb-ignore-set_trace", "-s", p)
1127+
assert result.ret == EXIT_NOTESTSCOLLECTED
1128+
result.stdout.fnmatch_lines(["*before_set_trace*", "*after_set_trace*"])

0 commit comments

Comments
 (0)