Easily record function calls while testing
Project description
pytest-mimic
pytest-mimic is a pytest plugin to record and replay expensive function or method calls for faster and cleaner unit testing. It enables you to:
- Speed up tests that rely on expensive operations (API calls, database queries, etc.)
- Create more reliable tests that don't depend on external services
- Reduce complexity in your test setup
Quick Start
import pytest_mimic
def test_function_to_test():
# Wrap the expensive function in a mimic context manager
with pytest_mimic.mimic('module.expensive_function'):
result = function_to_test() # This function calls expensive_function internally
assert result == expected_value
- Run your tests with recording enabled:
pytest --mimic-record - For subsequent runs, just use
pytestto utilize the stored outputs
Key Features
- Record and replay function calls with identical input/output behavior
- Support for both synchronous and asynchronous functions
- Works with regular functions, class methods, static methods, and instance methods
- Global configuration to mimic functions throughout your test suite
- CLI options for managing recorded function calls
- Detects and prevents issues with functions that mutate their inputs
Installation
You can install pytest-mimic via pip:
pip install pytest-mimic
Documentation
For detailed documentation, visit https://clockworks-data.github.io/pytest-mimic/ or check the docs/ directory.
- Usage Guide: Basic usage instructions
- Advanced Features: Working with class methods, async functions, and more
- API Reference: Complete API documentation
- Examples: Example usage in different scenarios
Global Configuration
Configure functions to be mimicked globally in your project configuration:
# pyproject.toml
[tool.pytest.ini_options]
mimic_functions = [
"some_module.expensive_function",
"some_module.another_function",
"some_module.sub_module.SomeClass.method"
]
CLI Options
pytest --mimic-record: Record function calls during testspytest --mimic-clear-unused: Clean up all mimic recordings that weren't usedpytest --mimic-fail-on-unused: Raise an error if any mimic recording was left unused (useful for CI)
Storage Considerations
The mimic vault directory (.mimic_vault by default) can grow large. For Git users, consider using Git LFS:
# Install Git LFS
git lfs install
# Track pickle files in your mimic vault
git lfs track ".mimic_vault/**/*.pkl"
# Commit .gitattributes
git add .gitattributes
Contributing
Contributions are welcome! Tests can be run with tox. Please ensure the coverage at least stays the same before submitting a pull request.
License
Distributed under the terms of the MIT license, pytest-mimic is free and open source software.
Issues
If you encounter any problems, please file an issue along with a detailed description.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_mimic-0.2.0.tar.gz.
File metadata
- Download URL: pytest_mimic-0.2.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
501944dc0af35ee1b60ba60bcbbcd8e9d3330ee279bc3fa9bef97717434506ac
|
|
| MD5 |
fb107e9f4be3d7358f43c5ed87fee234
|
|
| BLAKE2b-256 |
1c42f3268c3e85a2140a0dc23066b2bd5fc8254d33c5ffebf62440e584b8e568
|
Provenance
The following attestation bundles were made for pytest_mimic-0.2.0.tar.gz:
Publisher:
publish_to_pypi.yml on clockworks-data/pytest-mimic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_mimic-0.2.0.tar.gz -
Subject digest:
501944dc0af35ee1b60ba60bcbbcd8e9d3330ee279bc3fa9bef97717434506ac - Sigstore transparency entry: 202049801
- Sigstore integration time:
-
Permalink:
clockworks-data/pytest-mimic@4bc0d9307a2006a003bdb2a87417170adfed439d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/clockworks-data
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@4bc0d9307a2006a003bdb2a87417170adfed439d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pytest_mimic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pytest_mimic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a830c729e741962be5c1456e1e54b03ec8d5a698f8c26425d045b86c42630a09
|
|
| MD5 |
ee59514d19fb02e8bc2b1b5fe4cc135f
|
|
| BLAKE2b-256 |
29f66b39fdb018b9b5cf5d5087462fd7b9f4bd08644aacac9be996b41ddb0af5
|
Provenance
The following attestation bundles were made for pytest_mimic-0.2.0-py3-none-any.whl:
Publisher:
publish_to_pypi.yml on clockworks-data/pytest-mimic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_mimic-0.2.0-py3-none-any.whl -
Subject digest:
a830c729e741962be5c1456e1e54b03ec8d5a698f8c26425d045b86c42630a09 - Sigstore transparency entry: 202049803
- Sigstore integration time:
-
Permalink:
clockworks-data/pytest-mimic@4bc0d9307a2006a003bdb2a87417170adfed439d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/clockworks-data
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@4bc0d9307a2006a003bdb2a87417170adfed439d -
Trigger Event:
workflow_dispatch
-
Statement type: