Skip to main content

PyPI PyPI - Python Version CI Status Coverage Status Maintenance

A pytest plugin that automates vcrpy cassettes deletion on test failure.

Rationale

Sometimes when testing a function containing multiple http requests a failure will occur halfway through (this happens all the time when doing TDD). When using vcrpy to cache http requests, this could result in a test cache that only cover a fraction of the function under test, which in turn could prevent the function to ever succeed or the test to pass in subsequent run if the http requests that didn't get cached depended on a fresh context (maybe they are time sensitive or there's randomness involved).

This possibility leads to doubt and untrust towards the test suite, which is wrong on too many level.

This plugin provides a pytest marker that solves the uncertainty: when a marked test fails its http requests cache will be purged, so that it can start fresh on the next run.

Install

Simply run:

pip install pytest-vcr-delete-on-fail

Usage

Once the plugin is installed, mark the designated test like this:

import pytest
import requests
import vcr

my_vcr = vcr.VCR(record_mode="once")

cassette_path = "tests/cassettes/this.yaml"

@pytest.mark.vcr_delete_on_fail([cassette_path])
def test_this():
    with my_vcr.use_cassette(cassette_path):
        requests.get("https://github.com")
    assert False

Running the test will result in no cassettes on disk: the http request first got cached but since the test failed the cassette was later deleted.

When using pytest-recording to automatically save cassettes (or when using its path and naming conventions manually), this plugin's marker can be used without arguments since it will figure out the cassette path on its own:

import pytest
import requests

# Configure pytest_recording
@pytest.fixture(scope="module")
def vcr_config():
    return {"record_mode": ["once"]}

@pytest.mark.vcr_delete_on_fail
def test_this():
    requests.get("https://github.com")
    assert False

The marker is actually quite flexible; this is the full signature:

pytest.mark.vcr_delete_on_fail(
    cassette_path_list: Optional[List[Union[str, Callable[[Item], str]]]],
    delete_default: Optional[bool],
    skip: Optional[bool])
cassette_path_list

Either the first unnamed argument or a named one; when both are missing, the cassette path will be automatically determined. This list's elements can either be str or functions that take a pytest nodes.Item object and return a str: these are the to-be-deleted cassettes' full path. If cassette_path_list is None, no cassette will be deleted for that test (which is equivalent to pass skip=True); if an empty list is passed instead, that marker won't result in a cassette deletion but it won't prevent other markers to delete cassettes.

delete_default

Only valid as named argument. It's True by default if no cassette_path_list is passed to the marker, False otherwise. If True the cassette with the automatically computed path will be deleted.

skip

Only valid as named argument. It's False by default. If True no cassette will be deleted for that test. It's equivalent to passing cassette_path_list=None.

Utilities

When writing a function to determine a cassette path here are some useful imports from pytest_vcr_delete_on_fail:

get_default_cassette_path(item: nodes.Item) -> str

A function that return the path of the default cassette.

has_class_scoped_setup_failed(item: nodes.Item) -> bool

It returns True if a class scoped fixture failed in the setup phase. This could come in handy when using class scoped setup: an example of this pattern can be found in test_integrations.py.

has_class_scoped_teardown_failed(item: nodes.Item) -> bool

It returns True if a class scoped fixture failed in the teardown phase.

Development

Install invoke and poetry:

pip install invoke poetry

Now clone the git repo:

git clone https://github.com/CarloDePieri/pytest-vcr-delete-on-fail.git
cd pytest-vcr-delete-on-fail
inv install

This will try to create a virtualenv based on python3.7 and install there all project's dependencies. If a different python version is preferred, it can be selected by specifying the --python (-p) flag like this:

inv install -p python3.8

The test suite can be run with commands:

inv test         # run the test suite
inv test-spec    # run the tests while showing the output as a spec document
inv test-cov     # run the tests suite and produce a coverage report

To run the test suite against all supported python version (they must be in path!) run:

inv test-all-python-version

To test the github workflow with act:

inv act-dev           # test the dev workflow
inv act-dev -c shell  # open a shell in the act container (the above must fail first!)
inv act-dev -c clean  # stop and delete a failed act container

Metadata

Release files for pytest-vcr-delete-on-fail 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-vcr-delete-on-fail 0.9.0
File Size Uploaded Provenance Actions
pytest-vcr-delete-on-fail-0.9.0.tar.gz 18.4 kB Without provenance Details for pytest-vcr-delete-on-fail-0.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-vcr-delete-on-fail 0.9.0
File Interpreter ABI Platform Actions
pytest_vcr_delete_on_fail-0.9.0-py3-none-any.whl Python 3 none any Details for pytest_vcr_delete_on_fail-0.9.0-py3-none-any.whl

Total release size: 37.1 kB

Release files / pytest-vcr-delete-on-fail-0.9.0.tar.gz

Download URL pytest-vcr-delete-on-fail-0.9.0.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
86554a37f91c5aca959efd930fcea09c8183b0e183fb07aa249ed5a0f28f03f9
BLAKE2b-256 checksum
How to use checksums
927a023c01419638b985d261790970134b4f2e8bd592cfbc61448960cab05dd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.9.6 Linux/5.12.12-arch1-2-vfio

Release files / pytest_vcr_delete_on_fail-0.9.0-py3-none-any.whl

Download URL pytest_vcr_delete_on_fail-0.9.0-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15539275394d2f4cf19efceec8c76542c6950f80be301b11ee22e0a2a2b55552
BLAKE2b-256 checksum
How to use checksums
249e08ecae2ffc97d66c289f6f767340d873ac8bfb6eb860f88ef35bdba8fb66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.9.6 Linux/5.12.12-arch1-2-vfio

Security for pytest-vcr-delete-on-fail 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page