From c39689da41eee6656d2956d20c09b6725151583d Mon Sep 17 00:00:00 2001 From: wanghui Date: Thu, 25 May 2017 17:59:42 +0800 Subject: [PATCH] Correct warnings with unicode message. --- _pytest/warnings.py | 4 +++- testing/test_warnings.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/_pytest/warnings.py b/_pytest/warnings.py index bfa2b0087..7596b0098 100644 --- a/_pytest/warnings.py +++ b/_pytest/warnings.py @@ -5,6 +5,8 @@ from contextlib import contextmanager import pytest +from _pytest import compat + def _setoption(wmod, arg): """ @@ -62,7 +64,7 @@ def catch_warnings_for_item(item): for warning in log: msg = warnings.formatwarning( - warning.message, warning.category, + compat.safe_str(warning.message), warning.category, warning.filename, warning.lineno, warning.line) item.warn("unused", msg) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index e0baed8d1..85fda430e 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -1,3 +1,6 @@ +# -*- coding: utf8 -*- +from __future__ import unicode_literals + import pytest @@ -106,3 +109,29 @@ def test_ignore(testdir, pyfile_with_warnings, method): ]) assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() + + +def test_unicode(testdir, pyfile_with_warnings): + testdir.makepyfile(''' + # -*- coding: utf8 -*- + import warnings + import pytest + + + @pytest.fixture + def fix(): + warnings.warn(u"测试") + yield + + def test_func(fix): + pass + ''') + result = testdir.runpytest() + result.stdout.fnmatch_lines([ + '*== %s ==*' % WARNINGS_SUMMARY_HEADER, + + '*test_unicode.py:8: UserWarning: \u6d4b\u8bd5', + '*warnings.warn(u"\u6d4b\u8bd5")', + '* 1 passed, 1 warnings*', + ]) +