跳转到主要内容

pytest插件,允许使用VCR.py录制网络交互

项目描述

codecov Build Version Python versions License

一个pytest插件,可以通过VCR.py录制测试中的网络交互。

功能

  • 简单的pytest.mark.vcr,反映了VCR.use_cassettes API;

  • 组合多个VCR磁带;

  • 网络访问阻止;

  • 重新编写记录模式,从头开始重新编写磁带。

安装

此项目可以通过pip安装

pip install pytest-recording

⚠️此项目与pytest-vcr不兼容,请在⚠️之前确保已卸载

使用

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

运行您的测试

pytest --record-mode=once test_network.py

默认记录模式

pytest-recording 默认使用 none VCR 录制模式以防止意外网络请求。要允许它们,您需要通过 --record-mode 命令行选项将不同的录制模式(例如 once)传递给测试命令。有关可用的录制模式的更多信息,请参阅官方 VCR 文档

配置

您可以使用 vcr_config 固定装置提供录制配置,其范围可以是任何范围 - sessionpackagemodulefunction。它应该返回一个字典,该字典将直接传递给底层的 VCR.use_cassettes

import pytest

@pytest.fixture(scope="module")
def vcr_config():
    return {"filter_headers": ["authorization"]}

为了更细粒度的控制,您需要将这些关键字参数传递给单个 pytest.mark.vcr 标记,在这种情况下,所有参数将合并成一个具有以下优先级的单个字典(从低到高)

  • vcr_config 固定装置

  • 从最广泛的范围(“session”)到最狭窄的范围(“function”)的所有标记

示例

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():
    ...

每个测试的结果 VCR 配置

  • test_one - {"ignore_localhost": True, "filter_headers": []}

  • test_two - {"ignore_localhost": True, "filter_headers": ["authorization"], "filter_query_parameters": ["api_key"]}

您可以通过 pytest_recording_configure 钩子获取访问使用的 VCR 实例。这可能会对注册自定义匹配器、持久化程序等非常有用。

# 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)

您可以通过传递 --disable-recording 命令行选项来完全禁用 VCR.py 集成。

重写录制模式

您可以从头重新编写磁带,而不是像现在使用 VCR.py 的 all 录制模式那样扩展它。

但是,它只会重写默认磁带,而不会触摸额外磁带。

import pytest

@pytest.fixture(scope="module")
def vcr_config():
    return {"record_mode": "rewrite"}

或者通过命令行选项

$ pytest --record-mode=rewrite tests/

阻止网络访问

要更有信心确保测试不会通过网络进行,您可以使用 pytest.mark.block_network 标记来阻止它

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

除了标记之外,可以使用 --block-network 命令行选项全局阻止网络访问。

但是,如果启用了 VCR.py 录制,则带有 pytest.mark.vcr 的测试不会阻止网络。

示例

import pytest
import requests

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

运行 pytest

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

网络阻塞功能支持基于 socket 的传输和 pycurl

您可以在网络阻塞期间允许访问特定的主机

import pytest
import requests

@pytest.mark.block_network(allowed_hosts=["httpbin.*"])
def test_access():
    assert requests.get("http://httpbin.org/get").text == '{"get": true}'
    with pytest.raises(RuntimeError, match=r"^Network is disabled$"):
        requests.get("http://example.com")

或者通过命令行选项

$ pytest --record-mode=once --block-network --allowed-hosts=httpbin.*,localhost tests/

或者通过 vcr_config 固定装置

import pytest

@pytest.fixture(autouse=True)
def vcr_config():
    return {"allowed_hosts": ["httpbin.*"]}

其他资源

想了解更多示例?请参阅有关 pytest-recording 的这篇文章

贡献

要运行测试

$ tox -p all

有关更多信息,请参阅我们的贡献指南

Python 支持

Pytest-recording 支持

  • CPython 3.7、3.8、3.9、3.10、3.11 和 3.12

  • PyPy 7 (3.6)

许可证

此项目的代码根据 MIT 许可证 许可。通过向 pytest-recording 贡献,您同意您的贡献将在其 MIT 许可证下许可。

项目详情


下载文件

下载适用于您平台文件的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源代码分布

pytest_recording-0.13.2.tar.gz (25.3 kB 查看哈希值)

上传时间 源代码

构建分布

pytest_recording-0.13.2-py3-none-any.whl (12.8 kB 查看哈希值)

上传时间 Python 3

支持者