Add support for yielded functions.

This commit is contained in:
Jeffrey Rackauckas 2018-07-02 19:46:26 -07:00
parent b75320ba95
commit 10a8691eca
2 changed files with 45 additions and 23 deletions

View File

@ -15,9 +15,6 @@ except ImportError:
SUPPORTS_BREAKPOINT_BUILTIN = False SUPPORTS_BREAKPOINT_BUILTIN = False
immediately_break = False
def pytest_addoption(parser): def pytest_addoption(parser):
group = parser.getgroup("general") group = parser.getgroup("general")
group._addoption( group._addoption(
@ -49,8 +46,8 @@ def pytest_configure(config):
else: else:
pdb_cls = pdb.Pdb pdb_cls = pdb.Pdb
global immediately_break if config.getvalue("trace"):
immediately_break = config.getvalue("trace") config.pluginmanager.register(PdbTrace(), "pdbtrace")
if config.getvalue("usepdb"): if config.getvalue("usepdb"):
config.pluginmanager.register(PdbInvoke(), "pdbinvoke") config.pluginmanager.register(PdbInvoke(), "pdbinvoke")
@ -76,24 +73,6 @@ def pytest_configure(config):
config._cleanup.append(fin) config._cleanup.append(fin)
@hookimpl(hookwrapper=True)
def pytest_pyfunc_call(pyfuncitem):
if immediately_break:
pytestPDB.set_trace(set_break=False)
testfunction = pyfuncitem.obj
pyfuncitem.obj = pdb.runcall
if pyfuncitem._isyieldedfunction():
pyfuncitem._args = [testfunction, *pyfuncitem._args]
else:
if "func" in pyfuncitem._fixtureinfo.argnames:
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)
yield
class pytestPDB(object): class pytestPDB(object):
""" Pseudo PDB that defers to the real pdb. """ """ Pseudo PDB that defers to the real pdb. """
@ -136,6 +115,28 @@ class PdbInvoke(object):
post_mortem(tb) post_mortem(tb)
class PdbTrace(object):
@hookimpl(hookwrapper=True)
def pytest_pyfunc_call(self, pyfuncitem):
_test_pytest_function(pyfuncitem)
yield
def _test_pytest_function(pyfuncitem):
pytestPDB.set_trace(set_break=False)
testfunction = pyfuncitem.obj
pyfuncitem.obj = pdb.runcall
if pyfuncitem._isyieldedfunction():
pyfuncitem._args = [testfunction, *pyfuncitem._args]
else:
if "func" in pyfuncitem._fixtureinfo.argnames:
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)
def _enter_pdb(node, excinfo, rep): def _enter_pdb(node, excinfo, rep):
# XXX we re-use the TerminalReporter's terminalwriter # XXX we re-use the TerminalReporter's terminalwriter
# because this seems to avoid some encoding related troubles # because this seems to avoid some encoding related troubles

View File

@ -714,3 +714,24 @@ class TestTraceOption:
assert "1 passed" in rest assert "1 passed" in rest
assert "reading from stdin while output" not in rest assert "reading from stdin while output" not in rest
TestPDB.flush(child) TestPDB.flush(child)
def test_trace_against_yield_test(self, testdir):
p1 = testdir.makepyfile(
"""
def is_equal(a, b):
assert a == b
def test_1():
assert is_equal, 1, 1
"""
)
child = testdir.spawn_pytest("--trace " + str(p1))
child.expect("test_1")
child.expect("(Pdb)")
child.sendline("c")
child.expect("(Pdb)")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 passed" in rest
assert "reading from stdin while output" not in rest
TestPDB.flush(child)