Skip to content

Commit b421aef

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!` > RonnyPfannschmidt > >practically this belongs to the FunctionDefininition (and should only happen once at that) >unfortunately with the current design that's not exposed and we have a hack instead > >the ""correct" fix is not to have more than one place where this is added, but that's for after FunctionDefinition lands Co-Authored-By: Ronny Pfannschmidt <[email protected]>
1 parent cefe6bf commit b421aef

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,15 @@ def _test_pytest_function(pyfuncitem):
275275
_pdb = pytestPDB._init_pdb("runcall")
276276
testfunction = pyfuncitem.obj
277277
pyfuncitem.obj = _pdb.runcall
278-
if "func" in pyfuncitem._fixtureinfo.argnames: # pragma: no branch
278+
if "func" in pyfuncitem.funcargs: # pragma: no branch
279279
raise ValueError("--trace can't be used with a fixture named func!")
280280
pyfuncitem.funcargs["func"] = testfunction
281-
new_list = list(pyfuncitem._fixtureinfo.argnames)
282-
new_list.append("func")
283-
pyfuncitem._fixtureinfo.argnames = tuple(new_list)
281+
if "func" not in pyfuncitem._fixtureinfo.argnames:
282+
# TODO: when using parameterized tests, the _fixtureinfo is shared
283+
# that needs to move to FunctionDefinition
284+
new_list = list(pyfuncitem._fixtureinfo.argnames)
285+
new_list.append("func")
286+
pyfuncitem._fixtureinfo.argnames = tuple(new_list)
284287

285288

286289
def _enter_pdb(node, excinfo, rep):

testing/test_pdb.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,30 @@ 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+
1033+
@pytest.mark.parametrize('x', [1,2])
1034+
def test_1(x):
1035+
assert True
1036+
"""
1037+
)
1038+
child = testdir.spawn_pytest("--trace " + str(p1))
1039+
child.expect("test_1")
1040+
child.expect("Pdb")
1041+
child.sendline("c")
1042+
child.expect("test_1")
1043+
child.expect("Pdb")
1044+
child.sendline("c")
1045+
rest = child.read().decode("utf8")
1046+
assert "2 passed in" in rest
1047+
assert "reading from stdin while output" not in rest
1048+
# Only printed once - not on stderr.
1049+
assert "Exit: Quitting debugger" not in child.before.decode("utf8")
1050+
TestPDB.flush(child)
1051+
10281052

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

0 commit comments

Comments
 (0)