跳转到主要内容

pytest开发者插件,用于查看断言重写如何重写AST

项目描述

See Build Status on Travis CI See Build Status on AppVeyor

pytest开发者插件,用于查看断言重写如何重写AST


功能

pytest重写测试的AST(抽象语法树),以显示断言语句中涉及的子表达式。此插件将重写的AST转换回Python源代码,并显示它。

安装

您可以通过从PyPI使用pip安装“pytest-ast-back-to-python”

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

用法

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

示例

例如,对一个简单的测试

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

像这样查看重写的AST

$ py.test --show-ast-as-python test_simple.py
======================================== test session starts ========================================
plugins: ast-back-to-python-0.1.0, cov-2.2.1
collected 1 items

test_simple.py .
======================================== 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/test_thing.py', 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

贡献

欢迎贡献。可以使用tox运行测试,请在提交pull request之前确保覆盖率至少保持不变。

许可证

根据BSD-3许可证分发,“pytest-ast-back-to-python”是免费和开源软件

Pytest插件是使用Cookiecutter以及@hackebrotCookiecutter-pytest-plugin模板生成的。

问题

如果您遇到任何问题,请提交一个问题,并附上详细的描述。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

pytest-ast-back-to-python-1.2.1.tar.gz (15.8 kB 查看哈希值)

上传时间

构建分布

pytest_ast_back_to_python-1.2.1-py2.py3-none-any.whl (13.4 kB 查看哈希值)

上传时间 Python 2 Python 3

由...支持