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 —luprovides 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
lufor read-only calls. While you can record methods with side effects (for example, database writes),ludoes not maintain external state. Replaying a recorded write will not update a database or other external system. - No transaction or rollback support.
lurecords 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
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03d807deebdba6e03648efdb03e3fffb9dffde807f83e5af2a6acbb5ffc2ff3c
|
|
| MD5 |
959deaf84251943c494ea9b10b2fb1e4
|
|
| BLAKE2b-256 |
46c461fa06643c4e38c3631b1ee30de28cc1531835c1b83a4fcf57d3a368590a
|
Provenance
The following attestation bundles were made for lu_python-0.0.5.tar.gz:
Publisher:
python-publish.yml on jackxxu/lupy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lu_python-0.0.5.tar.gz -
Subject digest:
03d807deebdba6e03648efdb03e3fffb9dffde807f83e5af2a6acbb5ffc2ff3c - Sigstore transparency entry: 601062689
- Sigstore integration time:
-
Permalink:
jackxxu/lupy@8045698e79e4288967bf2f43d6abcc4a1d81c05b -
Branch / Tag:
refs/heads/master - Owner: https://github.com/jackxxu
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8045698e79e4288967bf2f43d6abcc4a1d81c05b -
Trigger Event:
workflow_run
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a82d387dddc0b3e011c6bb26a58c10c6bda6665f38739621d19c5d3ffba0dc0
|
|
| MD5 |
269ba405b1706e4a5769bde27aa59eab
|
|
| BLAKE2b-256 |
c015d0797cd0da9e2de8871867c7e1fd4ad877bb7035fa6c162eef86c8e306e9
|
Provenance
The following attestation bundles were made for lu_python-0.0.5-py3-none-any.whl:
Publisher:
python-publish.yml on jackxxu/lupy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lu_python-0.0.5-py3-none-any.whl -
Subject digest:
2a82d387dddc0b3e011c6bb26a58c10c6bda6665f38739621d19c5d3ffba0dc0 - Sigstore transparency entry: 601062692
- Sigstore integration time:
-
Permalink:
jackxxu/lupy@8045698e79e4288967bf2f43d6abcc4a1d81c05b -
Branch / Tag:
refs/heads/master - Owner: https://github.com/jackxxu
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8045698e79e4288967bf2f43d6abcc4a1d81c05b -
Trigger Event:
workflow_run
-
Statement type: