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 | """Rewrite the assertions in f, run it, and get the failure message.""" |
| 53 | src = "\n".join(_pytest._code.Code.from_function(f).source().lines) |
| 54 | mod = rewrite(src) |
| 55 | code = compile(mod, "<test>", "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() # type: ignore[operator] |
| 63 | except AssertionError: |
| 64 | if must_pass: |
| 65 | pytest.fail("shouldn't have raised") |
| 66 | s = str(sys.exc_info()[1]) |
| 67 | if not s.startswith("assert"): |
| 68 | return "AssertionError: " + s |
| 69 | return s |
| 70 | else: |
| 71 | if not must_pass: |
| 72 | pytest.fail("function didn't raise at all") |
| 73 | return None |
| 74 | |
| 75 | |
| 76 | class TestAssertionRewrite: |
no test coverage detected
searching dependent graphs…