From 6b135c83be8cef769dbc860a4706a95603d01d09 Mon Sep 17 00:00:00 2001 From: Florian Schulze Date: Wed, 22 Jun 2016 12:21:51 +0200 Subject: [PATCH] Initial commit. --- .gitignore | 6 ++++++ LICENSE | 19 ++++++++++++++++++ README.rst | 37 ++++++++++++++++++++++++++++++++++ pytest_warnings.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 17 ++++++++++++++++ test_warnings.py | 27 +++++++++++++++++++++++++ test_warnings2.py | 20 +++++++++++++++++++ tox.ini | 17 ++++++++++++++++ 8 files changed, 193 insertions(+) create mode 100644 .gitignore create mode 100755 LICENSE create mode 100644 README.rst create mode 100644 pytest_warnings.py create mode 100644 setup.py create mode 100644 test_warnings.py create mode 100644 test_warnings2.py create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..dac3fe1aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.cache/ +/.tox/ +/bin/ +/include/ +/lib/ +/pip-selfcheck.json diff --git a/LICENSE b/LICENSE new file mode 100755 index 000000000..8a30978e3 --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/README.rst b/README.rst new file mode 100644 index 000000000..02389db73 --- /dev/null +++ b/README.rst @@ -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)] diff --git a/pytest_warnings.py b/pytest_warnings.py new file mode 100644 index 000000000..1813cf519 --- /dev/null +++ b/pytest_warnings.py @@ -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) diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..fd478664c --- /dev/null +++ b/setup.py @@ -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"]) diff --git a/test_warnings.py b/test_warnings.py new file mode 100644 index 000000000..62167992f --- /dev/null +++ b/test_warnings.py @@ -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) diff --git a/test_warnings2.py b/test_warnings2.py new file mode 100644 index 000000000..d4c9be0ea --- /dev/null +++ b/test_warnings2.py @@ -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) diff --git a/tox.ini b/tox.ini new file mode 100644 index 000000000..977819b65 --- /dev/null +++ b/tox.ini @@ -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 .*