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)
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 |
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())
```
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 |
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-ast-transformer-1.0.1.tar.gz
.
File metadata
- Download URL: pytest-ast-transformer-1.0.1.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 869f43bb2a1e867974631b09f510c459359e2e88320c8c107b163dbb4037da0c |
|
MD5 | ddc31bc0482d64432dccd61890a878b7 |
|
BLAKE2b-256 | 5252f3e34054a1078297050728d94c245e9bae66b7e4f26dd19bd922e5277873 |
File details
Details for the file pytest_ast_transformer-1.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: pytest_ast_transformer-1.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8056e55e64ebb002df172f61fc027d7f6179c3db629b690a36dd413e88fb187e |
|
MD5 | 37991fdf3a7616f5cc73333dfd6133d0 |
|
BLAKE2b-256 | 991f58004038874c221a1123c160746683b292fea1a25fa93c0c60c96b878af5 |