{"id":13595781,"url":"https://github.com/kiwicom/pytest-recording","last_synced_at":"2025-04-09T13:33:25.807Z","repository":{"id":35019137,"uuid":"197214988","full_name":"kiwicom/pytest-recording","owner":"kiwicom","description":"A pytest plugin that allows recording network interactions via VCR.py","archived":false,"fork":false,"pushed_at":"2024-08-22T08:41:45.000Z","size":216,"stargazers_count":423,"open_issues_count":28,"forks_count":34,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-09-22T17:37:31.493Z","etag":null,"topics":["cassettes","hacktoberfest","pytest","python","testing","vcr"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kiwicom.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-16T14:57:52.000Z","updated_at":"2024-09-22T13:28:55.000Z","dependencies_parsed_at":"2023-12-07T01:52:59.702Z","dependency_job_id":"3d201d9b-ca56-4e05-a920-3a10c4de9e5b","html_url":"https://github.com/kiwicom/pytest-recording","commit_stats":{"total_commits":163,"total_committers":16,"mean_commits":10.1875,"dds":"0.49693251533742333","last_synced_commit":"f4a26b213e4873c70aa01896f5c982ef1cbceec8"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwicom%2Fpytest-recording","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwicom%2Fpytest-recording/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwicom%2Fpytest-recording/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwicom%2Fpytest-recording/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kiwicom","download_url":"https://codeload.github.com/kiwicom/pytest-recording/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223394743,"owners_count":17138607,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cassettes","hacktoberfest","pytest","python","testing","vcr"],"created_at":"2024-08-01T16:01:57.437Z","updated_at":"2025-04-09T13:33:25.799Z","avatar_url":"https://github.com/kiwicom.png","language":"Python","funding_links":[],"categories":["Python","Software Engineering"],"sub_categories":["Curated Python packages"],"readme":"pytest-recording\n================\n\n|codecov| |Build| |Version| |Python versions| |License|\n\nA pytest plugin that records network interactions in your tests via VCR.py.\n\nFeatures\n--------\n\n- Straightforward ``pytest.mark.vcr``, that reflects ``VCR.use_cassettes`` API;\n- Combining multiple VCR cassettes;\n- Network access blocking;\n- The ``rewrite`` recording mode that rewrites cassettes from scratch.\n\nInstallation\n------------\n\nThis project can be installed via pip:\n\n.. code:: bash\n\n    pip install pytest-recording\n\n⚠️This project is not compatible with `pytest-vcr`, make sure to uninstall before ⚠️\n\n\nUsage\n-----\n\n.. code:: python\n\n    import pytest\n    import requests\n\n    # cassettes/{module_name}/test_single.yaml will be used\n    @pytest.mark.vcr\n    def test_single():\n        assert requests.get(\"http://httpbin.org/get\").text == '{\"get\": true}'\n\n    # cassettes/{module_name}/example.yaml will be used\n    @pytest.mark.default_cassette(\"example.yaml\")\n    @pytest.mark.vcr\n    def test_default():\n        assert requests.get(\"http://httpbin.org/get\").text == '{\"get\": true}'\n\n    # these cassettes will be used in addition to the default one\n    @pytest.mark.vcr(\"/path/to/ip.yaml\", \"/path/to/get.yaml\")\n    def test_multiple():\n        assert requests.get(\"http://httpbin.org/get\").text == '{\"get\": true}'\n        assert requests.get(\"http://httpbin.org/ip\").text == '{\"ip\": true}'\n\n    # Make assertions based on the cassette calls/responses:\n    @pytest.mark.vcr\n    def test_call_count(vcr):\n        assert requests.get(\"http://httpbin.org/get\").text == '{\"get\": true}'\n        assert requests.get(\"http://httpbin.org/ip\").text == '{\"ip\": true}'\n        # See https://vcrpy.readthedocs.io/en/latest/advanced.html for more info\n        # about the Cassette object:\n        assert vcr.play_count == 2\n\nRun your tests:\n\n.. code:: bash\n\n    pytest --record-mode=once test_network.py\n\nDefault recording mode\n~~~~~~~~~~~~~~~~~~~~~~\n\n``pytest-recording`` uses the ``none`` VCR recording mode by default to prevent unintentional network requests.\nTo allow them you need to pass a different recording mode (e.g. ``once``) via the ``--record-mode`` CLI option to your test command.\nSee more information about available recording modes in the `official VCR documentation \u003chttps://vcrpy.readthedocs.io/en/latest/usage.html#record-modes\u003e`_\n\nConfiguration\n~~~~~~~~~~~~~\n\nYou can provide the recording configuration with the ``vcr_config`` fixture, which could be any scope - ``session``,\n``package``, ``module``, or ``function``. It should return a dictionary that will be passed directly to ``VCR.use_cassettes``\nunder the hood.\n\n.. code:: python\n\n    import pytest\n\n    @pytest.fixture(scope=\"module\")\n    def vcr_config():\n        return {\"filter_headers\": [\"authorization\"]}\n\nFor more granular control you need to pass these keyword arguments to individual ``pytest.mark.vcr`` marks, and in this case\nall arguments will be merged into a single dictionary with the following priority (low -\u003e high):\n\n- ``vcr_config`` fixture\n- all marks from the most broad scope (\"session\") to the most narrow one (\"function\")\n\nExample:\n\n.. code:: python\n\n    import pytest\n\n    pytestmark = [pytest.mark.vcr(ignore_localhost=True)]\n\n    @pytest.fixture(scope=\"module\")\n    def vcr_config():\n        return {\"filter_headers\": [\"authorization\"]}\n\n    @pytest.mark.vcr(filter_headers=[])\n    def test_one():\n        ...\n\n    @pytest.mark.vcr(filter_query_parameters=[\"api_key\"])\n    def test_two():\n        ...\n\nResulting VCR configs for each test:\n\n- ``test_one`` - ``{\"ignore_localhost\": True, \"filter_headers\": []}``\n- ``test_two`` - ``{\"ignore_localhost\": True, \"filter_headers\": [\"authorization\"], \"filter_query_parameters\": [\"api_key\"]}``\n\nYou can get access to the used ``VCR`` instance via ``pytest_recording_configure`` hook. It might be useful for registering\ncustom matchers, persisters, etc.:\n\n.. code:: python\n\n    # conftest.py\n\n    def jurassic_matcher(r1, r2):\n        assert r1.uri == r2.uri and \"JURASSIC PARK\" in r1.body, \\\n            \"required string (JURASSIC PARK) not found in request body\"\n\n    def pytest_recording_configure(config, vcr):\n        vcr.register_matcher(\"jurassic\", jurassic_matcher)\n\nYou can disable the VCR.py integration entirely by passing the ``--disable-recording`` CLI option.\n\nRewrite record mode\n~~~~~~~~~~~~~~~~~~~\n\nIt 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.\n\nHowever, it will rewrite only the default cassette and won't touch extra cassettes.\n\n.. code:: python\n\n    import pytest\n\n    @pytest.fixture(scope=\"module\")\n    def vcr_config():\n        return {\"record_mode\": \"rewrite\"}\n\nOr via command-line option:\n\n.. code:: bash\n\n    $ pytest --record-mode=rewrite tests/\n\nBlocking network access\n~~~~~~~~~~~~~~~~~~~~~~~\n\nTo have more confidence that your tests will not go over the wire, you can block it with ``pytest.mark.block_network`` mark:\n\n.. code:: python\n\n    import pytest\n    import requests\n\n    @pytest.mark.block_network\n    def test_multiple():\n        assert requests.get(\"http://httpbin.org/get\").text == '{\"get\": true}'\n\n    ...\n    # in case of access\n    RuntimeError: Network is disabled\n\nBesides marks, the network access could be blocked globally with ``--block-network`` command-line option.\n\nHowever, if VCR.py recording is enabled, the network is not blocked for tests with ``pytest.mark.vcr``.\n\nExample:\n\n.. code:: python\n\n    import pytest\n    import requests\n\n    @pytest.mark.vcr\n    def test_multiple():\n        assert requests.get(\"http://httpbin.org/get\").text == '{\"get\": true}'\n\nRun ``pytest``:\n\n.. code:: bash\n\n    $ pytest --record-mode=once --block-network tests/\n\nThe network blocking feature supports ``socket``-based transports and ``pycurl``.\n\nIt is possible to allow access to specified hosts during network blocking:\n\n.. code:: python\n\n    import pytest\n    import requests\n\n    @pytest.mark.block_network(allowed_hosts=[\"httpbin.*\"])\n    def test_access():\n        assert requests.get(\"http://httpbin.org/get\").text == '{\"get\": true}'\n        with pytest.raises(RuntimeError, match=r\"^Network is disabled$\"):\n            requests.get(\"http://example.com\")\n\nOr via command-line option:\n\n.. code:: bash\n\n    $ pytest --record-mode=once --block-network --allowed-hosts=httpbin.*,localhost tests/\n\n\nOr via `vcr_config` fixture:\n\n.. code:: python\n\n    import pytest\n\n    @pytest.fixture(autouse=True)\n    def vcr_config():\n        return {\"allowed_hosts\": [\"httpbin.*\"]}\n\nAdditional resources\n--------------------\n\nLooking for more examples? Check out `this article \u003chttps://code.kiwi.com/pytest-cassettes-forget-about-mocks-or-live-requests-a9336e1caee6\u003e`_ about ``pytest-recording``.\n\nContributing\n------------\n\nTo run the tests:\n\n.. code:: bash\n\n    $ tox -p all\n\nFor more information, take a look at `our contributing guide \u003chttps://github.com/kiwicom/pytest-recording/blob/master/CONTRIBUTING.rst\u003e`_\n\nPython support\n--------------\n\nPytest-recording supports:\n\n- CPython 3.9, 3.10, 3.11, 3.12, and 3.13\n- PyPy 7.3 (3.10)\n\nLicense\n-------\n\nThe code in this project is licensed under `MIT license`_. By contributing to ``pytest-recording``, you agree that your contributions will be licensed under its MIT license.\n\n\n.. |codecov| image:: https://codecov.io/gh/kiwicom/pytest-recording/branch/master/graph/badge.svg\n   :target: https://codecov.io/gh/kiwicom/pytest-recording\n.. |Build| image:: https://github.com/kiwicom/pytest-recording/actions/workflows/build.yml/badge.svg\n   :target: https://github.com/kiwicom/pytest-recording/actions?query=workflow%3Abuild\n.. |Version| image:: https://img.shields.io/pypi/v/pytest-recording.svg\n   :target: https://pypi.org/project/pytest-recording/\n.. |Python versions| image:: https://img.shields.io/pypi/pyversions/pytest-recording.svg\n   :target: https://pypi.org/project/pytest-recording/\n.. |License| image:: https://img.shields.io/pypi/l/pytest-recording.svg\n   :target: https://opensource.org/licenses/MIT\n\n.. _MIT license: https://opensource.org/licenses/MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiwicom%2Fpytest-recording","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkiwicom%2Fpytest-recording","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiwicom%2Fpytest-recording/lists"}