python - 如何将 Tavern 测试的响应保存在 JSON 文件中?

标签 python json rest dictionary pytest

我正在使用 Tavern 工具进行 API 测试,并且我想在执行测试时将返回的响应保存在 JSON 文件中,因此我使用以下代码来响应 yaml 测试文件:

 response:
    status_code: 200
    save:
      $ext:
        function: tavern_utils:save_response

tavern_utils:save_response() 函数:

def save_response(response):
    with open('saved.json','w') as file:
       json.dump(file,response.json())

因此,在使用 pytest 执行测试时,出现以下错误:

TypeError: The Object of type 'TextIOWrapper' is not JSON serializable

如何解决此错误或通过任何其他方法保存响应?

最佳答案

得到解决方案: 只需将 save_response 函数替换为:

def save_response(response):
    filename='file4.json'
    with open(filename, 'w') as f:
        json.dump(response.json(), f)

Yaml 测试文件为:

  response:
    status_code: 200
    body:
      $ext:
        function: tavern_utils:save_response            

关于python - 如何将 Tavern 测试的响应保存在 JSON 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51084123/

相关文章:

python - 在没有 multipart/form-data 的 Flask 中 ReSTfully 上传文件

php - HTTPS 协议(protocol)是否与 REST API Web 服务相关?

python - 如何计算大型 FASTA 文件中包含的序列的氨基酸组成百分比

javascript - 将记录重新插入 extJS 存储

node.js - faker 可以用来创建假的 JSON 数据吗

json - 如何在 F# 中对数学向量类型进行 JSON 序列化?

java - Spring 数据休息: Different resource returned from received

python - 如何在继承树中多次包装子方法?

python - 导入 cv2 终端错误 - python

python - 在 Flask 中保护 REST API