From 0ac85218d1b761f09d67eb55e10d66575285c99c Mon Sep 17 00:00:00 2001 From: David Szotten Date: Mon, 19 Sep 2016 15:43:54 +0000 Subject: [PATCH 1/3] allow pdbcls without implying usepdb --- _pytest/debugging.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/_pytest/debugging.py b/_pytest/debugging.py index 299b0502850..685da82008f 100644 --- a/_pytest/debugging.py +++ b/_pytest/debugging.py @@ -20,15 +20,15 @@ def pytest_namespace(): return {'set_trace': pytestPDB().set_trace} def pytest_configure(config): - if config.getvalue("usepdb") or config.getvalue("usepdb_cls"): + if config.getvalue("usepdb_cls"): + modname, classname = config.getvalue("usepdb_cls").split(":") + __import__(modname) + pdb_cls = getattr(sys.modules[modname], classname) + else: + pdb_cls = pdb.Pdb + + if config.getvalue("usepdb"): config.pluginmanager.register(PdbInvoke(), 'pdbinvoke') - if config.getvalue("usepdb_cls"): - modname, classname = config.getvalue("usepdb_cls").split(":") - __import__(modname) - pdb_cls = getattr(sys.modules[modname], classname) - else: - pdb_cls = pdb.Pdb - pytestPDB._pdb_cls = pdb_cls old = (pdb.set_trace, pytestPDB._pluginmanager) def fin(): @@ -38,6 +38,7 @@ def fin(): pdb.set_trace = pytest.set_trace pytestPDB._pluginmanager = config.pluginmanager pytestPDB._config = config + pytestPDB._pdb_cls = pdb_cls config._cleanup.append(fin) class pytestPDB: From c7b4b8cf6fe3b2bb608f392839a2638857d6509a Mon Sep 17 00:00:00 2001 From: David Szotten Date: Mon, 19 Sep 2016 16:05:57 +0000 Subject: [PATCH 2/3] test --- testing/test_pdb.py | 46 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/testing/test_pdb.py b/testing/test_pdb.py index d79d7126209..15cb40eb696 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -12,6 +12,26 @@ def runpdb_and_get_report(testdir, source): return reports[1] +@pytest.fixture +def custom_pdb_calls(): + called = [] + + # install dummy debugger class and track which methods were called on it + class _CustomPdb: + def __init__(self, *args, **kwargs): + called.append("init") + + def reset(self): + called.append("reset") + + def interaction(self, *args): + called.append("interaction") + + _pytest._CustomPdb = _CustomPdb + return called + + + class TestPDB: @pytest.fixture @@ -334,22 +354,18 @@ def test_foo(): if child.isalive(): child.wait() - def test_pdb_custom_cls(self, testdir): - called = [] - - # install dummy debugger class and track which methods were called on it - class _CustomPdb: - def __init__(self, *args, **kwargs): - called.append("init") - - def reset(self): - called.append("reset") - - def interaction(self, *args): - called.append("interaction") + def test_pdb_custom_cls(self, testdir, custom_pdb_calls): + p1 = testdir.makepyfile("""xxx """) + result = testdir.runpytest_inprocess( + "--pdb", "--pdbcls=_pytest:_CustomPdb", p1) + result.stdout.fnmatch_lines([ + "*NameError*xxx*", + "*1 error*", + ]) + assert custom_pdb_calls == ["init", "reset", "interaction"] - _pytest._CustomPdb = _CustomPdb + def test_pdb_custom_cls_without_pdb(self, testdir, custom_pdb_calls): p1 = testdir.makepyfile("""xxx """) result = testdir.runpytest_inprocess( "--pdbcls=_pytest:_CustomPdb", p1) @@ -357,4 +373,4 @@ def interaction(self, *args): "*NameError*xxx*", "*1 error*", ]) - assert called == ["init", "reset", "interaction"] + assert custom_pdb_calls == [] From d75748ef6f158aca7e253d0632615bc67072d946 Mon Sep 17 00:00:00 2001 From: David Szotten Date: Wed, 21 Sep 2016 08:44:39 +0000 Subject: [PATCH 3/3] test for calling set_trace with custom pdb cls --- testing/test_pdb.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 15cb40eb696..f164f9691b8 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -374,3 +374,22 @@ def test_pdb_custom_cls_without_pdb(self, testdir, custom_pdb_calls): "*1 error*", ]) assert custom_pdb_calls == [] + + def test_pdb_custom_cls_with_settrace(self, testdir, monkeypatch): + testdir.makepyfile(custom_pdb=""" + class CustomPdb: + def set_trace(*args, **kwargs): + print 'custom set_trace>' + """) + p1 = testdir.makepyfile(""" + import pytest + + def test_foo(): + pytest.set_trace() + """) + monkeypatch.setenv('PYTHONPATH', str(testdir.tmpdir)) + child = testdir.spawn_pytest("--pdbcls=custom_pdb:CustomPdb %s" % str(p1)) + + child.expect('custom set_trace>') + if child.isalive(): + child.wait()