Skip to main content

A plugin for pytest devs to view how assertion rewriting recodes the AST

Project description

See Build Status on Travis CI See Build Status on AppVeyor

A plugin for pytest devs to view how assertion rewriting recodes the AST


Pytest rewrites the AST (abstract syntax tree) of your tests, for the purpose of displaying the subexpressions involved in your assert statements. This plugin converts that rewritten AST back to Python source, and displays it.


You can install “pytest-ast-back-to-python” via pip from PyPI:

$ pip install pytest-ast-back-to-python


py.test --show-ast-as-python


Take a trivial test like:

def test_simple():
    a = 1
    b = 2
    assert a + b == 3

View the rewritten AST as Python like this:

$ py.test --show-ast-as-python
======================================== test session starts ========================================
plugins: ast-back-to-python-0.1.0, cov-2.2.1
collected 1 items .
======================================== Rewritten AST as Python ========================================
import builtins as @py_builtins
import _pytest.assertion.rewrite as @pytest_ar

def test_simple():
    a = 1
    b = 2
    @py_assert2 = a + b
    @py_assert4 = 3
    @py_assert3 = @py_assert2 == @py_assert4
    if @py_assert3 is None:
        from _pytest.warning_types import PytestAssertRewriteWarning
        from warnings import warn_explicit
        warn_explicit(PytestAssertRewriteWarning('asserting the value None, please use "assert is None"'), category=None, filename='/home/tom/.virtualenvs/tmp-483cf04ecc31dda8/', lineno=4)
    if not @py_assert3:
        @py_format6 = @pytest_ar._call_reprcompare(('==',), (@py_assert3,), ('(%(py0)s + %(py1)s) == %(py5)s',), (@py_assert2, @py_assert4)) % {'py0': @pytest_ar._saferepr(a) if 'a' in @py_builtins.locals() or @pytest_ar._should_repr_global_name(a) else 'a', 'py1': @pytest_ar._saferepr(b) if 'b' in @py_builtins.locals() or @pytest_ar._should_repr_global_name(b) else 'b', 'py5': @pytest_ar._saferepr(@py_assert4)}
        @py_format8 = ('' + 'assert %(py7)s') % {'py7': @py_format6}
        raise AssertionError(@pytest_ar._format_explanation(@py_format8))
    @py_assert2 = @py_assert3 = @py_assert4 = None


Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.


Distributed under the terms of the BSD-3 license, “pytest-ast-back-to-python” is free and open source software

This Pytest plugin was generated with Cookiecutter along with @hackebrot’s Cookiecutter-pytest-plugin template.


If you encounter any problems, please file an issue along with a detailed description.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-ast-back-to-python-1.2.1.tar.gz (15.8 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page