Skip to content

Commit 844a957

Browse files
Fix --trace for parametrized tests
Without this, the second time it tries to stop in a parametrized function it raises instead: `ValueError: --trace can't be used with a fixture named func!` Test (and changelog tweaks) thanks to @blueyed Co-Authored-By: Ronny Pfannschmidt <[email protected]>
1 parent cefe6bf commit 844a957

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

changelog/6099.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``--trace`` when used with parametrized functions.

src/_pytest/debugging.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" interactive debugging with PDB, the Python Debugger. """
22
import argparse
3+
import functools
34
import pdb
45
import sys
56
from doctest import UnexpectedException
@@ -274,13 +275,24 @@ def pytest_pyfunc_call(self, pyfuncitem):
274275
def _test_pytest_function(pyfuncitem):
275276
_pdb = pytestPDB._init_pdb("runcall")
276277
testfunction = pyfuncitem.obj
277-
pyfuncitem.obj = _pdb.runcall
278-
if "func" in pyfuncitem._fixtureinfo.argnames: # pragma: no branch
279-
raise ValueError("--trace can't be used with a fixture named func!")
280-
pyfuncitem.funcargs["func"] = testfunction
281-
new_list = list(pyfuncitem._fixtureinfo.argnames)
282-
new_list.append("func")
283-
pyfuncitem._fixtureinfo.argnames = tuple(new_list)
278+
279+
# we need a copy of pdb.runcall because it takes `func` as a parameter
280+
# which means we can't have a kwarg called func
281+
@functools.wraps(testfunction)
282+
def runcall(*args, **kwds):
283+
_pdb.reset()
284+
sys.settrace(_pdb.trace_dispatch)
285+
res = None
286+
try:
287+
res = testfunction(*args, **kwds)
288+
except BdbQuit:
289+
pass
290+
finally:
291+
_pdb.quitting = True
292+
sys.settrace(None)
293+
return res
294+
295+
pyfuncitem.obj = runcall
284296

285297

286298
def _enter_pdb(node, excinfo, rep):

testing/test_pdb.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,51 @@ def test_3():
10251025
assert "Exit: Quitting debugger" not in child.before.decode("utf8")
10261026
TestPDB.flush(child)
10271027

1028+
def test_trace_with_parametrize_handles_shared_fixtureinfo(self, testdir):
1029+
p1 = testdir.makepyfile(
1030+
"""
1031+
import pytest
1032+
@pytest.mark.parametrize('myparam', [1,2])
1033+
def test_1(myparam, request):
1034+
assert myparam in (1, 2)
1035+
assert request.function.__name__ == "test_1"
1036+
@pytest.mark.parametrize('func', [1,2])
1037+
def test_func(func, request):
1038+
assert func in (1, 2)
1039+
assert request.function.__name__ == "test_func"
1040+
@pytest.mark.parametrize('myparam', [1,2])
1041+
def test_func_kw(myparam, request, func="func_kw"):
1042+
assert myparam in (1, 2)
1043+
assert func == "func_kw"
1044+
assert request.function.__name__ == "test_func_kw"
1045+
"""
1046+
)
1047+
child = testdir.spawn_pytest("--trace " + str(p1))
1048+
for func, argname in [
1049+
("test_1", "myparam"),
1050+
("test_func", "func"),
1051+
("test_func_kw", "myparam"),
1052+
]:
1053+
child.expect_exact("> PDB runcall (IO-capturing turned off) >")
1054+
child.expect_exact(func)
1055+
child.expect_exact("Pdb")
1056+
child.sendline("args")
1057+
child.expect_exact("{} = 1\r\n".format(argname))
1058+
child.expect_exact("Pdb")
1059+
child.sendline("c")
1060+
child.expect_exact("Pdb")
1061+
child.sendline("args")
1062+
child.expect_exact("{} = 2\r\n".format(argname))
1063+
child.expect_exact("Pdb")
1064+
child.sendline("c")
1065+
child.expect_exact("> PDB continue (IO-capturing resumed) >")
1066+
rest = child.read().decode("utf8")
1067+
assert "6 passed in" in rest
1068+
assert "reading from stdin while output" not in rest
1069+
# Only printed once - not on stderr.
1070+
assert "Exit: Quitting debugger" not in child.before.decode("utf8")
1071+
TestPDB.flush(child)
1072+
10281073

10291074
def test_trace_after_runpytest(testdir):
10301075
"""Test that debugging's pytest_configure is re-entrant."""

0 commit comments

Comments
 (0)