Skip to main content

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

Metadata

Release files for auto-pytest-mg 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for auto-pytest-mg 0.5.0
File Size Uploaded
auto_pytest_mg-0.5.0.tar.gz 11.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for auto-pytest-mg 0.5.0
File Interpreter ABI Platform
auto_pytest_mg-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.8 kB

Release files / auto_pytest_mg-0.5.0.tar.gz

Download URL auto_pytest_mg-0.5.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f03698f77feec86f0de74e1bce24f1b4348e049cb69cd8b13c4b8939ae08a884
BLAKE2b-256 checksum
How to use checksums
270d7197416604892e5d1a81fd9044acad99d1e49ea642fcdb90a1b1a9d44553
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.13 Darwin/21.4.0

Release files / auto_pytest_mg-0.5.0-py3-none-any.whl

Download URL auto_pytest_mg-0.5.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
13cbf4a94229951a87f0b9496393618cb7b33f97c7a3593a926eb2e38a5cb790
BLAKE2b-256 checksum
How to use checksums
9a576ad63060729ba0732f8a2051f33a90fef6fab86adf3cd1ec107bafef3062
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.13 Darwin/21.4.0

Release history Release notifications | RSS feed

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

This release

0.5.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page