Skip to content

Commit

Permalink
feat: --disable-recording CLI option to completely disable the VCR.…
Browse files Browse the repository at this point in the history
…py integration (#65)
  • Loading branch information
Stranger6667 authored Nov 25, 2020
1 parent ba11b9f commit cb40410
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~

Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------

Expand Down Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions src/pytest_recording/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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."""
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 21 additions & 0 deletions tests/test_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit cb40410

Please sign in to comment.