MCPcopy Create free account
hub / github.com/pytest-dev/pytest / getmsg

Function getmsg

testing/test_assertrewrite.py:44–68  ·  view source on GitHub ↗

Rewrite the assertions in f, run it, and get the failure message.

(
    f, extra_ns: Optional[Mapping[str, object]] = None, *, must_pass: bool = False
)

Source from the content-addressed store, hash-verified

42
43
44def getmsg(
45 f, extra_ns: Optional[Mapping[str, object]] = None, *, must_pass: bool = False
46) -> Optional[str]:
47 """Rewrite the assertions in f, run it, and get the failure message."""
48 src = "\n".join(_pytest._code.Code.from_function(f).source().lines)
49 mod = rewrite(src)
50 code = compile(mod, "<test>", "exec")
51 ns: Dict[str, object] = {}
52 if extra_ns is not None:
53 ns.update(extra_ns)
54 exec(code, ns)
55 func = ns[f.__name__]
56 try:
57 func() # type: ignore[operator]
58 except AssertionError:
59 if must_pass:
60 pytest.fail("shouldn't have raised")
61 s = str(sys.exc_info()[1])
62 if not s.startswith("assert"):
63 return "AssertionError: " + s
64 return s
65 else:
66 if not must_pass:
67 pytest.fail("function didn't raise at all")
68 return None
69
70
71class TestAssertionRewrite:

Callers 15

test_nameMethod · 0.85
test_boolopMethod · 0.85
test_unary_opMethod · 0.85
test_binary_opMethod · 0.85
test_boolop_percentMethod · 0.85
test_callMethod · 0.85
test_attributeMethod · 0.85
test_comparisonsMethod · 0.85

Calls 6

rewriteFunction · 0.85
sourceMethod · 0.80
from_functionMethod · 0.80
updateMethod · 0.80
failMethod · 0.80
funcFunction · 0.70

Tested by

no test coverage detected