Skip to main content

PyTest Process VCR

Project description

pytest-pvcr

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.0.tar.gz (5.4 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.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_pvcr-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 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.0.tar.gz
Algorithm Hash digest
SHA256 5a04f5677466f5cd25ad4a8a21168911d27ed7a37d52311bc415bcc8c53b0b42
MD5 760dc923e7e0593df886a16d37683ac7
BLAKE2b-256 19a80b423a3bb52c18cbc00b0e47c73f2557a9280d0c1bb16980281fe5f44837

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pytest_pvcr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eba6fff4d8f4b69294fc69ae7024b0a5ad467e175f793115047d9521ea7097d2
MD5 d5913238bc425a1d9ce152ed9aa4d52b
BLAKE2b-256 d7cc023ecf5f14d3687d9094475f5852268002a865b3b05e99eba8575695459b

See more details on using hashes here.

Provenance

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