Skip to main content

vcr-style record/replay stubbing library for Python methods

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.

flowchart LR
    subgraph L0 [Business Logic]
        A1[your code]
        B1{{unittest.mock, lu}}
    end

    subgraph L1 [Client Library]
        A[requests/urllib3/pyodbc]
        B{{vcrpy, pytest-recordings<br>unittest.mock,lu}}
    end
    
    subgraph L2 [Network]
        C[TCP/IP]
        D{{keploy}}
    end
    
    subgraph L3 [Data]
        E[Database]
        F{{testcontainers}}
    end
    
    %% Business logic
    A1 --> A
    A --> C
    C --> E    

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.8.tar.gz (9.8 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.8-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lu_python-0.0.8.tar.gz
  • Upload date:
  • Size: 9.8 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.8.tar.gz
Algorithm Hash digest
SHA256 70b29a65c472c15bc4ea958b0f6970b543c8aebfbb2ea17506284d04df89267e
MD5 d87bbee3deaba409bf962bb80c1a2da0
BLAKE2b-256 3fa775eb98b2859f33dda4f652c2181d4cb8297d29a65ad1ab1e30ff15e642c7

See more details on using hashes here.

Provenance

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

Publisher: python-publish.yml on jackxxu/lu

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.8-py3-none-any.whl.

File metadata

  • Download URL: lu_python-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 9.5 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.8-py3-none-any.whl
Algorithm Hash digest
SHA256 bc32f72d56144aa59b99dba3a593878e2d2656694c6407794952852e25982ceb
MD5 e7acd519c5ac8b7f0f80dfdbfd5a29a3
BLAKE2b-256 503130d40ee215789efd28281ad507478b2f8c599cd754a10d8d7b1af7e41df9

See more details on using hashes here.

Provenance

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

Publisher: python-publish.yml on jackxxu/lu

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