MCPcopy
hub / github.com/pytest-dev/pytest / getmsg

Function getmsg

testing/test_assertrewrite.py:49–73  ·  testing/test_assertrewrite.py::getmsg

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

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

Source from the content-addressed store, hash-verified

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

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

Tested by

no test coverage detected