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 fixtures and boilerplate test functions. Rendered tests include the mocker and mg fixtures which are available via the pytest-mock and pytest-mocker-generator packages, respectively.

Note that this packages is a static analysis tool and will not execute any of your code.

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
import requests

class MyClass:

    def __init__(self, a: int):
        self.a = a

    def method(self) -> int:
        return self.a


def get(url: str) -> requests.Response:
    return requests.get(url)

Running auto_pytest_mg my_project/my_file.py will then output to stdout the generated test file:

import pytest

from my_project.my_file import get, MyClass


MODULE_PATH = "my_project.my_file"


@pytest.fixture
def mock_requests(mocker):
    return mocker.patch(f"{MODULE_PATH}.requests")



@pytest.fixture
def my_class(mocker):
    a = mocker.MagicMock()
    return MyClass(a=a)


class TestMyClass:
    def test__init__(self, mocker):
        a = mocker.MagicMock()

        my_class_ = MyClass(a=a)

    def test_method(self, mocker, mg, my_class):
        mg.generate_uut_mocks_with_asserts(my_class.method)

        result = my_class.method()


      
def test_get(mocker, mg):
    url = mocker.MagicMock()
    mg.generate_uut_mocks_with_asserts(get)

    result = get(url=url)

Similar packages

  • pyguin
    • Runs given code and uses a genetic algorithm to produce test cases
    • Can output to unittest/pytest test styles
  • pythoscope
    • Last updated in 2016
    • Performs static analysis, does not run your code.

Development

See DEVELOPMENT.md

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.7.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.7.0
File Size Uploaded
auto_pytest_mg-0.7.0.tar.gz 11.2 kB Details

Built distribution (wheel)

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

Total release size: 23.7 kB

Release files / auto_pytest_mg-0.7.0.tar.gz

Download URL auto_pytest_mg-0.7.0.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b7daa7ded8598d77463fc20ca04b84d79bd967fcddf3188559790cf5857bcdf5
BLAKE2b-256 checksum
How to use checksums
323db8ab8e6b98c0824254202c35200e000b619e0feb96748fa148bd97cad7cc
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.7.0-py3-none-any.whl

Download URL auto_pytest_mg-0.7.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8916ae43d07e980c6199d38d189e763a99e78e6e94d62467bc021b6c0050d01
BLAKE2b-256 checksum
How to use checksums
6243ea050c0a7394f4631240ee05465b6e424d44ed70518e6f2dbaa4be5a19e7
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

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

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