API Reference

This is the reference for every public interface. Except pytest.mark.vcr_delete_on_fail() (which is available as a pytest marker), everything else needs to be imported from the pytest_vcr_delete_on_fail module.

@pytest.mark.vcr_delete_on_fail
@pytest.mark.vcr_delete_on_fail(target, delete_default, skip)

The pytest marker used to specify which cassette(s) will be deleted on failure.

Parameters:
  • target (ValidTarget) – the cassette(s) to delete

  • delete_default (bool) – whether to delete the default cassette. Default: True if no target is specified, False otherwise

  • skip (bool) – whether to skip deletion of the target cassette(s). Default: False

pytest_vcr_delete_on_fail.ValidTarget

The type used by the target argument of pytest.mark.vcr_delete_on_fail().

Type:

Union[None, str, List[ValidTarget], Callable[[_pytest.python.Function], ValidTarget]]

This type is mainly used for type checking; it’s recursively defined as follow:

from typing import TypeVar, Callable, List
from _pytest.python import Function

ValidTarget = TypeVar(
    "ValidTarget",  # The name of the type
    None,  # It can be None: which means that no cassette will be deleted
    str,  # It can be a string: which will be interpreted as a path
    List["ValidTarget"],  # It can be a List of ValidTarget
    Callable[[Function], "ValidTarget"],  # It can be a function that returns ValidTarget
)

It allows for nested structures of List[ValidTarget] and Callable[[Function], ValidTarget]. All of these are valid examples:

def function_a(item: Function) -> List[str]:
    return ["c"]

def function_b(item: Function) -> List[Union[str, Callable[[Function], List[str]]]]:
    return ["d", function_a]

["cassette.yaml"]
["cassette.yaml", None, ["a", "b"]]
[function_a, [function_b]]
pytest_vcr_delete_on_fail.delete_on_fail(cassettes, skip)

Deletes the target cassette(s) if an Exception is raised inside this context manger code block.

Parameters:
  • cassettes (Optional[List[str]]) – the cassette(s) to delete

  • skip (bool) – whether to skip deletion of the target cassette(s). Default: False

pytest_vcr_delete_on_fail.vcr_and_dof(vcr, cassette, skip_delete, additional_delete, **kwargs)

A convenient thin wrapper around both delete_on_fail and VCR().use_cassette: it allows to record a cassette and delete it on failure with a single context manager.

Parameters:
  • vcr (VCR) – the vcr.VCR instance used for use_cassette

  • cassette (str) – the cassette to record and delete in case of failure

  • skip_delete (bool) – whether to skip deletion of the target cassette(s). Default: False

  • additional_delete (List[str]) – other cassettes to delete in case of failure. Default: []

  • kwargs (Any) – every additional named parameter will be passed to use_cassette. Default: None

pytest_vcr_delete_on_fail.get_default_cassette_path(item)
Return the default cassette full path given the test Function.
Follow the convention: ./cassettes/{module-name}/{test-class-if-any.}{test_name}.yaml
Parameters:

item (_pytest.python.Function) – the Function instance that represent the current test

Returns:

the path of the default cassette

Return type:

str

pytest_vcr_delete_on_fail.has_class_scoped_setup_failed(item)

Return True if test has failed because of a class scoped fixture in the setup phase.

Parameters:

item (_pytest.python.Function) – the Function instance that represent the current test

Returns:

whether the class scoped setup failed

Return type:

bool

pytest_vcr_delete_on_fail.has_class_scoped_teardown_failed(item)

Return True if test has failed because of a class scoped fixture in the teardown phase.

Parameters:

item (_pytest.python.Function) – the Function instance that represent the current test

Returns:

whether the class scoped teardown failed

Return type:

bool