Merge pull request #2014 from RonnyPfannschmidt/warning-record-namedtuple

turn RecordedWarning into a namedtuple
This commit is contained in:
Bruno Oliveira 2016-11-01 19:24:44 -02:00 committed by GitHub
commit d49e9e5562
4 changed files with 15 additions and 10 deletions

View File

@ -28,6 +28,9 @@ Changes
to ``io.UnsupportedOperation``. Thanks `@vlad-dragos`_ for the PR. to ``io.UnsupportedOperation``. Thanks `@vlad-dragos`_ for the PR.
* fix `#2013`_: turn RecordedWarning into namedtupe,
to give it a comprehensible repr while preventing unwarranted modification
.. _@davidszotten: https://github.com/davidszotten .. _@davidszotten: https://github.com/davidszotten
.. _@fushi: https://github.com/fushi .. _@fushi: https://github.com/fushi
.. _@mattduck: https://github.com/mattduck .. _@mattduck: https://github.com/mattduck
@ -35,6 +38,7 @@ Changes
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512 .. _#1512: https://github.com/pytest-dev/pytest/issues/1512
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874 .. _#1874: https://github.com/pytest-dev/pytest/pull/1874
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952 .. _#1952: https://github.com/pytest-dev/pytest/pull/1952
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013
3.0.4.dev 3.0.4.dev

View File

@ -1,5 +1,4 @@
""" recording warnings during test function execution. """ """ recording warnings during test function execution. """
import inspect import inspect
import _pytest._code import _pytest._code
@ -7,6 +6,7 @@ import py
import sys import sys
import warnings import warnings
import pytest import pytest
from collections import namedtuple
@pytest.yield_fixture @pytest.yield_fixture
@ -110,15 +110,10 @@ def warns(expected_warning, *args, **kwargs):
return func(*args[1:], **kwargs) return func(*args[1:], **kwargs)
class RecordedWarning(object): RecordedWarning = namedtuple('RecordedWarning', (
def __init__(self, message, category, filename, lineno, file, line): 'message', 'category', 'filename', 'lineno', 'file', 'line',
self.message = message ))
self.category = category
self.filename = filename
self.lineno = lineno
self.file = file
self.line = line
class WarningsRecorder(object): class WarningsRecorder(object):
"""A context manager to record raised warnings. """A context manager to record raised warnings.

View File

@ -92,6 +92,9 @@ Each recorded warning has the attributes ``message``, ``category``,
class of the warning. The ``message`` is the warning itself; calling class of the warning. The ``message`` is the warning itself; calling
``str(message)`` will return the actual message of the warning. ``str(message)`` will return the actual message of the warning.
.. note::
:class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1
.. note:: .. note::
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated ``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
differently; see :ref:`ensuring_function_triggers`. differently; see :ref:`ensuring_function_triggers`.

View File

@ -203,6 +203,9 @@ class TestWarns(object):
assert len(record) == 1 assert len(record) == 1
assert str(record[0].message) == "user" assert str(record[0].message) == "user"
print(repr(record[0]))
assert str(record[0].message) in repr(record[0])
def test_record_only(self): def test_record_only(self):
with pytest.warns(None) as record: with pytest.warns(None) as record:
warnings.warn("user", UserWarning) warnings.warn("user", UserWarning)