Skip to content

Commit

Permalink
Merge pull request #2014 from RonnyPfannschmidt/warning-record-namedt…
Browse files Browse the repository at this point in the history
…uple

turn RecordedWarning into a namedtuple
  • Loading branch information
nicoddemus authored Nov 1, 2016
2 parents 377e649 + b3c337d commit d49e9e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ Changes
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
.. _@fushi: https://github.com/fushi
.. _@mattduck: https://github.com/mattduck

.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013


3.0.4.dev
Expand Down
15 changes: 5 additions & 10 deletions _pytest/recwarn.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
""" recording warnings during test function execution. """

import inspect

import _pytest._code
import py
import sys
import warnings
import pytest
from collections import namedtuple


@pytest.yield_fixture
Expand Down Expand Up @@ -110,15 +110,10 @@ def warns(expected_warning, *args, **kwargs):
return func(*args[1:], **kwargs)


class RecordedWarning(object):
def __init__(self, message, category, filename, lineno, file, line):
self.message = message
self.category = category
self.filename = filename
self.lineno = lineno
self.file = file
self.line = line

RecordedWarning = namedtuple('RecordedWarning', (
'message', 'category', 'filename', 'lineno', 'file', 'line',
))


class WarningsRecorder(object):
"""A context manager to record raised warnings.
Expand Down
3 changes: 3 additions & 0 deletions doc/en/recwarn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ Each recorded warning has the attributes ``message``, ``category``,
class of the warning. The ``message`` is the warning itself; calling
``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::
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
differently; see :ref:`ensuring_function_triggers`.
Expand Down
3 changes: 3 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ def test_record(self):
assert len(record) == 1
assert str(record[0].message) == "user"

print(repr(record[0]))
assert str(record[0].message) in repr(record[0])

def test_record_only(self):
with pytest.warns(None) as record:
warnings.warn("user", UserWarning)
Expand Down

0 comments on commit d49e9e5

Please sign in to comment.