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.2.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.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_pvcr-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 b7db99933bb91e1d3c6317fa0eb91822a4c06a55e3784d09e5ecfe00b4525720
MD5 8d646f9ef15c8e8b42a18d3c3427b0bd
BLAKE2b-256 8c0765e73668580adf95dae53c54773d611efe98ffe641028ead02d44264af4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_pvcr-0.1.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_pvcr-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a9983369d6f442defd63d9c7224ae1a0142bf41cd68766fdb9b6a9541387e7f8
MD5 d0cb2f801a0342d9dec1f3b1a33d926b
BLAKE2b-256 6d6e3983d0ad12bef47de022bf12a2c93e5a1d8c361a3de17badf509d0ac362d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_pvcr-0.1.2-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