SKILL.md--- name: pytest-recording description: Work with pytest-recording (VCR.py) for recording and replaying HTTP interactions in tests. Use when writing VCR tests, managing cassettes, configuring VCR options, filtering sensitive data, or debugging recorded HTTP responses. --- # pytest-recording (VCR.py) Testing ## Overview pytest-recording wraps VCR.py to record HTTP interactions as YAML cassettes, enabling deterministic tests without live API calls. ## Quick Reference ### Running Tests ```bash # Run all tests (uses existing cassettes) uv run pytest tests/ # Run a single test uv run pytest tests/test_module.py::test_function # Rewrite all cassettes with fresh responses uv run pytest tests/ --vcr-record=rewrite # Record only missing cassettes uv run pytest tests/ --vcr-record=new_episodes # Disable VCR (make live requests) uv run pytest tests/ --disable-recording ``` ### Recording Modes | Mode | Flag | Behavior | |------|------|----------| | `none` | `--vcr-record=none` | Only replay, fail if no cassette | | `once` | (default) | Record if no cassette exists | | `new_episodes` | `--vcr-record=new_episodes` | Record new requests, keep existing | | `all` | `--vcr-record=all` | Always record, overwrite existing | | `rewrite` | `--vcr-record=rewrite` | Delete and re-record all cassettes | ### Writing VCR Tests Basic test with VCR: ```python import pytest @pytest.mark.vcr() def test_api_call(): response = my_api_function() assert response.status_code == 200 ``` Custom cassette name: ```python @pytest.mark.vcr("custom_cassette_name.yaml") def test_with_custom_cassette(): pass ``` Multiple cassettes: ```python @pytest.mark.vcr("cassette1.yaml", "cassette2.yaml") def test_with_multiple_cassettes(): pass ``` ### VCR Configuration in conftest.py The `vcr_config` fixture controls VCR behavior: ```python @pytest.fixture(scope="module") def vcr_config(): return { # Filter sensitive headers from recordings "filter_headers": ["authorization", "api-key", "x-api-key"], # Filter query parameters "filter_query_parameters": ["key", "api_key", "token"], # Match requests by these criteria "match_on": ["method", "scheme", "host", "port", "path", "query"], # Ignore certain hosts (don't record) "ignore_hosts": ["localhost", "127.0.0.1"], # Record mode "record_mode": "once", } ``` ### Filtering Sensitive Data For LLM providers, filter authentication: ```python @pytest.fixture(scope="module") def vcr_config(): return { "filter_headers": [ "authorization", # OpenAI, Anthropic "api-key", # Azure OpenAI "x-api-key", # Anthropic "x-goog-api-key", # Google AI ], "filter_query_parameters": ["key"], } ``` ### Response Processing Use `pytest_recording_configure` for advanced processing: ```python def pytest_recording_configure(config, vcr): vcr.serializer = "yaml" vcr.decode_compressed_response = True # Sanitize response headers def sanitize_response(response): response['headers']['Set-Co