diff --git a/changelog/5478.bugfix.rst b/changelog/5478.bugfix.rst new file mode 100644 index 00000000000..d71e526b1a7 --- /dev/null +++ b/changelog/5478.bugfix.rst @@ -0,0 +1 @@ +Use safe_str to serialize Exceptions in pytest.raise diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 8c73ccc6adc..83403cd42f2 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -572,8 +572,9 @@ def match(self, regexp): 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 {!r} not found in {!r}".format(regexp, value) return True diff --git a/testing/python/raises.py b/testing/python/raises.py index cd463d74b07..db34c6624f5 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -278,3 +278,7 @@ def __class__(self): 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"