Skip to main content

Lightweight recorder/fixture helper (formerly mcr)

Project description

lu — a vcr-style record/replay stubbing library for Python methods

lu (录) — Chinese for "recording".

lu provides a small context manager, record(), which can patch module-qualified functions or instance methods to record return values (or exceptions) to compressed pickle files and generate a manifest.json describing the recordings.

On the first run, the patched methods execute normally and their outputs are recorded. Subsequent runs replay the recorded return values (or re-raise recorded exceptions) instead of executing the original code — similar to how HTTP cassette libraries like vcrpy work, but for arbitrary Python methods.

Quickstart

Install from PyPI:

pip install lu-python

Basic usage:

import lu

with lu.record(
    target={
        'module1.Foo.expensive_method1': ['arg1', 'kwarg1'],
        'package2.module2.expensive_method2': ['arg2'],
    },
    recordings_dir='tests/fixtures/recordings/',
):
    from module1 import Foo
    foo = Foo()
    foo.expensive_method('bar', kwarg1=3)

    from package2 import module2
    module2.expensive_method2('baz')

See tests/test_lu.py for more examples.

Using with pytest

Add an autouse fixture (for example in tests/conftest.py) to enable recording/replay for the test session:

import pytest
import lu

@pytest.fixture(scope='session', autouse=True)
def stub_calls():
    with lu.record(
        target={
            # 'module.Class.method': ['arg_name', 'kwarg_name'],
            # add targets you want recorded here
        },
        recordings_dir='tests/fixtures/recordings/',
    ):
        yield

How lu differs from other tools

I wrote lu because I couldn't find a simple record/replay tool for arbitrary Python methods. A few alternatives exist but they don't match this use case:

  • testcontainers: runs real services in Docker, which requires setup and infrastructure and does not provide automatic request/response recording.
  • keploy: captures and replays network calls at a lower level; powerful but heavier-weight.
  • vcrpy / pytest-recordings: focused on HTTP clients (requests/urllib3) only.
  • unittest.mock: flexible but manual — lu provides an automated record-and-replay workflow on top of method patching.

lu works at the method level, so you can record calls to client libraries (e.g., database drivers, HTTP clients) or your own functions.

Limitations

  • Prefer using lu for read-only calls. While you can record methods with side effects (for example, database writes), lu does not maintain external state. Replaying a recorded write will not update a database or other external system.
  • No transaction or rollback support. lu records and replays return values and exceptions only.

Contributing

Contributions and bug reports are welcome. Please open an issue or submit a pull request with tests that demonstrate the change or fix.

License

This project is provided under the terms of the repository license. See the LICENSE file for details.

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

lu_python-0.0.5.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

lu_python-0.0.5-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file lu_python-0.0.5.tar.gz.

File metadata

  • Download URL: lu_python-0.0.5.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lu_python-0.0.5.tar.gz
Algorithm Hash digest
SHA256 03d807deebdba6e03648efdb03e3fffb9dffde807f83e5af2a6acbb5ffc2ff3c
MD5 959deaf84251943c494ea9b10b2fb1e4
BLAKE2b-256 46c461fa06643c4e38c3631b1ee30de28cc1531835c1b83a4fcf57d3a368590a

See more details on using hashes here.

Provenance

The following attestation bundles were made for lu_python-0.0.5.tar.gz:

Publisher: python-publish.yml on jackxxu/lupy

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

File details

Details for the file lu_python-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: lu_python-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lu_python-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2a82d387dddc0b3e011c6bb26a58c10c6bda6665f38739621d19c5d3ffba0dc0
MD5 269ba405b1706e4a5769bde27aa59eab
BLAKE2b-256 c015d0797cd0da9e2de8871867c7e1fd4ad877bb7035fa6c162eef86c8e306e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for lu_python-0.0.5-py3-none-any.whl:

Publisher: python-publish.yml on jackxxu/lupy

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