updates for code review recommendations

This commit is contained in:
Anthony Shaw 2018-03-27 17:38:17 +11:00
parent e97bd87ee2
commit f1f4c8c104
No known key found for this signature in database
GPG Key ID: AB4A19AE1CE85744
2 changed files with 23 additions and 22 deletions

View File

@ -36,7 +36,7 @@ def pytest_configure(config):
# Use custom Pdb class set_trace instead of default Pdb on breakpoint() call # Use custom Pdb class set_trace instead of default Pdb on breakpoint() call
if SUPPORTS_BREAKPOINT_BUILTIN: if SUPPORTS_BREAKPOINT_BUILTIN:
_environ_pythonbreakpoint = getattr(os.environ, 'PYTHONBREAKPOINT', '') _environ_pythonbreakpoint = os.environ.get('PYTHONBREAKPOINT', '')
if _environ_pythonbreakpoint == '': if _environ_pythonbreakpoint == '':
sys.breakpointhook = pytestPDB.set_trace sys.breakpointhook = pytestPDB.set_trace

View File

@ -9,7 +9,7 @@ from _pytest.debugging import (SUPPORTS_BREAKPOINT_BUILTIN, pytestPDB,
import pytest import pytest
_ENVIRON_PYTHONBREAKPOINT = getattr(os.environ, 'PYTHONBREAKPOINT', '') _ENVIRON_PYTHONBREAKPOINT = os.environ.get('PYTHONBREAKPOINT', '')
def runpdb_and_get_report(testdir, source): def runpdb_and_get_report(testdir, source):
@ -59,7 +59,8 @@ def custom_debugger_hook():
called.append("set_trace") called.append("set_trace")
_pytest._CustomDebugger = _CustomDebugger _pytest._CustomDebugger = _CustomDebugger
return called yield called
del _pytest._CustomDebugger
class TestPDB(object): class TestPDB(object):
@ -492,35 +493,39 @@ class TestDebuggingBreakpoints(object):
Test that sys.breakpointhook is set to the custom Pdb class once configured, test that Test that sys.breakpointhook is set to the custom Pdb class once configured, test that
hook is reset to system value once pytest has been unconfigured hook is reset to system value once pytest has been unconfigured
""" """
config = testdir.parseconfig() testdir.makeconftest("""
from pytest import hookimpl
from _pytest.debugging import pytestPDB
pytest_configure(config) @hookimpl(hookwrapper=True)
assert sys.breakpointhook == pytestPDB.set_trace def pytest_configure(config):
yield
assert sys.breakpointhook is pytestPDB.set_trace
@hookimpl(hookwrapper=True)
def pytest_unconfigure(config):
yield
assert sys.breakpointhook is sys.__breakpoint__
p1 = testdir.makepyfile("""
def test_nothing():
a = 0
assert a == 0
""") """)
result = testdir.runpytest_inprocess("", p1) testdir.makepyfile("""
result.stdout.fnmatch_lines([ def test_nothing(): pass
"* passed*", """)
])
assert sys.breakpointhook != pytestPDB.set_trace
@pytest.mark.parametrize('args', [('--pdb',), ()])
@pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin") @pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin")
def test_sys_breakpointhook_configure_and_unconfigure_with_pdb_flag(self, testdir): def test_sys_breakpointhook_configure_and_unconfigure_with_pdb_flag(self, testdir, args):
config = testdir.parseconfig() config = testdir.parseconfig()
pytest_configure(config) pytest_configure(config)
assert sys.breakpointhook == pytestPDB.set_trace assert sys.breakpointhook == pytestPDB.set_trace
p1 = testdir.makepyfile(""" testdir.makepyfile("""
def test_nothing(): def test_nothing():
a = 0 a = 0
assert a == 0 assert a == 0
""") """)
result = testdir.runpytest_inprocess("--pdb", p1) result = testdir.runpytest_inprocess(*args)
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*1 passed*", "*1 passed*",
]) ])
@ -558,10 +563,6 @@ class TestDebuggingBreakpoints(object):
@pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin") @pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin")
def test_pdb_not_altered(self, testdir): def test_pdb_not_altered(self, testdir):
"""
Test that calling PDB set_trace() is not affected
when PYTHONBREAKPOINT=0
"""
p1 = testdir.makepyfile(""" p1 = testdir.makepyfile("""
import pdb import pdb
def test_1(): def test_1():