Use pytest_configure
to patch pytest/_pytest/assertion/rewrite.py::rewrite_asserts
Note: I'm now in the process of converting this guide to a pytest plugin.
git clone [email protected]:/c3537c2f2b2b8172f83e.git display_ast_rewrite
cd display_ast_rewrite
pip install -r requirements.txt
Note: -s
is required to see rewritten AST output, even for failing tests, because the output comes while
tests are being collected and rewritten, not during the execution of a test. So pytest's "show output upon
fail" cannot help here.
py.test -s test_simple.py
def test_simple():
a = 1
b = 2
assert a == b
import __builtin__ as @py_builtins
import _pytest.assertion.rewrite as @pytest_ar
def test_simple():
a = 1
b = 2
@py_assert1 = a == b
if not @py_assert1:
@py_format3 = @pytest_ar._call_reprcompare(('==',), (@py_assert1,),
('%(py0)s == %(py2)s',), (a, b)) % {'py0': @pytest_ar._saferepr
(a) if 'a' in @py_builtins.locals() or @pytest_ar.
_should_repr_global_name(a) else 'a', 'py2': @pytest_ar.
_saferepr(b) if 'b' in @py_builtins.locals() or @pytest_ar.
_should_repr_global_name(b) else 'b'}
@py_format5 = ('' + 'assert %(py4)s') % {'py4': @py_format3}
raise AssertionError(@pytest_ar._format_explanation(@py_format5))
@py_assert1 = None