From e9668d75b80057441b53c65c84c9a6351b6c87cb Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 19 Oct 2016 08:57:24 +0200 Subject: [PATCH] turn RecordedWarning into a namedtuple fixes #2013 --- _pytest/recwarn.py | 15 +++++---------- testing/test_recwarn.py | 3 +++ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/_pytest/recwarn.py b/_pytest/recwarn.py index a89474c036a..0f6f93571c9 100644 --- a/_pytest/recwarn.py +++ b/_pytest/recwarn.py @@ -1,5 +1,4 @@ """ recording warnings during test function execution. """ - import inspect import _pytest._code @@ -7,6 +6,7 @@ import sys import warnings import pytest +from collections import namedtuple @pytest.yield_fixture @@ -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. diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 87e5846c2b0..36be5d0d25a 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -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)