-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for the new builtin breakpoint function in Python 3.7 #3331
Merged
RonnyPfannschmidt
merged 22 commits into
pytest-dev:features
from
tonybaloney:breakpoint_support
Apr 10, 2018
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9edcb7e
start acceptance testing
tonybaloney 3bca983
add a module global for whether the current runtime supports the buil…
tonybaloney 91d99af
assert that custom PDB class is used as breakpoint hook where supported
tonybaloney 5a53b9a
move tests to test_pdb
tonybaloney a1ff758
"Added acceptance tests for configuration of sys.breakpointhook and r…
tonybaloney 21ada0f
add check for support of breakpoint() and use Custom Pdb class when s…
tonybaloney 0e83e4f
conditional for resetting of sys.breakpointhook for cleanup where bre…
tonybaloney db581ea
add tests to validate that --pdbcls custom debugger classes will be c…
tonybaloney dcbba38
add changelog entry
tonybaloney 1ec9913
add myself to authors
tonybaloney 242fb78
linting and removed double test
tonybaloney e97bd87
fix assertion when hypothesis is installed (which is should be for de…
tonybaloney f1f4c8c
updates for code review recommendations
tonybaloney 671ab5a
update documentation for new feature
tonybaloney b45006e
fix syntax
tonybaloney 0a4200b
Improve docs formatting
nicoddemus 060f047
Use full link in CHANGELOG
nicoddemus 3998b70
add test for custom environment variable
tonybaloney 09e5a22
add broken test
tonybaloney 804392e
Fix tests that check that breakpoint function is configured/restored
nicoddemus 4d84759
remove a test that would fail because pytest is being used to test it…
tonybaloney 0762666
Remove unused pytestPDB import
nicoddemus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support for Python 3.7's builtin ``breakpoint()`` method, see `Using the builtin breakpoint function <https://docs.pytest.org/en/latest/usage.html#breakpoint-builtin>`_ for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
from __future__ import absolute_import, division, print_function | ||
import sys | ||
import platform | ||
import os | ||
|
||
import _pytest._code | ||
from _pytest.debugging import SUPPORTS_BREAKPOINT_BUILTIN | ||
import pytest | ||
|
||
|
||
_ENVIRON_PYTHONBREAKPOINT = os.environ.get('PYTHONBREAKPOINT', '') | ||
|
||
|
||
def runpdb_and_get_report(testdir, source): | ||
p = testdir.makepyfile(source) | ||
result = testdir.runpytest_inprocess("--pdb", p) | ||
|
@@ -33,6 +38,30 @@ def interaction(self, *args): | |
return called | ||
|
||
|
||
@pytest.fixture | ||
def custom_debugger_hook(): | ||
called = [] | ||
|
||
# install dummy debugger class and track which methods were called on it | ||
class _CustomDebugger(object): | ||
def __init__(self, *args, **kwargs): | ||
called.append("init") | ||
|
||
def reset(self): | ||
called.append("reset") | ||
|
||
def interaction(self, *args): | ||
called.append("interaction") | ||
|
||
def set_trace(self, frame): | ||
print("**CustomDebugger**") | ||
called.append("set_trace") | ||
|
||
_pytest._CustomDebugger = _CustomDebugger | ||
yield called | ||
del _pytest._CustomDebugger | ||
|
||
|
||
class TestPDB(object): | ||
|
||
@pytest.fixture | ||
|
@@ -434,3 +463,122 @@ def test_foo(): | |
|
||
child.expect('custom set_trace>') | ||
self.flush(child) | ||
|
||
|
||
class TestDebuggingBreakpoints(object): | ||
|
||
def test_supports_breakpoint_module_global(self): | ||
""" | ||
Test that supports breakpoint global marks on Python 3.7+ and not on | ||
CPython 3.5, 2.7 | ||
""" | ||
if sys.version_info.major == 3 and sys.version_info.minor >= 7: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could use |
||
assert SUPPORTS_BREAKPOINT_BUILTIN is True | ||
if sys.version_info.major == 3 and sys.version_info.minor == 5: | ||
assert SUPPORTS_BREAKPOINT_BUILTIN is False | ||
if sys.version_info.major == 2 and sys.version_info.minor == 7: | ||
assert SUPPORTS_BREAKPOINT_BUILTIN is False | ||
|
||
@pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin") | ||
@pytest.mark.parametrize('arg', ['--pdb', '']) | ||
def test_sys_breakpointhook_configure_and_unconfigure(self, testdir, arg): | ||
""" | ||
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 | ||
""" | ||
testdir.makeconftest(""" | ||
import sys | ||
from pytest import hookimpl | ||
from _pytest.debugging import pytestPDB | ||
|
||
def pytest_configure(config): | ||
config._cleanup.append(check_restored) | ||
|
||
def check_restored(): | ||
assert sys.breakpointhook == sys.__breakpointhook__ | ||
|
||
def test_check(): | ||
assert sys.breakpointhook == pytestPDB.set_trace | ||
""") | ||
testdir.makepyfile(""" | ||
def test_nothing(): pass | ||
""") | ||
args = (arg,) if arg else () | ||
result = testdir.runpytest_subprocess(*args) | ||
result.stdout.fnmatch_lines([ | ||
'*1 passed in *', | ||
]) | ||
|
||
@pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin") | ||
def test_pdb_custom_cls(self, testdir, custom_debugger_hook): | ||
p1 = testdir.makepyfile(""" | ||
def test_nothing(): | ||
breakpoint() | ||
""") | ||
result = testdir.runpytest_inprocess( | ||
"--pdb", "--pdbcls=_pytest:_CustomDebugger", p1) | ||
result.stdout.fnmatch_lines([ | ||
"*CustomDebugger*", | ||
"*1 passed*", | ||
]) | ||
assert custom_debugger_hook == ["init", "set_trace"] | ||
|
||
@pytest.mark.parametrize('arg', ['--pdb', '']) | ||
@pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin") | ||
def test_environ_custom_class(self, testdir, custom_debugger_hook, arg): | ||
testdir.makeconftest(""" | ||
import os | ||
import sys | ||
|
||
os.environ['PYTHONBREAKPOINT'] = '_pytest._CustomDebugger.set_trace' | ||
|
||
def pytest_configure(config): | ||
config._cleanup.append(check_restored) | ||
|
||
def check_restored(): | ||
assert sys.breakpointhook == sys.__breakpointhook__ | ||
|
||
def test_check(): | ||
import _pytest | ||
assert sys.breakpointhook is _pytest._CustomDebugger.set_trace | ||
""") | ||
testdir.makepyfile(""" | ||
def test_nothing(): pass | ||
""") | ||
args = (arg,) if arg else () | ||
result = testdir.runpytest_subprocess(*args) | ||
result.stdout.fnmatch_lines([ | ||
'*1 passed in *', | ||
]) | ||
|
||
@pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin") | ||
@pytest.mark.skipif(not _ENVIRON_PYTHONBREAKPOINT == '', reason="Requires breakpoint() default value") | ||
def test_sys_breakpoint_interception(self, testdir): | ||
p1 = testdir.makepyfile(""" | ||
def test_1(): | ||
breakpoint() | ||
""") | ||
child = testdir.spawn_pytest(str(p1)) | ||
child.expect("test_1") | ||
child.expect("(Pdb)") | ||
child.sendeof() | ||
rest = child.read().decode("utf8") | ||
assert "1 failed" in rest | ||
assert "reading from stdin while output" not in rest | ||
TestPDB.flush(child) | ||
|
||
@pytest.mark.skipif(not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin") | ||
def test_pdb_not_altered(self, testdir): | ||
p1 = testdir.makepyfile(""" | ||
import pdb | ||
def test_1(): | ||
pdb.set_trace() | ||
""") | ||
child = testdir.spawn_pytest(str(p1)) | ||
child.expect("test_1") | ||
child.expect("(Pdb)") | ||
child.sendeof() | ||
rest = child.read().decode("utf8") | ||
assert "1 failed" in rest | ||
assert "reading from stdin while output" not in rest | ||
TestPDB.flush(child) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well hello fellow anthony :D