Initial commit.

This commit is contained in:
Florian Schulze 2016-06-22 12:21:51 +02:00
commit 6b135c83be
8 changed files with 193 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/.cache/
/.tox/
/bin/
/include/
/lib/
/pip-selfcheck.json

19
LICENSE Executable file
View File

@ -0,0 +1,19 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

37
README.rst Normal file
View File

@ -0,0 +1,37 @@
pytest-warnings
===============
py.test plugin to list Python warnings in pytest report
Usage
-----
install via::
pip install pytest-warnings
if you then type::
py.test -rw
any warnings in your code are reported in the pytest report.
You can use the ``-W`` option or ``--pythonwarnings`` exactly like for the ``python`` executable.
The following example ignores all warnings, but prints DeprecationWarnings once per occurrence::
py.test -rw -W ignore -W once::DeprecationWarning
You can also turn warnings into actual errors::
py.test -W error
Changes
=======
0.1 - Unreleased
----------------
- Initial release.
[fschulze (Florian Schulze)]

50
pytest_warnings.py Normal file
View File

@ -0,0 +1,50 @@
from _pytest.recwarn import RecordedWarning, WarningsRecorder
import inspect
import os
import pytest
import warnings
def pytest_addoption(parser):
group = parser.getgroup("pytest-warnings")
group.addoption(
'-W', '--pythonwarnings', action='append',
help="set which warnings to report, see ...")
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
wrec = WarningsRecorder()
def showwarning(message, category, filename, lineno, file=None, line=None):
frame = inspect.currentframe()
if '/_pytest/recwarn' in frame.f_back.f_code.co_filename:
# we are in test recorder, so this warning is already handled
return
wrec._list.append(RecordedWarning(
message, category, filename, lineno, file, line))
# still perform old showwarning functionality
wrec._showwarning(
message, category, filename, lineno, file=file, line=line)
args = item.config.getoption('pythonwarnings') or []
with wrec:
_showwarning = wrec._showwarning
warnings.showwarning = showwarning
wrec._module.simplefilter('once')
for arg in args:
wrec._module._setoption(arg)
yield
wrec._showwarning = _showwarning
for warning in wrec.list:
msg = warnings.formatwarning(
warning.message, warning.category,
os.path.relpath(warning.filename), warning.lineno, warning.line)
fslocation = getattr(item, "location", None)
if fslocation is None:
fslocation = getattr(item, "fspath", None)
else:
fslocation = "%s:%s" % fslocation[:2]
fslocation = "in %s the following warning was recorded:\n" % fslocation
item.config.warn("W0", msg, fslocation=fslocation)

17
setup.py Normal file
View File

@ -0,0 +1,17 @@
from setuptools import setup
setup(
name="pytest-warnings",
description='pytest plugin to list Python warnings in pytest report',
long_description=open("README.rst").read(),
license="MIT license",
version='0.1.0',
author='Florian Schulze',
author_email='florian.schulze@gmx.net',
url='https://github.com/fschulze/pytest-warnings',
py_modules=["pytest_warnings"],
entry_points={'pytest11': ['pytest_warnings = pytest_warnings']},
install_requires=['pytest'],
classifiers=[
"Framework :: Pytest"])

27
test_warnings.py Normal file
View File

@ -0,0 +1,27 @@
import pytest
import warnings
def test_warnings():
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
def test_warnings1():
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
def test_warn():
with pytest.warns(DeprecationWarning):
warnings.warn("Bar", DeprecationWarning)

20
test_warnings2.py Normal file
View File

@ -0,0 +1,20 @@
def test_warnings():
import warnings
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
def test_warnings1():
import warnings
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", RuntimeWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)
warnings.warn("Foo", DeprecationWarning)

17
tox.ini Normal file
View File

@ -0,0 +1,17 @@
[tox]
envlist = py27,py33,py34,py35
[testenv]
usedevelop = true
deps =
pytest
pytest-flakes
pytest-pep8
coverage
commands =
{envbindir}/py.test --junitxml={envlogdir}/junit-{envname}.xml {posargs}
[pytest]
addopts = --flakes --pep8
pep8ignore = E501
norecursedirs = bin lib include Scripts .*