-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Integrate pytest warnings #2072
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
Merged
RonnyPfannschmidt
merged 33 commits into
pytest-dev:features
from
nicoddemus:integrate-pytest-warnings
Mar 22, 2017
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
6b135c8
Initial commit.
fschulze b9c4ecf
Add MANIFEST.in.
fschulze 3feee0c
Prepare 0.1 release.
fschulze bc5a8c7
Fix version in readme.
fschulze bc94a51
Add a warning option which does not escape its arguments.
Carreau 28621b0
Merge pull request #2 from Carreau/filter-regex
fschulze f229b57
Bump version, add changelog entry and move stuff around for added cov…
fschulze ce13806
Prepare pytest-warnings 0.2.0.
fschulze 6ec0c3f
Bump.
fschulze e31421a
Moving all stuff to a subdirectory to try to retain history
nicoddemus 9db32ae
Merge c:\pytest-warnings\ into integrate-pytest-warnings
nicoddemus 1da1906
Rename code to _pytest.warnings and delete old files from the repository
nicoddemus 26ca5a7
Add tests and integrated the original code into the core
nicoddemus bd343ef
Merge remote-tracking branch 'upstream/features' into integrate-pytes…
nicoddemus a7643a5
Merge branch 'features' into integrate-pytest-warnings
nicoddemus 82785fc
Use warnings.catch_warnings instead of WarningsRecorder
nicoddemus e24081b
Change warning output
nicoddemus de09023
Also capture warnings during setup/teardown
nicoddemus bddb922
Rename internal option to disable_warnings
nicoddemus 272afa9
Display node ids and the warnings generated by it
nicoddemus 337f891
Fixed tests
nicoddemus 0baed78
Merge remote-tracking branch 'upstream/features' into integrate-pytes…
nicoddemus be5db6f
Capture warnings around the entire runtestprotocol
nicoddemus 7819409
Improve warning representation in terminal plugin and fix tests
nicoddemus 9f85584
Merge remote-tracking branch 'upstream/features' into integrate-pytes…
nicoddemus 3373e02
Add __future__ imports to warnings module
nicoddemus d027f76
Avoid displaying the same warning multiple times for an item
nicoddemus fa56114
Clean up warnings generated by pytest's own suite
nicoddemus eabe3ee
Add docs for the warnings functionality
nicoddemus 916d272
Fix test on linux
nicoddemus 2c73074
Fix errors related to warnings raised by xdist
nicoddemus 74b54ac
Fix errors related to warnings raised on pypy test environment
nicoddemus 0c1c258
Add CHANGELOG entry
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import warnings | ||
from contextlib import contextmanager | ||
|
||
import pytest | ||
|
||
|
||
def _setoption(wmod, arg): | ||
""" | ||
Copy of the warning._setoption function but does not escape arguments. | ||
""" | ||
parts = arg.split(':') | ||
if len(parts) > 5: | ||
raise wmod._OptionError("too many fields (max 5): %r" % (arg,)) | ||
while len(parts) < 5: | ||
parts.append('') | ||
action, message, category, module, lineno = [s.strip() | ||
for s in parts] | ||
action = wmod._getaction(action) | ||
category = wmod._getcategory(category) | ||
if lineno: | ||
try: | ||
lineno = int(lineno) | ||
if lineno < 0: | ||
raise ValueError | ||
except (ValueError, OverflowError): | ||
raise wmod._OptionError("invalid lineno %r" % (lineno,)) | ||
else: | ||
lineno = 0 | ||
wmod.filterwarnings(action, message, category, module, lineno) | ||
|
||
|
||
def pytest_addoption(parser): | ||
group = parser.getgroup("pytest-warnings") | ||
group.addoption( | ||
'-W', '--pythonwarnings', action='append', | ||
help="set which warnings to report, see -W option of python itself.") | ||
parser.addini("filterwarnings", type="linelist", | ||
help="Each line specifies warning filter pattern which would be passed" | ||
"to warnings.filterwarnings. Process after -W and --pythonwarnings.") | ||
|
||
|
||
@contextmanager | ||
def catch_warnings_for_item(item): | ||
""" | ||
catches the warnings generated during setup/call/teardown execution | ||
of the given item and after it is done posts them as warnings to this | ||
item. | ||
""" | ||
args = item.config.getoption('pythonwarnings') or [] | ||
inifilters = item.config.getini("filterwarnings") | ||
with warnings.catch_warnings(record=True) as log: | ||
warnings.simplefilter('once') | ||
for arg in args: | ||
warnings._setoption(arg) | ||
|
||
for arg in inifilters: | ||
_setoption(warnings, arg) | ||
|
||
yield | ||
|
||
for warning in log: | ||
msg = warnings.formatwarning( | ||
warning.message, warning.category, | ||
warning.filename, warning.lineno, warning.line) | ||
item.warn("unused", msg) | ||
|
||
|
||
@pytest.hookimpl(hookwrapper=True) | ||
def pytest_runtest_protocol(item): | ||
with catch_warnings_for_item(item): | ||
yield |
This file contains hidden or 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ Full pytest documentation | |
monkeypatch | ||
tmpdir | ||
capture | ||
recwarn | ||
warnings | ||
doctest | ||
mark | ||
skipping | ||
|
This file contains hidden or 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
Oops, something went wrong.
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.
that must have been so puzzleing