Rewrite the assertions in f, run it, and get the failure message.
(
f, extra_ns: Mapping[str, object] | None = None, *, must_pass: bool = False
)
| 47 | |
| 48 | |
| 49 | def 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 | |
| 76 | class TestAssertionRewrite: |
no test coverage detected