Skip to main content

A pytest plugin that allows to record network interactions via VCR.py

Project description

codecov Build Version Python versions License

A pytest plugin that allows to record network interactions via VCR.py.

Features

  • Straightforward pytest.mark.vcr, that reflects VCR.use_cassettes API;

  • Combining multiple VCR cassettes;

  • Network access blocking.

Usage

import pytest
import requests

@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}'

# 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}'

Configuration

The recording configuration could be provided with 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.

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:

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"]}

Blocking network access

To have more confidence that your tests will not go over the wire, you can block it with pytest.mark.block_network mark:

import pytest
import requests

@pytest.mark.block_network
def test_multiple():
    assert requests.get("http://httpbin.org/get").text == '{"get": true}'

...
# in case of access
RuntimeError: Network is disabled

Besides marks, the network access could be blocked globally with --block-network command-line option.

However, if VCR.py recording is enabled then, the network is not blocked for tests, that have pytest.mark.vcr.

Example:

import pytest
import requests

@pytest.mark.vcr
def test_multiple():
    assert requests.get("http://httpbin.org/get").text == '{"get": true}'

Run pytest:

$ pytest --record-mode=all --block-network tests/

The network blocking feature supports socket-based transports and pycurl.

Contributing

To run the tests:

$ tox -p all

If you have troubles with installing pycurl with tox, you could try to pass CPPFLAGS and LDFLAGS with the tox command:

$  CPPFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib" tox -p all

Python support

Pytest-recording supports:

  • CPython 2.7, 3.5, 3.6, 3.7 and 3.8.

  • PyPy 7 (2.7 & 3.6)

License

The 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-recording-0.3.6.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_recording-0.3.6-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest-recording-0.3.6.tar.gz.

File metadata

  • Download URL: pytest-recording-0.3.6.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for pytest-recording-0.3.6.tar.gz
Algorithm Hash digest
SHA256 d04ec19e3d3cda3e8e9ebaf93f4383fcc4c0bb7424b0533ba2a96eb3c396677d
MD5 1ca3804281003b2dfc19b9a631d19cd6
BLAKE2b-256 92d6e9239d2d71ed5d3689a530cffd235bbf25c34e58b9b47bc19537cb5e72db

See more details on using hashes here.

File details

Details for the file pytest_recording-0.3.6-py3-none-any.whl.

File metadata

  • Download URL: pytest_recording-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for pytest_recording-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 79e533d716b1d5ffc6a621bbad31dd783f03ed3a0b302200336d372ef13eb61e
MD5 cd7f0f9dad83f0ee372db0567bccf3d2
BLAKE2b-256 d74179d33c82fd49b67a075694698667676315004d7823ef715acedbd4c5671c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page