https://github.com/okorolev/pytest-ast-transformer
AST Transformer for tests integrated with py.test.
https://github.com/okorolev/pytest-ast-transformer
ast ast-transformations debug magic pytest pytest-plugin python3 refactoring testing
Last synced: about 2 months ago
JSON representation
AST Transformer for tests integrated with py.test.
- Host: GitHub
- URL: https://github.com/okorolev/pytest-ast-transformer
- Owner: okorolev
- Created: 2018-10-09T14:03:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-04T16:50:01.000Z (about 6 years ago)
- Last Synced: 2025-03-26T00:41:24.470Z (3 months ago)
- Topics: ast, ast-transformations, debug, magic, pytest, pytest-plugin, python3, refactoring, testing
- Language: Python
- Homepage:
- Size: 40 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
[](https://travis-ci.org/okorolev/pytest-ast-transformer)
[](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 astfrom pytest_ast_transformer.ast_transformer import PytestTransformer
def my_assert(test_result, msg=''):
print(f'my assert: {test_result} {msg}')
assert test_result, msgclass 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())
```