Skip to content

Use pytest's plain asserts #228

Description

@Jelleas

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

4.xIssues relating to check50 4.xenhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions