-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use pytest's plain asserts #228
Comments
How do they generate the error messages? We can always add "AssertionError" to the list of exceptions we catch in the check decorator and do a manual conversion fo I guess I'm not necessarily opposed to this, but I'm also not sure it adds much. |
The default try:
assert 1 == 2
except AssertionError as e:
print(e.args) # prints () pytest however puts the actual assertion into Allowing you to do the following: import ast
from _pytest.assertion.rewrite import rewrite_asserts
from _pytest.assertion import util
def custom_assert_handler(left, right, op):
print(f"left: {left}, right: {right}, op:{op}")
util._reprcompare = custom_assert_handler
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) Through this we could quite reasonably intercept different operators and types to provide student-friendly output, without putting all the burden on the checkwriter. Some low hanging fruit perhaps:
|
pytest has built-in support for rather detailed
assert
messages. Allowing the testwriter to just use plainassert
s:instead of
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:
Just a thought, but perhaps it's worth exploring?
The text was updated successfully, but these errors were encountered: