Skip to content

Commit 54d3cd5

Browse files
committed
Adding the --trace option.
1 parent 8680dfc commit 54d3cd5

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/_pytest/debugging.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
import os
66
from doctest import UnexpectedException
77

8+
from _pytest.config import hookimpl
9+
810
try:
911
from builtins import breakpoint # noqa
1012

1113
SUPPORTS_BREAKPOINT_BUILTIN = True
1214
except ImportError:
1315
SUPPORTS_BREAKPOINT_BUILTIN = False
1416

15-
17+
immediately_break = False
1618
def pytest_addoption(parser):
1719
group = parser.getgroup("general")
1820
group._addoption(
@@ -28,6 +30,12 @@ def pytest_addoption(parser):
2830
help="start a custom interactive Python debugger on errors. "
2931
"For example: --pdbcls=IPython.terminal.debugger:TerminalPdb",
3032
)
33+
group._addoption(
34+
"--trace",
35+
dest="trace",
36+
action="store_true",
37+
help="Immediately break when running each test.",
38+
)
3139

3240

3341
def pytest_configure(config):
@@ -63,6 +71,23 @@ def fin():
6371
config._cleanup.append(fin)
6472

6573

74+
75+
@hookimpl(hookwrapper=True)
76+
def pytest_pyfunc_call(pyfuncitem):
77+
if immediately_break:
78+
pytestPDB.set_trace(set_break=False)
79+
testfunction = pyfuncitem.obj
80+
pyfuncitem.obj = pdb.runcall
81+
if pyfuncitem._isyieldedfunction():
82+
pyfuncitem.args = [testfunction, pyfuncitem._args]
83+
else:
84+
pyfuncitem.funcargs['func'] = testfunction
85+
new_list = list(pyfuncitem._fixtureinfo.argnames)
86+
new_list.append('func')
87+
pyfuncitem._fixtureinfo.argnames = tuple(new_list)
88+
outcome = yield
89+
90+
6691
class pytestPDB(object):
6792
""" Pseudo PDB that defers to the real pdb. """
6893

@@ -71,7 +96,7 @@ class pytestPDB(object):
7196
_pdb_cls = pdb.Pdb
7297

7398
@classmethod
74-
def set_trace(cls):
99+
def set_trace(cls, set_break=True):
75100
""" invoke PDB set_trace debugging, dropping any IO capturing. """
76101
import _pytest.config
77102

@@ -84,7 +109,8 @@ def set_trace(cls):
84109
tw.line()
85110
tw.sep(">", "PDB set_trace (IO-capturing turned off)")
86111
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config)
87-
cls._pdb_cls().set_trace(frame)
112+
if set_break:
113+
cls._pdb_cls().set_trace(frame)
88114

89115

90116
class PdbInvoke(object):

0 commit comments

Comments
 (0)