- 
                Notifications
    You must be signed in to change notification settings 
- Fork 300
Closed
Labels
Milestone
Description
pytest has built-in support for rather detailed assert messages. Allowing the testwriter to just use plain asserts:
assert 1 == 2
instead of
if 1 == 2:
     raise check50.Failure("1 does not equal 2")
To do this pytest rewrites part of the code and as it turns out that functionality is reasonably separate from pytests' core. With inspiration from their own test suite:
echo "assert 1 == 2" > bar.py
import ast
from _pytest.assertion.rewrite import rewrite_asserts
def rewrite(src: str) -> ast.Module:
    tree = ast.parse(src)
    rewrite_asserts(tree, src.encode())
    return tree
src = open("bar.py").read()
mod = rewrite(src)
code = compile(mod, "<test>", "exec")
namespace = {}
exec(code, namespace)Just a thought, but perhaps it's worth exploring?