Skip to main content

PyTest Process VCR

Project description

pytest-pvcr

PyPI version Python versions License

A pytest plugin that records and replays commands executed with subprocess.run().

This plugin was inspired by VCR.py.

Installation

This project can be installed via pip:

pip install pytest-pvcr

Usage

import subprocess
import pytest

@pytest.mark.pvcr()
def test_command():
    # subprocess.run is patched at runtime
    ret = subprocess.run(["ls", "/tmp"])

    assert rc.returncode == 0

@pytest.mark.pvcr(wait=False)
def test_command():
    # Super long command but since wait == False, PVCR does not wait its completion
    ret = subprocess.run(["sleep", "1000"])

    assert rc.returncode == 0

Run your tests:

pytest --pvcr-record-mode=new test_commands.py

Record modes

There is three record modes:

# Only record new commands not previously recorded
pytest --pvcr-record-mode=new test_commands.py

# Record nothing
pytest --pvcr-record-mode=none test_commands.py

# Record all commands, even previously recorded ones
pytest --pvcr-record-mode=all test_commands.py

Block execution

The execution of processes can be completely blocked. This is useful to protect test environments from destructive commands.

pytest --pvcr-block-run test_commands.py

The test will fail if an unrecorded command is executed.

Fuzzy matching

Commands can be fuzzy matched by defining one or more regex.

If a fuzzy regex has matching groups, the matched parts are kept for matching the commands.

If a fuzzy regex has no matching groups, the whole matched string is ignored when matching the commands.

# Ignore `--dry-run` arguments when matching commands
pytest --pvcr-fuzzy-matcher='--dry-run' test_commands.py

# Ignore the beginning of a path and keep the filename
pytest --pvcr-fuzzy-matcher='^.+\/(kubeconfig)$' test_commands.py

It's possible to automatically ignore the parent path of the test script. This parameter is useful to make test assets portable.

pytest --pvcr-auto-fuzzy-match test_commands.py

Python support

This plugin supports python >= 3.12

Authors

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_pvcr-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

pytest_pvcr-0.1.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_pvcr-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for pytest_pvcr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7dfbffa0decf6e406ee9adf65d502c3ff59671699444c06883abb8c8c61c3955
MD5 0d9490d207854cc2577f03179085b668
BLAKE2b-256 9aeb090276ba54f1124dbd352fb9e64245d3dbffab285c224abbb24639386321

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_pvcr-0.1.1.tar.gz:

Publisher: publish-to-pypi.yml on kafeinnet/pytest-pvcr

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

File metadata

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

File hashes

Hashes for pytest_pvcr-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e3bd60225832f75a81bcc20e3b8178b48535ea6538ccef3a5f39f6f8f5e6cb1f
MD5 cbbb987686417e4372b2502a94f931b7
BLAKE2b-256 601bbcb23a5a5dc1ade1d710cd2eb6231b51254360299eaae5cbae926c89ea43

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_pvcr-0.1.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on kafeinnet/pytest-pvcr

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