Skip to content

Commit e88aa95

Browse files
authored
Merge pull request #4854 from blueyed/pdb-skip
pdb: add option to skip `pdb.set_trace()`
2 parents 1410d3d + adebfd0 commit e88aa95

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):
@@ -209,6 +216,9 @@ def setup(self, f, tb):
209216
@classmethod
210217
def set_trace(cls, *args, **kwargs):
211218
"""Invoke debugging via ``Pdb.set_trace``, dropping any IO capturing."""
219+
if pytestPDB._config: # Might not be available when called directly.
220+
if pytestPDB._config.getoption("pdb_ignore_set_trace"):
221+
return
212222
frame = sys._getframe().f_back
213223
_pdb = cls._init_pdb(*args, **kwargs)
214224
_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
@@ -1121,3 +1122,16 @@ def test_inner({fixture}):
11211122
assert child.exitstatus == 0
11221123
assert "= 1 passed in " in rest
11231124
assert "> PDB continue (IO-capturing resumed for fixture %s) >" % (fixture) in rest
1125+
1126+
1127+
def test_pdb_skip_option(testdir):
1128+
p = testdir.makepyfile(
1129+
"""
1130+
print("before_set_trace")
1131+
__import__('pdb').set_trace()
1132+
print("after_set_trace")
1133+
"""
1134+
)
1135+
result = testdir.runpytest_inprocess("--pdb-ignore-set_trace", "-s", p)
1136+
assert result.ret == EXIT_NOTESTSCOLLECTED
1137+
result.stdout.fnmatch_lines(["*before_set_trace*", "*after_set_trace*"])

0 commit comments

Comments
 (0)