pytest-pvcr
A pytest plugin that records and replays commands executed with subprocess.run(), inspired by VCR.py.
Installation
pip install pytest-pvcr
Usage
Mark your tests with @pytest.mark.pvcr() and run them:
import subprocess
import pytest
@pytest.mark.pvcr()
def test_ls():
ret = subprocess.run(["ls", "/tmp"])
assert ret.returncode == 0
@pytest.mark.pvcr(wait=False)
def test_slow_command():
# On replay, pvcr won't sleep for the original command duration
ret = subprocess.run(["sleep", "10"])
assert ret.returncode == 0
pytest --pvcr-record-mode=new test_commands.py
Recordings are stored as YAML files in recordings/<module>/<test_name>.yaml.
Record modes
# Only record new commands not previously recorded (default)
pytest --pvcr-record-mode=new
# Replay only, never record
pytest --pvcr-record-mode=none
# Re-record all commands, even previously recorded ones
pytest --pvcr-record-mode=all
# Record on first run, then replay only and block unrecorded commands
pytest --pvcr-record-mode=once
The once mode is useful in CI: it records everything on the first run (when no recording file exists), then on subsequent runs it replays and raises PVCRBlockedRunException if an unrecorded command is encountered.
Block execution
Block all unrecorded subprocess calls, useful to protect test environments:
pytest --pvcr-block-run
Fuzzy matching
Fuzzy matching replaces variable parts of commands so recordings stay portable.
Via CLI (global, applies to all tests):
# Ignore `--dry-run` arguments when matching commands
pytest --pvcr-fuzzy-matcher='--dry-run'
# Keep only the filename from a path
pytest --pvcr-fuzzy-matcher='^.+\/(kubeconfig)$'
# Automatically ignore the parent directory of the test file
pytest --pvcr-auto-fuzzy-match
Via marker (per-test):
@pytest.mark.pvcr()
@pytest.mark.pvcr_fuzzy_matcher(r"^.+\/(config\.yml)$")
def test_with_fuzzy():
subprocess.run(["cat", "/some/path/config.yml"])
If a regex has no capture groups, the matched string is replaced with a placeholder. If a regex has capture groups, the captured parts are kept and the rest is replaced.
Python support
Python >= 3.12
Authors
- Fabien Dupont fab+github@kafe-in.net
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 pytest_pvcr-0.2.0.tar.gz.
File metadata
- Download URL: pytest_pvcr-0.2.0.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
762ba702de443345d307f2049171e2e2868d6e1721883454bbcb6053cc000e37
|
|
| MD5 |
048b80622e53fa2c8bc5d312d2adf5b4
|
|
| BLAKE2b-256 |
cf070c028dc1a534b15d7da8b56c399418a36709a2a3149c248b9e04371414f8
|
Provenance
The following attestation bundles were made for pytest_pvcr-0.2.0.tar.gz:
Publisher:
publish-to-pypi.yml on kafeinnet/pytest-pvcr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_pvcr-0.2.0.tar.gz -
Subject digest:
762ba702de443345d307f2049171e2e2868d6e1721883454bbcb6053cc000e37 - Sigstore transparency entry: 2291396108
- Sigstore integration time:
-
Permalink:
kafeinnet/pytest-pvcr@98b212a4399364928e28cc476b5b7507a95d95ae -
Branch / Tag:
refs/tags/0.2.0 - Owner: https://github.com/kafeinnet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@98b212a4399364928e28cc476b5b7507a95d95ae -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_pvcr-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pytest_pvcr-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
236ba04fc3c4ae77f20a02ecc6cc29f464c5c61c9f9f2ebf2ddf0485c304c17d
|
|
| MD5 |
7eb835d7d426c2a7458152310e06ccfc
|
|
| BLAKE2b-256 |
af3a3b9e4e68fb0d70e6774e05314b698f83e8bec47046f0acfd0206c9430387
|
Provenance
The following attestation bundles were made for pytest_pvcr-0.2.0-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on kafeinnet/pytest-pvcr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_pvcr-0.2.0-py3-none-any.whl -
Subject digest:
236ba04fc3c4ae77f20a02ecc6cc29f464c5c61c9f9f2ebf2ddf0485c304c17d - Sigstore transparency entry: 2291396130
- Sigstore integration time:
-
Permalink:
kafeinnet/pytest-pvcr@98b212a4399364928e28cc476b5b7507a95d95ae -
Branch / Tag:
refs/tags/0.2.0 - Owner: https://github.com/kafeinnet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@98b212a4399364928e28cc476b5b7507a95d95ae -
Trigger Event:
push
-
Statement type: