用途:pytest-recording 封装 VCR.py,把测试中的 HTTP 交互录制为 YAML cassette,之后回放,使测试不依赖真实 API 调用,结果可复现。
主要能力:支持多种录制模式(none 仅回放、once 默认无 cassette 时录制、new_episodes 只补录新请求、all 总是录制并覆盖、rewrite 删除并重录全部);可用 @pytest.mark.vcr() 标记测试,指定自定义或多个 cassette 名称;通过 conftest.py 中的 vcr_config fixture 配置过滤敏感请求头与查询参数、match_on 匹配条件、ignore_hosts 忽略主机、record_mode 等;可用 pytest_recording_configure 设置序列化器、解码压缩响应、在录制前处理响应。
使用方式:运行 uv run pytest tests/ 使用已有 cassette;uv run pytest tests/ --vcr-record=rewrite 重录全部;--vcr-record=new_episodes 只补录缺失;--disable-recording 关闭 VCR 发起真实请求。cassette 默认存放在 tests/cassettes/ 下并按测试模块组织。
适用场景:需要稳定、离线可跑的 API 集成测试,尤其是调用 LLM 提供商(OpenAI、Anthropic、Azure OpenAI、Google AI、Cohere 等)时过滤认证信息。
限制与注意:必须过滤认证头与参数,避免提交密钥;cassette 需随 API 变化定期重录;请求不匹配时需检查 match_on 与 cassette 内容;资料未提供安装命令、版本要求及更多平台细节。