Skip to content

Commit 5e96edd

Browse files
authored
Merge pull request #1952 from davidszotten/pdbcls_without_pdb_on_fail
Pdbcls without pdb on fail
2 parents 887c097 + d75748e commit 5e96edd

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

_pytest/debugging.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ def pytest_namespace():
2020
return {'set_trace': pytestPDB().set_trace}
2121

2222
def pytest_configure(config):
23-
if config.getvalue("usepdb") or config.getvalue("usepdb_cls"):
23+
if config.getvalue("usepdb_cls"):
24+
modname, classname = config.getvalue("usepdb_cls").split(":")
25+
__import__(modname)
26+
pdb_cls = getattr(sys.modules[modname], classname)
27+
else:
28+
pdb_cls = pdb.Pdb
29+
30+
if config.getvalue("usepdb"):
2431
config.pluginmanager.register(PdbInvoke(), 'pdbinvoke')
25-
if config.getvalue("usepdb_cls"):
26-
modname, classname = config.getvalue("usepdb_cls").split(":")
27-
__import__(modname)
28-
pdb_cls = getattr(sys.modules[modname], classname)
29-
else:
30-
pdb_cls = pdb.Pdb
31-
pytestPDB._pdb_cls = pdb_cls
3232

3333
old = (pdb.set_trace, pytestPDB._pluginmanager)
3434
def fin():
@@ -38,6 +38,7 @@ def fin():
3838
pdb.set_trace = pytest.set_trace
3939
pytestPDB._pluginmanager = config.pluginmanager
4040
pytestPDB._config = config
41+
pytestPDB._pdb_cls = pdb_cls
4142
config._cleanup.append(fin)
4243

4344
class pytestPDB:

testing/test_pdb.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ def runpdb_and_get_report(testdir, source):
1212
return reports[1]
1313

1414

15+
@pytest.fixture
16+
def custom_pdb_calls():
17+
called = []
18+
19+
# install dummy debugger class and track which methods were called on it
20+
class _CustomPdb:
21+
def __init__(self, *args, **kwargs):
22+
called.append("init")
23+
24+
def reset(self):
25+
called.append("reset")
26+
27+
def interaction(self, *args):
28+
called.append("interaction")
29+
30+
_pytest._CustomPdb = _CustomPdb
31+
return called
32+
33+
34+
1535
class TestPDB:
1636

1737
@pytest.fixture
@@ -334,27 +354,42 @@ def test_foo():
334354
if child.isalive():
335355
child.wait()
336356

337-
def test_pdb_custom_cls(self, testdir):
338-
called = []
339-
340-
# install dummy debugger class and track which methods were called on it
341-
class _CustomPdb:
342-
def __init__(self, *args, **kwargs):
343-
called.append("init")
344-
345-
def reset(self):
346-
called.append("reset")
347-
348-
def interaction(self, *args):
349-
called.append("interaction")
357+
def test_pdb_custom_cls(self, testdir, custom_pdb_calls):
358+
p1 = testdir.makepyfile("""xxx """)
359+
result = testdir.runpytest_inprocess(
360+
"--pdb", "--pdbcls=_pytest:_CustomPdb", p1)
361+
result.stdout.fnmatch_lines([
362+
"*NameError*xxx*",
363+
"*1 error*",
364+
])
365+
assert custom_pdb_calls == ["init", "reset", "interaction"]
350366

351-
_pytest._CustomPdb = _CustomPdb
352367

368+
def test_pdb_custom_cls_without_pdb(self, testdir, custom_pdb_calls):
353369
p1 = testdir.makepyfile("""xxx """)
354370
result = testdir.runpytest_inprocess(
355371
"--pdbcls=_pytest:_CustomPdb", p1)
356372
result.stdout.fnmatch_lines([
357373
"*NameError*xxx*",
358374
"*1 error*",
359375
])
360-
assert called == ["init", "reset", "interaction"]
376+
assert custom_pdb_calls == []
377+
378+
def test_pdb_custom_cls_with_settrace(self, testdir, monkeypatch):
379+
testdir.makepyfile(custom_pdb="""
380+
class CustomPdb:
381+
def set_trace(*args, **kwargs):
382+
print 'custom set_trace>'
383+
""")
384+
p1 = testdir.makepyfile("""
385+
import pytest
386+
387+
def test_foo():
388+
pytest.set_trace()
389+
""")
390+
monkeypatch.setenv('PYTHONPATH', str(testdir.tmpdir))
391+
child = testdir.spawn_pytest("--pdbcls=custom_pdb:CustomPdb %s" % str(p1))
392+
393+
child.expect('custom set_trace>')
394+
if child.isalive():
395+
child.wait()

0 commit comments

Comments
 (0)