Skip to content

Commit

Permalink
Fix --trace for parametrized tests
Browse files Browse the repository at this point in the history
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 <opensource@ronnypfannschmidt.de>
  • Loading branch information
davidszotten and RonnyPfannschmidt committed Oct 29, 2019
1 parent cefe6bf commit b421aef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/6099.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Fix ``--trace`` when used with parametrized functions
11 changes: 7 additions & 4 deletions src/_pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,15 @@ def _test_pytest_function(pyfuncitem):
_pdb = pytestPDB._init_pdb("runcall")
testfunction = pyfuncitem.obj
pyfuncitem.obj = _pdb.runcall
if "func" in pyfuncitem._fixtureinfo.argnames: # pragma: no branch
if "func" in pyfuncitem.funcargs: # pragma: no branch
raise ValueError("--trace can't be used with a fixture named func!")
pyfuncitem.funcargs["func"] = testfunction
new_list = list(pyfuncitem._fixtureinfo.argnames)
new_list.append("func")
pyfuncitem._fixtureinfo.argnames = tuple(new_list)
if "func" not in pyfuncitem._fixtureinfo.argnames:
# TODO: when using parameterized tests, the _fixtureinfo is shared
# that needs to move to FunctionDefinition
new_list = list(pyfuncitem._fixtureinfo.argnames)
new_list.append("func")
pyfuncitem._fixtureinfo.argnames = tuple(new_list)


def _enter_pdb(node, excinfo, rep):
Expand Down
24 changes: 24 additions & 0 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,30 @@ def test_3():
assert "Exit: Quitting debugger" not in child.before.decode("utf8")
TestPDB.flush(child)

def test_trace_with_parametrize_handles_shared_fixtureinfo(self, testdir):
p1 = testdir.makepyfile(
"""
import pytest
@pytest.mark.parametrize('x', [1,2])
def test_1(x):
assert True
"""
)
child = testdir.spawn_pytest("--trace " + str(p1))
child.expect("test_1")
child.expect("Pdb")
child.sendline("c")
child.expect("test_1")
child.expect("Pdb")
child.sendline("c")
rest = child.read().decode("utf8")
assert "2 passed in" in rest
assert "reading from stdin while output" not in rest
# Only printed once - not on stderr.
assert "Exit: Quitting debugger" not in child.before.decode("utf8")
TestPDB.flush(child)


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

0 comments on commit b421aef

Please sign in to comment.