Skip to main content

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)

Python Version Dependencies Status

Code style: black Coverage Report

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 uses isort, black and darglint 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:

  1. Bump the version of your package poetry version {(major|minor|patch)}
  2. git add pyproject.toml
  3. Commit and push git commit -m "Updating to version: v{version}"
  4. Create a release on GitHub
  5. poetry publish --build

🛡 License

This project is licensed under the terms of the MIT license. See LICENSE for more details.

Credits 🚀 Your next Python package needs a bleeding-edge project structure.

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

auto_pytest_mg-0.5.0.tar.gz (11.9 kB view hashes)

Uploaded Source

Built Distribution

auto_pytest_mg-0.5.0-py3-none-any.whl (10.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page