diff --git a/changelog/5478.bugfix.rst b/changelog/5478.bugfix.rst new file mode 100644 index 000000000..b9e80a88e --- /dev/null +++ b/changelog/5478.bugfix.rst @@ -0,0 +1 @@ +Use safe_str to serialize Exceptions in pytest.raises diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 8c73ccc6a..e621f3ee0 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -572,8 +572,9 @@ class ExceptionInfo(object): raised. """ __tracebackhide__ = True - if not re.search(regexp, str(self.value)): - assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value) + value = safe_str(self.value) + if not re.search(regexp, value): + assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, value) return True diff --git a/testing/python/raises.py b/testing/python/raises.py index cd463d74b..db34c6624 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -278,3 +278,7 @@ class TestRaises(object): with pytest.raises(CrappyClass()): pass assert "via __class__" in excinfo.value.args[0] + + def test_u(self): + with pytest.raises(AssertionError, match=u"\u2603"): + assert False, u"\u2603"