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.
Metadata
Release files for pytest-mimic 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_mimic-0.2.0.tar.gz | 13.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_mimic-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.8 kB
Release files / pytest_mimic-0.2.0.tar.gz
| Download URL | pytest_mimic-0.2.0.tar.gz |
|---|---|
| Size | 13.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
501944dc0af35ee1b60ba60bcbbcd8e9d3330ee279bc3fa9bef97717434506ac
|
|
BLAKE2b-256 checksum How to use checksums |
1c42f3268c3e85a2140a0dc23066b2bd5fc8254d33c5ffebf62440e584b8e568
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 24, 2025.
Transparency logRelease files / pytest_mimic-0.2.0-py3-none-any.whl
| Download URL | pytest_mimic-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a830c729e741962be5c1456e1e54b03ec8d5a698f8c26425d045b86c42630a09
|
|
BLAKE2b-256 checksum How to use checksums |
29f66b39fdb018b9b5cf5d5087462fd7b9f4bd08644aacac9be996b41ddb0af5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 24, 2025.
Transparency log