diff --git a/_pytest/pdb.py b/_pytest/pdb.py index d9b4c2de8..1fe8dd61a 100644 --- a/_pytest/pdb.py +++ b/_pytest/pdb.py @@ -19,11 +19,13 @@ def pytest_configure(config): class pytestPDB: """ Pseudo PDB that defers to the real pdb. """ item = None + collector = None def set_trace(self): """ invoke PDB set_trace debugging, dropping any IO capturing. """ frame = sys._getframe().f_back - item = getattr(self, 'item', None) + item = self.item or self.collector + if item is not None: capman = item.config.pluginmanager.getplugin("capturemanager") out, err = capman.suspendcapture() @@ -38,6 +40,14 @@ def pdbitem(item): pytestPDB.item = item pytest_runtest_setup = pytest_runtest_call = pytest_runtest_teardown = pdbitem +@pytest.mark.tryfirst +def pytest_make_collect_report(__multicall__, collector): + try: + pytestPDB.collector = collector + return __multicall__.execute() + finally: + pytestPDB.collector = None + def pytest_runtest_makereport(): pytestPDB.item = None diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 9b9c5df4c..cd92b33d2 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -144,6 +144,19 @@ class TestPDB: child.sendeof() child.wait() + def test_pdb_used_in_generate_tests(self, testdir): + p1 = testdir.makepyfile(""" + import pytest + def pytest_generate_tests(metafunc): + pytest.set_trace() + x = 5 + def test_foo(a): + pass + """) + child = testdir.spawn_pytest(str(p1)) + child.expect("x = 5") + child.sendeof() + child.wait() def test_pdb_collection_failure_is_shown(self, testdir): p1 = testdir.makepyfile("""xxx """) result = testdir.runpytest("--pdb", p1)