pytest_{enter,leave}_pdb: pass through pdb instance

This commit is contained in:
Daniel Hahler 2018-10-24 23:27:14 +02:00
parent a4ea66cb1f
commit ede3a4e850
4 changed files with 23 additions and 6 deletions

View File

@ -1 +1,4 @@
Resume capturing output after ``continue`` with ``__import__("pdb").set_trace()``. Resume capturing output after ``continue`` with ``__import__("pdb").set_trace()``.
This also adds a new ``pytest_leave_pdb`` hook, and passes in ``pdb`` to the
existing ``pytest_enter_pdb`` hook.

View File

@ -95,7 +95,6 @@ class pytestPDB(object):
tw = _pytest.config.create_terminal_writer(cls._config) tw = _pytest.config.create_terminal_writer(cls._config)
tw.line() tw.line()
tw.sep(">", "PDB set_trace (IO-capturing turned off)") tw.sep(">", "PDB set_trace (IO-capturing turned off)")
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config)
class _PdbWrapper(cls._pdb_cls, object): class _PdbWrapper(cls._pdb_cls, object):
_pytest_capman = capman _pytest_capman = capman
@ -108,7 +107,9 @@ class pytestPDB(object):
tw.line() tw.line()
tw.sep(">", "PDB continue (IO-capturing resumed)") tw.sep(">", "PDB continue (IO-capturing resumed)")
self._pytest_capman.resume_global_capture() self._pytest_capman.resume_global_capture()
cls._pluginmanager.hook.pytest_leave_pdb(config=cls._config) cls._pluginmanager.hook.pytest_leave_pdb(
config=cls._config, pdb=self
)
self._continued = True self._continued = True
return ret return ret
@ -129,6 +130,7 @@ class pytestPDB(object):
return ret return ret
_pdb = _PdbWrapper() _pdb = _PdbWrapper()
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb)
else: else:
_pdb = cls._pdb_cls() _pdb = cls._pdb_cls()

View File

@ -603,19 +603,21 @@ def pytest_exception_interact(node, call, report):
""" """
def pytest_enter_pdb(config): def pytest_enter_pdb(config, pdb):
""" called upon pdb.set_trace(), can be used by plugins to take special """ called upon pdb.set_trace(), can be used by plugins to take special
action just before the python debugger enters in interactive mode. action just before the python debugger enters in interactive mode.
:param _pytest.config.Config config: pytest config object :param _pytest.config.Config config: pytest config object
:param pdb.Pdb pdb: Pdb instance
""" """
def pytest_leave_pdb(config): def pytest_leave_pdb(config, pdb):
""" called when leaving pdb (e.g. with continue after pdb.set_trace()). """ called when leaving pdb (e.g. with continue after pdb.set_trace()).
Can be used by plugins to take special action just after the python Can be used by plugins to take special action just after the python
debugger leaves interactive mode. debugger leaves interactive mode.
:param _pytest.config.Config config: pytest config object :param _pytest.config.Config config: pytest config object
:param pdb.Pdb pdb: Pdb instance
""" """

View File

@ -550,16 +550,26 @@ class TestPDB(object):
def test_enter_leave_pdb_hooks_are_called(self, testdir): def test_enter_leave_pdb_hooks_are_called(self, testdir):
testdir.makeconftest( testdir.makeconftest(
""" """
mypdb = None
def pytest_configure(config): def pytest_configure(config):
config.testing_verification = 'configured' config.testing_verification = 'configured'
def pytest_enter_pdb(config): def pytest_enter_pdb(config, pdb):
assert config.testing_verification == 'configured' assert config.testing_verification == 'configured'
print('enter_pdb_hook') print('enter_pdb_hook')
def pytest_leave_pdb(config): global mypdb
mypdb = pdb
mypdb.set_attribute = "bar"
def pytest_leave_pdb(config, pdb):
assert config.testing_verification == 'configured' assert config.testing_verification == 'configured'
print('leave_pdb_hook') print('leave_pdb_hook')
global mypdb
assert mypdb is pdb
assert mypdb.set_attribute == "bar"
""" """
) )
p1 = testdir.makepyfile( p1 = testdir.makepyfile(