Skip to content

pytest_assertion_pass strange results #5510

@nicoddemus

Description

@nicoddemus

(From #5509 (comment))

It seems this patch fixes the problem:

diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py
index 8810c156c..28091c115 100644
--- a/src/_pytest/assertion/rewrite.py
+++ b/src/_pytest/assertion/rewrite.py
@@ -750,7 +750,7 @@ class AssertionRewriter(ast.NodeVisitor):

             # Passed
             fmt_pass = self.helper("_format_explanation", msg)
-            orig = astor.to_source(assert_.test).rstrip("\n").lstrip("(").rstrip(")")
+            orig = astor.to_source(assert_.test).strip()
             hook_call_pass = ast.Expr(
                 self.helper(
                     "_call_assertion_pass",
item = <Function test>
lineno = 5
orig = f()
expl = 1
 +  where 1 = f()

The test we have for it fails, but just because it expects the parenthesis to be stripped:

E           and: '>       raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno))'
E           and: 'E       Exception: Assertion Passed: (a + b == c + d) (1 + 2) == (3 + 0) at line 7'
...
E       remains unmatched: '*Assertion Passed: a + b == c + d (1 + 2) == (3 + 0) at line 7*'

So I think we should avoid trying to strip the results ourselves. I will open a PR.

cc @Sup3rGeo @asottile

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions