Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

No project description provided

Project description

[![Build Status](https://travis-ci.org/okorolev/pytest-ast-transformer.svg?branch=master)](https://travis-ci.org/okorolev/pytest-ast-transformer)
[![image](https://img.shields.io/pypi/v/pytest-ast-transformer.svg)](https://pypi.org/project/pytest-ast-transformer/)

## About
AST Transformer integrated with py.test.

Useful for debug, refactoring, 'clean asserts' (see [examples/replace_asserts](examples/replace_asserts))

Support options
---------------
| Options | Description |
| ----------- | ----------- |
| `--show-code` | show generated code |
| `--disable-transforms` | disable all ast transformers |

## Install
```bash
pip install pytest-ast-transformer
```

## Usage

* Write ast transformer
```python
# transformer.py
import ast

from pytest_ast_transformer.ast_transformer import PytestTransformer


def my_assert(test_result, msg=''):
print(f'my assert: {test_result} {msg}')
assert test_result, msg


class AssertTransformer(PytestTransformer):
context = {
'my_assert': my_assert
}

def visit_Assert(self, node: ast.Assert) -> ast.Expr:
func_name = ast.Name(id='my_assert', ctx=ast.Load())
call_func = ast.Call(func=func_name, args=[node.test, node.msg], keywords=[])
expr = ast.Expr(value=call_func)

return ast.fix_missing_locations(expr)
```
* Register new ast transformer
```python
# conftest.py
from pytest_ast_transformer.ast_manager import ASTManager
from tests.transformer import AssertTransformer


def pytest_register_ast_transformer(ast_manager: ASTManager):
ast_manager.add_transformer(AssertTransformer())
```


Project details


Download files

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

Files for pytest-ast-transformer, version 1.0.3
Filename, size File type Python version Upload date Hashes
Filename, size pytest_ast_transformer-1.0.3-py2.py3-none-any.whl (11.5 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size pytest-ast-transformer-1.0.3.tar.gz (6.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page