VCRPY记录盒式磁带在使用test_client.post的测试中不记录



我正在实现pytest,我想使用vcrpy记录请求的答案。

我使用的是烧瓶1.0.2,pytest 4.5.0,pytest-flask 0.15.0和vcrpy 2.0.1。我使用烧瓶实现了一个安息的API。我写了一个测试,但我想记录请求的响应,而VCR没有记录响应。

我的终点是:

from flask import jsonify
from flask import Blueprint
class CompanyResource(Resource):
    def post(self):
        response = jsonify({
            "timestamp": datetime.utcnow()
        })
        response.status_code = 201
        return response

api_bp = Blueprint('dashboard', __name__)
api = Api(api_bp)
api.add_resource(CompanyResource, 'company', endpoint='company')

测试是以下

import pytest
import vcr
from flask import url_for
base_vcr = vcr.VCR(
    cassette_library_dir='tests/dashboard/fixtures',
    record_mode='once'
)

def test_create_company(client, company_data, headers):
      with base_vcr.use_cassette('create_user.yaml'):
           response = client.post(url_for('dashboard.company'),
                                  json=company_data,
                                  headers=headers)

我希望在指定的目录上自动生成一个文件。但是当我运行测试时,盒式盒子没有生成。问题是,如果我通过 requests.post更改client.post,则记录了get请求的响应,但是如果我尝试使用帖子,则出现错误:

requests.exceptions.MissingSchema: Invalid URL '/api/dashboard/company': No schema supplied. Perhaps you meant http:///api/dashboard/company?

您是否有任何建议来测试我的帖子端点并记录答案?

这有点迟了,但是vcrpy具有记录HTTP请求的库列表。requests是其中之一,其他内容在https://vcrpy.readthedocs.io/en/latest/installation.html#compatibility中提到。

  • aiohttp
  • boto
  • boto3
  • http.client
  • httplib2
  • 请求(1.x和2.x版本(
  • 龙卷风.httpclient
  • urllib2
  • urllib3

不确定您在Pytest中使用的测试客户端固定件,但是如果您使用requests成功调用端点,则应为您记录响应。

最新更新