Skip to main content

Easily record function calls while testing

Project description

pytest-mimic

PyPI version Python versions See Build Status on GitHub Actions


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
  1. Run your tests with recording enabled: pytest --mimic-record
  2. For subsequent runs, just use pytest to 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.

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 tests
  • pytest --mimic-clear-unused: Clean up all mimic recordings that weren't used
  • pytest --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

pytest_mimic-0.2.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_mimic-0.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

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

Hashes for pytest_mimic-0.2.0.tar.gz
Algorithm Hash digest
SHA256 501944dc0af35ee1b60ba60bcbbcd8e9d3330ee279bc3fa9bef97717434506ac
MD5 fb107e9f4be3d7358f43c5ed87fee234
BLAKE2b-256 1c42f3268c3e85a2140a0dc23066b2bd5fc8254d33c5ffebf62440e584b8e568

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for pytest_mimic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a830c729e741962be5c1456e1e54b03ec8d5a698f8c26425d045b86c42630a09
MD5 ee59514d19fb02e8bc2b1b5fe4cc135f
BLAKE2b-256 29f66b39fdb018b9b5cf5d5087462fd7b9f4bd08644aacac9be996b41ddb0af5

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page