Awesome `auto_pytest_mg` is a Python cli/package created with https://github.com/TezRomacH/python-package-template
Project description
auto_pytest_mg (Automatic pytest Mock Generator)
- GitHub: https://github.com/rozelie/auto_pytest_mg
- GitHub Releases: https://github.com/rozelie/auto_pytest_mg/releases
- PyPi: https://pypi.org/project/auto-pytest-mg/
auto_pytest_mg parses the AST of an input python file to generate a new test file with boilerplate
test functions. Rendered tests include the mocker
and mg
fixtures which are available via the
pytest-mock and pytest-mocker-generator
packages, respectively.
Usage
# install the package
pip install auto_pytest_mg
# go to project's source root
cd my_project
# pass the file to generate tests for
auto_pytest_mg my_project/my_file.py
Example
Source file located at my_project/my_file.py
# my_project/my_file.py
from dataclasses import dataclass
@dataclass
class DataClass:
a: str
b: int
@property
def property_(self) -> None:
...
def method(self) -> None:
...
def method_with_args(self, a: int, b: str) -> None:
...
def a_function():
...
Running auto_pytest_mg my_project/my_file.py
then generates my_project/test_my_file.py
:
# my_project/test_my_file.py
import pytest
from my_project.my_file import a_function, DataClass
@pytest.fixture
def data_class(mocker):
a = mocker.MagicMock()
b = mocker.MagicMock()
return DataClass(a=a, b=b)
class TestDataClass:
def test__init__(self, mocker):
a = mocker.MagicMock()
b = mocker.MagicMock()
return DataClass(a=a, b=b)
def test_property_(self, mocker, mg, data_class):
mg.generate_uut_mocks_with_asserts(data_class.property_)
result = data_class.property_
def test_method(self, mocker, mg, data_class):
mg.generate_uut_mocks_with_asserts(data_class.method)
result = data_class.method()
def test_method_with_args(self, mocker, mg, data_class):
a = mocker.MagicMock()
b = mocker.MagicMock()
mg.generate_uut_mocks_with_asserts(data_class.method_with_args)
result = data_class.method_with_args(a=a, b=b)
def test_a_function(mocker, mg):
mg.generate_uut_mocks_with_asserts(a_function)
result = a_function()
Development
Makefile usage
Makefile
contains a lot of functions for faster development.
1. Download and remove Poetry
To download and install Poetry run:
make poetry-download
To uninstall
make poetry-remove
2. Install all dependencies and pre-commit hooks
Install requirements:
make install
Pre-commit hooks coulb be installed after git init
via
make pre-commit-install
3. Codestyle
Automatic formatting uses pyupgrade
, isort
and black
.
make format
Codestyle checks only, without rewriting files:
make check-format
Note:
check-format
usesisort
,black
anddarglint
library
Update all dev libraries to the latest version using one comand
make update-dev-deps
4. Code security
make check-safety
This command launches Poetry
integrity checks as well as identifies security issues with Safety
and Bandit
.
make check-safety
5. Type checks
Run mypy
static type checker
make mypy
6. Tests with coverage badges
Run pytest
make test
7. All linters
Of course there is a command to rule run all linters in one:
make lint
the same as:
make test && make check-format && make mypy && make check-safety
8. Docker
make docker-build
which is equivalent to:
make docker-build VERSION=latest
Remove docker image with
make docker-remove
More information about docker.
9. Cleanup
Delete pycache files
make pycache-remove
Remove package build
make build-remove
Delete .DS_STORE files
make dsstore-remove
Remove .mypycache
make mypycache-remove
Or to remove all above run:
make cleanup
Build and Release
Building a new version of the application contains steps:
- Bump the version of your package
poetry version {(major|minor|patch)}
git add pyproject.toml
- Commit and push
git commit -m "Updating to version: v{version}"
- Create a release on GitHub
poetry publish --build
🛡 License
This project is licensed under the terms of the MIT
license. See LICENSE for more details.
Credits
This project was generated with python-package-template
Project details
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
Hashes for auto_pytest_mg-0.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13cbf4a94229951a87f0b9496393618cb7b33f97c7a3593a926eb2e38a5cb790 |
|
MD5 | 9c1dd04e6c2639b7592272175afe73dc |
|
BLAKE2b-256 | 9a576ad63060729ba0732f8a2051f33a90fef6fab86adf3cd1ec107bafef3062 |