Skip to content

Commit

Permalink
Fix --trace for parametrized tests
Browse files Browse the repository at this point in the history
I'm not sure if this is the correct / best way to fix so please check
critically when reviewing

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!`
  • Loading branch information
davidszotten committed Oct 29, 2019
1 parent cefe6bf commit 96d8d0c
Show file tree
Hide file tree
Showing 3 changed files with 31 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
10 changes: 6 additions & 4 deletions src/_pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,14 @@ 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:
# if using e.g. parametrize the _fixtureinfo is shared
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(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 96d8d0c

Please sign in to comment.