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)
auto_pytest_mg generates a test skeleton for a given python file. This skeleton includes:
- fixtures that mock all non-standard library imported names
- a boilerplate test for every class method and property
- a boilerplate test for every function
mocker
andmg
fixtures for all testsmocker
: pytest-mockmg
: pytest-mocker-generator- If you're unfamiliar with pytest-mock-generator, you can read up on usage information in their README.
It is not auto_pytest_mg's goal to produce the entirety of the test. The creation of test mocks and
asserts is delegated to pytest-mocker-generator via the mg
fixture and the
mg.generate_uut_mocks_with_asserts(...)
call.
This package 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
- Executes given source 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.
- Outputs unittest test style only
Development
See DEVELOPMENT.md
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
File details
Details for the file auto_pytest_mg-0.8.0.tar.gz
.
File metadata
- Download URL: auto_pytest_mg-0.8.0.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.13 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0853022b43b7373809277e42f4ed03656d8df3b8f29c3bd264fcbcc4fecf8fbd |
|
MD5 | 0ce17fbfd7af3694ad243ada1dceac23 |
|
BLAKE2b-256 | d9e5030802dfd7bacc712454de4ae7cae99c957586336ccb3567d0cb9dfe5230 |
File details
Details for the file auto_pytest_mg-0.8.0-py3-none-any.whl
.
File metadata
- Download URL: auto_pytest_mg-0.8.0-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.13 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4295e7a9c33359b5cd566015254cc51f0be3d7ea42d02e5aa766faa52c2961ad |
|
MD5 | 35623288f9d0464913bab8ba38ec4e80 |
|
BLAKE2b-256 | 9998cfb9786968187fd19a5ea994c3495bbf686121106db26534642a062b1129 |