diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 772f3ac03..6cb5ee96c 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -327,6 +327,13 @@ def rewrite_asserts(mod): def _saferepr(obj): + """Get a safe repr of an object for assertion error messages + + The assertion formatting (util.format_explanation()) requires + newlines to be escaped since they are a special character for it. + But py.io.saferepr allows newlines, so we need to escape them + here. + """ repr = py.io.saferepr(obj) if py.builtin._istext(repr): t = py.builtin.text