### Install pytest-vcr Source: https://github.com/ktosiek/pytest-vcr/blob/master/README.rst Installs the pytest-vcr plugin using pip. This is the first step to using the plugin in your project. ```sh pip install pytest-vcr ``` -------------------------------- ### Install pytest-vcr Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/index.md Installs the pytest-vcr plugin using pip. This is the first step to start using the plugin for managing VCR.py cassettes in your pytest tests. ```sh pip install pytest-vcr ``` -------------------------------- ### Serve Documentation Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/hacking.md Starts a local server to preview the rendered documentation. This is useful for making changes to the documentation. ```bash tox -e mkdocs -- serve ``` -------------------------------- ### VCR Marker Usage for Test Configuration Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/configuration.md Illustrates how to use the pytest marker '@pytest.mark.vcr' to pass configuration options directly to the VCR constructor for a specific test function. This example shows how to ignore localhost requests during VCR processing. ```python from urllib.request import urlopen import pytest @pytest.mark.vcr(ignore_localhost=True) def test_local_and_remote(): # This one will replay from the cassette urlopen('http://www.iana.org/domains/reserved').read() # This one will always be downloaded urlopen('http://127.0.0.1/').read() ``` -------------------------------- ### Configure VCR.py for Header Filtering Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/index.md Provides an example of a pytest fixture (`vcr_config`) to configure VCR.py. This specific configuration filters the 'authorization' header, replacing its value with 'DUMMY' in the saved cassettes to prevent leaking secrets. ```python import pytest @pytest.fixture(scope='module') def vcr_config(): return { # Replace the Authorization request header with "DUMMY" in cassettes "filter_headers": [('authorization', 'DUMMY')], } ``` -------------------------------- ### Custom VCR Instance Configuration Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/configuration.md Demonstrates how to override the default 'vcr' fixture to customize the VCR instance. This allows for registering custom matchers, serializers, or persisters. The example shows registering a 'my_matcher' and setting it as the primary matching strategy. ```python import pytest from vcr import VCR # Assume my_matcher is defined elsewhere # def my_matcher(r1, r2): # pass @pytest.ాను ``` -------------------------------- ### Customize Cassette Directory Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/index.md Shows how to override the default cassette directory using the `vcr_cassette_dir` fixture. This example configures pytest-vcr to store cassettes in a path like `vhs/{module}/{test}.yaml`, organizing them by module and test name. ```python import pytest import os @pytest.fixture(scope='module') def vcr_cassette_dir(request): # Put all cassettes in vhs/{module}/{test}.yaml return os.path.join('vhs', request.module.__name__) ``` -------------------------------- ### Basic Test with pytest-vcr Source: https://github.com/ktosiek/pytest-vcr/blob/master/README.rst Demonstrates how to use the pytest-vcr plugin to record and replay HTTP requests. The `@pytest.mark.vcr()` decorator automatically handles cassette creation and usage. ```python import pytest from urllib.request import urlopen @pytest.mark.vcr() def test_iana(): response = urlopen('http://www.iana.org/domains/reserved').read() assert b'Example domains' in response ``` -------------------------------- ### pytest-vcr Configuration Fixtures Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/configuration.md Describes the pytest fixtures used for configuring pytest-vcr. These fixtures allow customization of VCR behavior, including the record mode, cassette storage directory, and cassette naming. 'vcr_config' accepts additional arguments for the VCR constructor. ```APIDOC vcr_config Scope: module Default: {} Additional arguments for the [VCR constructor](http://vcrpy.readthedocs.io/en/latest/configuration.html#configuration), as a dictionary. This will be overridden by marker and command-line options. vcr_cassette_dir Scope: module Default: test_file_directory + "/cassettes/" Path to the directory where cassettes should be stored. vcr_cassette_name Default: name of the test Name of the current test's cassette. vcr Scope: module Default: an instance of VCR This is the instance used for creating cassettes. You'd override this fixture to register your own matchers, serializers or persisters, for example: @pytest.fixture(scope='module') def vcr(vcr): vcr.register_matcher('my_matcher', my_matcher) vcr.match_on = ['my_matcher'] return vcr ``` -------------------------------- ### Annotate Tests with pytest-vcr Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/index.md Demonstrates how to use the `@pytest.mark.vcr()` decorator to enable VCR.py cassette recording for a specific test function. This automatically creates or uses a cassette file for the test's HTTP interactions. ```python import pytest from urllib.request import urlopen @pytest.mark.vcr() def test_iana(): response = urlopen('http://www.iana.org/domains/reserved').read() assert b'Example domains' in response ``` -------------------------------- ### Run All Tests and Checks Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/hacking.md Executes all tests and quality checks defined in the project's tox configuration. This is the primary command for ensuring code quality. ```bash tox ``` -------------------------------- ### pytest-vcr Command Line Options Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/configuration.md Details the command-line options available for controlling pytest-vcr behavior. '--vcr-record' sets the VCR.py record mode, useful for CI environments. '--disable-vcr' completely disables VCR recording and replaying. ```APIDOC --vcr-record Selects the [VCR.py record mode](http://vcrpy.readthedocs.io/en/latest/usage.html#record-modes). Useful in CI (where you want --vcr-record=none). --disable-vcr Disable replaying and recording. The VCR is still installed, but shouldn't affect the requests. ``` -------------------------------- ### Combine and View Test Coverage Source: https://github.com/ktosiek/pytest-vcr/blob/master/docs/hacking.md Combines test coverage data from parallel runs and generates an HTML report for detailed analysis. This helps in identifying areas with insufficient test coverage. ```bash coverage combine && coverage html ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.