freshcrate
Home > Frameworks > pytest-recording

pytest-recording

A pytest plugin powered by VCR.py to record and replay HTTP traffic

Description

pytest-recording ================ |codecov| |Build| |Version| |Python versions| |License| A pytest plugin powered by VCR.py to record and replay HTTP traffic. Features -------- - Straightforward ``pytest.mark.vcr``, that reflects ``VCR.use_cassettes`` API; - Combining multiple VCR cassettes; - Network access blocking; - The ``rewrite`` recording mode that rewrites cassettes from scratch. Installation ------------ This project can be installed via pip: .. code:: bash pip install pytest-recording āš ļø Incompatibility Warning If you have ``pytest-vcr`` installed, please uninstall it before using ``pytest-recording``, as the two plugins are incompatible. Usage ----- .. code:: python import pytest import requests # cassettes/{module_name}/test_single.yaml will be used @pytest.mark.vcr def test_single(): assert requests.get("http://httpbin.org/get").text == '{"get": true}' # cassettes/{module_name}/example.yaml will be used @pytest.mark.default_cassette("example.yaml") @pytest.mark.vcr def test_default(): assert requests.get("http://httpbin.org/get").text == '{"get": true}' # these cassettes will be used in addition to the default one @pytest.mark.vcr("/path/to/ip.yaml", "/path/to/get.yaml") def test_multiple(): assert requests.get("http://httpbin.org/get").text == '{"get": true}' assert requests.get("http://httpbin.org/ip").text == '{"ip": true}' # Make assertions based on the cassette calls/responses: @pytest.mark.vcr def test_call_count(vcr): assert requests.get("http://httpbin.org/get").text == '{"get": true}' assert requests.get("http://httpbin.org/ip").text == '{"ip": true}' # See https://vcrpy.readthedocs.io/en/latest/advanced.html for more info # about the Cassette object: assert vcr.play_count == 2 Run your tests: .. code:: bash pytest --record-mode=once test_network.py Default recording mode ~~~~~~~~~~~~~~~~~~~~~~ ``pytest-recording`` uses the ``none`` VCR recording mode by default to prevent unintentional network requests. To allow them you need to pass a different recording mode (e.g. ``once``) via the ``--record-mode`` CLI option to your test command. See more information about available recording modes in the `official VCR documentation <https://vcrpy.readthedocs.io/en/latest/usage.html#record-modes>`_ Configuration ~~~~~~~~~~~~~ You can provide the recording configuration with the ``vcr_config`` fixture, which could be any scope - ``session``, ``package``, ``module``, or ``function``. It should return a dictionary that will be passed directly to ``VCR.use_cassettes`` under the hood. .. code:: python import pytest @pytest.fixture(scope="module") def vcr_config(): return {"filter_headers": ["authorization"]} For more granular control you need to pass these keyword arguments to individual ``pytest.mark.vcr`` marks, and in this case all arguments will be merged into a single dictionary with the following priority (low -> high): - ``vcr_config`` fixture - all marks from the most broad scope ("session") to the most narrow one ("function") Example: .. code:: python import pytest pytestmark = [pytest.mark.vcr(ignore_localhost=True)] @pytest.fixture(scope="module") def vcr_config(): return {"filter_headers": ["authorization"]} @pytest.mark.vcr(filter_headers=[]) def test_one(): ... @pytest.mark.vcr(filter_query_parameters=["api_key"]) def test_two(): ... Resulting VCR configs for each test: - ``test_one`` - ``{"ignore_localhost": True, "filter_headers": []}`` - ``test_two`` - ``{"ignore_localhost": True, "filter_headers": ["authorization"], "filter_query_parameters": ["api_key"]}`` You can get access to the used ``VCR`` instance via ``pytest_recording_configure`` hook. It might be useful for registering custom matchers, persisters, etc.: .. code:: python # conftest.py def jurassic_matcher(r1, r2): assert r1.uri == r2.uri and "JURASSIC PARK" in r1.body, \ "required string (JURASSIC PARK) not found in request body" def pytest_recording_configure(config, vcr): vcr.register_matcher("jurassic", jurassic_matcher) You can disable the VCR.py integration entirely by passing the ``--disable-recording`` CLI option. Rewrite record mode ~~~~~~~~~~~~~~~~~~~ It is possible to rewrite a cassette from scratch and not extend it with new entries as it works now with the ``all`` record mode from VCR.py. However, it will rewrite only the default cassette and won't touch extra cassettes. .. code:: python import pytest @pytest.fixture(scope="module") def vcr_config(): return {"record_mode": "rewrite"} Or via command-line option: .. code:: bash $ pytest --record-mode=rewrite tests/ Blocking network access ~~~~~~~~~~~~~~~~~~~~~~~ To have more confidence that your tests will not go over the wire, you can

Release History

VersionChangesUrgencyDate
0.13.4Imported from PyPI (0.13.4)Low4/21/2026
v0.13.4### Fixed - `AttributeError` on Windows. #174 Low5/8/2025
v0.13.3### Fixed - Limit generated cassette names to prevent `OSError`. #172 Low4/24/2025
v0.13.2- Add lazy loading of VCR to reduce plugin overhead. #145 - Documentation improvements.Low7/9/2024
v0.13.1- Add support for Python 3.12. - Add trove classifier for the license.Low12/6/2023
v0.13.0- Drop support for Python 3.5 and 3.6. #97 - Add support for VCR.py 5.0.0. #118 - Drop direct dependency on `attrs`. - Build: Switch the build backend to [Hatch](https://hatch.pypa.io/).Low7/31/2023
v0.12.2### :rocket: Added - Support for Python 3.10 and 3.11. #96 Low2/16/2023
v0.12.1### :rocket: Added - Allow `block_network.allowed_hosts` configuration via the `vcr_config` fixture. #82 Low6/20/2022
v0.12.0 ## :bug: Bug fixes - 484bb88 Honor `record_mode` set via the `vcr_config` fixture or the `vcr` mark when `block_network` is applied ## :wrench: Chores and Improvements - 8f2049a Release 0.12.0 - 071ede2 Validate input arguments for `vcr` and `block_network` pytest marks - 92a922b Update pre-commit & fix a typo in the `isort` config ## :package: Other - e0ae83d Mention the default recording mode in the README file Low7/8/2021
v0.11.0 ## :rocket: Features - cb40410 `--disable-recording` CLI option to completely disable the VCR.py integration (#65) ## :wrench: Chores and Improvements - 2a78502 Release 0.11.0 - 2e2e3e3 Add Tox `build` job for packaging purposes ## :package: Other - f9d6db7 Add release & publish jobs - ba11b9f Fix typo - b245d79 Update README - 15cc6bc Add Python 3.9 builds Low11/25/2020
v0.9.0### Added - Type annotations to the plugin's internals. ### Fixed - `TypeError` when using network blocking with an address as `bytes` or `bytearray`. #55 ### Removed - Python 2 support. #53Low8/13/2020
v0.3.4#### Added - An error is raised if `pytest-vcr` is installed. `pytest-recording` is not compatible with it. #20Low10/21/2019
v0.3.3#### Added - Pytest assertion rewriting for not matched requests.Low8/18/2019
v0.3.2#### Fixed - Do not add "yaml" extension to cassettes if JSON serializer is used. #10Low8/1/2019
v0.3.1#### Added - ``network.block`` / ``network.unblock`` functions for manual network blocking manipulations. #8Low8/1/2019
v0.3.0#### Added - A pytest mark to block all network requests, except for VCR recording. Low7/20/2019
v0.2.0#### Added - Reusable `vcr_config` fixture for `VCR.use_cassette` call. #2Low7/18/2019

Dependencies & License Audit

Loading dependencies...

Similar Packages

pytest-mockThin-wrapper around the mock package for easier use with pytest3.15.1
schemathesisProperty-based testing framework for Open API and GraphQL based apps4.15.2
pytest-subtestsunittest subTest() support and subtests fixture0.15.0
seleniumbaseA complete web automation framework for end-to-end testing.4.48.2
pytest-randomlyPytest plugin to randomly order tests and control random.seed.4.1.0