From cb40410652894fc1c6f36135ede6747e27d4d8db Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Wed, 25 Nov 2020 14:49:11 +0100 Subject: [PATCH] feat: `--disable-recording` CLI option to completely disable the VCR.py integration (#65) --- README.rst | 2 ++ docs/changelog.rst | 6 ++++++ src/pytest_recording/plugin.py | 25 ++++++++++++++++++++++--- tests/test_recording.py | 21 +++++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 7f44400..64f1a86 100644 --- a/README.rst +++ b/README.rst @@ -97,6 +97,8 @@ custom matchers, persisters, etc.: 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 ~~~~~~~~~~~~~~~~~~~ diff --git a/docs/changelog.rst b/docs/changelog.rst index 6e2640b..f27146d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,11 @@ Changelog `Unreleased`_ ------------- +Added +~~~~~ + +- ``--disable-recording`` CLI option to completely disable the VCR.py integration. `#64`_ + `0.10.0`_ - 2020-10-06 ---------------------- @@ -171,6 +176,7 @@ Added .. _0.3.0: https://github.com/kiwicom/pytest-recording/compare/v0.2.0...v0.3.0 .. _0.2.0: https://github.com/kiwicom/pytest-recording/compare/v0.1.0...v0.2.0 +.. _#64: https://github.com/kiwicom/pytest-recording/issues/64 .. _#55: https://github.com/kiwicom/pytest-recording/issues/55 .. _#53: https://github.com/kiwicom/pytest-recording/issues/53 .. _#47: https://github.com/kiwicom/pytest-recording/issues/47 diff --git a/src/pytest_recording/plugin.py b/src/pytest_recording/plugin.py index 8e29c0b..b014239 100644 --- a/src/pytest_recording/plugin.py +++ b/src/pytest_recording/plugin.py @@ -51,6 +51,12 @@ def pytest_addoption(parser: Parser) -> None: default=None, help="List of regexes, separated by comma, to match hosts to where connection must be allowed.", ) + group.addoption( + "--disable-recording", + action="store_true", + default=False, + help="Disable VCR.py integration.", + ) def pytest_addhooks(pluginmanager: PytestPluginManager) -> None: @@ -63,6 +69,12 @@ def record_mode(request: SubRequest) -> str: return request.config.getoption("--record-mode") +@pytest.fixture(scope="session") # type: ignore +def disable_recording(request: SubRequest) -> bool: + """Disable VCR.py integration.""" + return request.config.getoption("--disable-recording") + + @pytest.fixture # type: ignore def vcr_config() -> Dict: """A shareable configuration for VCR.use_cassette call.""" @@ -92,11 +104,18 @@ def block_network(request: SubRequest, record_mode: str) -> Iterator[None]: @pytest.fixture(autouse=True) # type: ignore -def vcr( - request: SubRequest, vcr_markers: List[Mark], vcr_cassette_dir: str, record_mode: str, pytestconfig: Config +def vcr( # pylint: disable=too-many-arguments + request: SubRequest, + vcr_markers: List[Mark], + vcr_cassette_dir: str, + record_mode: str, + disable_recording: bool, + pytestconfig: Config, ) -> Iterator[Optional[Cassette]]: """Install a cassette if a test is marked with `pytest.mark.vcr`.""" - if vcr_markers: + if disable_recording: + yield None + elif vcr_markers: config = request.getfixturevalue("vcr_config") default_cassette = request.getfixturevalue("default_cassette_name") with use_cassette( diff --git a/tests/test_recording.py b/tests/test_recording.py index f8b4aa5..7532fc4 100644 --- a/tests/test_recording.py +++ b/tests/test_recording.py @@ -43,6 +43,27 @@ def test_without_network(): assert not cassette_path.exists() +def test_disable_recording(testdir): + testdir.makepyfile( + """ + import pytest + import requests + + @pytest.mark.vcr + def test_(httpbin): + assert requests.get(httpbin.url + "/get").status_code == 200 + """ + ) + + # If recording is disabled + result = testdir.runpytest("--disable-recording") + result.assert_outcomes(passed=1) + + # Then there should be no cassettes + cassette_path = testdir.tmpdir.join("cassettes/test_disable_recording/test_.yaml") + assert not cassette_path.exists() + + def test_record_mode_in_mark(testdir): # See GH-47 testdir.makepyfile(