Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

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

Project Description

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_assert0 = 1
    @py_assert2 = 2
    @py_assert4 = @py_assert0 + @py_assert2
    @py_assert6 = 3
    @py_assert5 = @py_assert4 == @py_assert6
    if not @py_assert5:
        @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)}
        @py_format10 = ('' + 'assert %(py9)s') % {'py9': @py_format8}
        raise AssertionError(@pytest_ar._format_explanation(@py_format10))
    @py_assert0 = @py_assert2 = @py_assert4 = @py_assert5 = @py_assert6 = 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.

Release History

This version
History Node


History Node


History Node


History Node


Download Files

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

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(14.6 kB) Copy SHA256 Hash SHA256
Wheel 3.5 Oct 19, 2017
(15.4 kB) Copy SHA256 Hash SHA256
Source None Oct 19, 2017

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers