<div dir="ltr">To update on this topic, I've now published a pytest plugin to display the rewritten AST as Python:<div><a href="https://github.com/tomviner/pytest-ast-back-to-python">https://github.com/tomviner/pytest-ast-back-to-python</a></div><div><br></div><div>Not sure if it will ever come in handy, but it was interesting to create my first pytest plugin anyway! Keep it in mind as an option, if we get any bugs with assertion rewriting.</div><div><br></div><div>It works like this:</div><div><br></div><div><div>As an example take a trivial test like:</div><div><br></div><div>def test_simple():</div><div>  Â  a = 1</div><div>  Â  b = 2</div><div>  Â  assert 1 + 2 == 3</div><div><br></div><div>View the rewritten AST as Python like this:</div><div><br></div><div>$ py.test --show-ast-as-python test_simple.py</div><div>======================================== test session starts ========================================</div><div>plugins: ast-back-to-python-0.1.0, cov-2.2.1</div><div>collected 1 items</div><div><br></div><div>test_simple.py .</div><div>======================================== Rewritten AST as Python ========================================</div><div>import builtins as @py_builtins</div><div>import _pytest.assertion.rewrite as @pytest_ar</div><div><br></div><div>def test_simple():</div><div>  Â  a = 1</div><div>  Â  b = 2</div><div>  Â  @py_assert0 = 1</div><div>  Â  @py_assert2 = 2</div><div>  Â  @py_assert4 = @py_assert0 + @py_assert2</div><div>  Â  @py_assert6 = 3</div><div>  Â  @py_assert5 = @py_assert4 == @py_assert6</div><div>  Â  if not @py_assert5:</div><div>  Â  Â  Â  @py_format8 = @pytest_ar._call_reprcompare(('==',), (@py_assert5,), ('(%(py1)s + %(py3)s) == %(py7)s',), (@py_assert4, @py_assert6)) % {'py3': @pytest_ar._saferepr(@py_assert2), 'py1': @pytest_ar._saferepr(@py_assert0), 'py7': @pytest_ar._saferepr(@py_assert6)}</div><div>  Â  Â  Â  @py_format10 = ('' + 'assert %(py9)s') % {'py9': @py_format8}</div><div>  Â  Â  Â  raise AssertionError(@pytest_ar._format_explanation(@py_format10))</div><div>  Â  @py_assert0 = @py_assert2 = @py_assert4 = @py_assert5 = @py_assert6 = None</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 2 March 2015 at 09:06, Tom Viner <span dir="ltr"><<a href="mailto:tom@viner.tv" target="_blank">tom@viner.tv</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">One extra note:<div><br></div><div><div>`py.test -s` is required to see rewritten AST output, even for failing tests, because the output comes while</div><div>tests are being collected and rewritten, not during the execution of a test. So pytest's "show output upon</div><div>fail" cannot help here.</div></div><div><br></div><div>If anyone knows how to tie the output to particular tests, that would be great.</div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On 1 March 2015 at 23:01, Tom Viner <span dir="ltr"><<a href="mailto:tom@viner.tv" target="_blank">tom@viner.tv</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks for your ideas Floris, I've managed to get it working with your pytest_configure suggestion.<div><br></div><div>The only extra bit was having to disable reading the cached pyc files that pytest writes, as these skip my monkeypatch.<div><br></div><div>Take a look, it's simple to use:</div><div><a href="https://gist.github.com/tomviner/c3537c2f2b2b8172f83e" target="_blank">https://gist.github.com/tomviner/c3537c2f2b2b8172f83e</a></div><div><br></div><div>If there's any demand, I could easily make this a pip installable pytest plugin.</div><div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On 13 November 2014 at 23:34, Floris Bruynooghe <span dir="ltr"><<a href="mailto:flub@devork.be" target="_blank">flub@devork.be</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi Tom,<br>
<span><br>
On 9 November 2014 19:18, Tom Viner <<a href="mailto:tom@viner.tv" target="_blank">tom@viner.tv</a>> wrote:<br>
> The four options I can think of to gain access to this variable:<br>
> - monkey patch rewrite.rewrite_asserts<br>
> - edit rewrite_asserts and paste in the logging code above:<br>
>  Â  Â -- temporarily, by each developer, on each occasion required<br>
>  Â  Â -- permanently, but only running when a certain debug flag is active<br>
> - expose the rewritten AST via a new hook<br>
><br>
> monkey patch is my preferred option, because if I can make it work, a plugin<br>
> could be written. I just need some help to find an appropriate hook to do<br>
> the monkey patching before pytest gets going with the AssertionRewritingHook<br>
> and calls rewrite_asserts. Without knowledge of an early-running hook, I<br>
> could only get this working by patching rewrite_asserts then calling<br>
> pytest.main myself, see<br>
> <a href="https://gist.github.com/tomviner/13c95cdb1e159028fc0b" target="_blank">https://gist.github.com/tomviner/13c95cdb1e159028fc0b</a><br>
<br>
</span>Would this work by using monkeypatching, but using a method on<br>
_pytest.pytester.TmpTestDir which wraps around inline_run or<br>
inline_runsource?  That would seem like a fairly reasonable approach.<br>
It could simply skip with an appropriate message if the meta lib is<br>
importable and print the reversed python code to stdout for capsys to<br>
collect.<br>
<br>
If you would like to go the full plugin way I believe you just need to<br>
hook in before collection happens.  I imagine pytest_configure would<br>
be a reasonable hook for that, but have to admit I haven't tried it.<br>
<br>
<br>
Regards,<br>
Floris<br>
</blockquote></div><br></div></div></div></div></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div></div>