我正在使用Tavern测试我的REST端点。基本上,我所做的就是执行一个GET请求,尝试保存响应,然后将该响应的一部分用于后续的更新测试。
# test_.tavern.yaml file
# GET
test_name: Get Rules by ServiceCode
stages:
- name: Get Rules by a ServiceCode
request:
method: GET
url: http://localhost:9000/record/v1/admin/rules
params:
service_code: "ZEL302" #hardcoded
response:
status_code: 200
save:
$ext:
function: rule:save_rule_no # saving the results into a variable called "saved_rule_no"
---
# UPDATE
test_name: Update Rule
stages:
- name: Update a Rule
request:
method: PUT
url: "http://localhost:9000/record/v1/admin/rules/69"
json:
ruleNo: !int "{saved_rule_no}" # use the saved variable here
category: "tavernc"
identifier: "taverni"
operator: "taverno"
value: "tavernv"
productId: "tavernpid"
serviceCode: "tavernsc"
templateNo: 1 #hardcoded
response:
status_code: 200# rule.py file
def save_rule_no(response):
"""
After creating a Rule, get the Rule's ruleNo field to use for testing update/delete
"""
ruleNo = response.json()[0]["ruleNo"]
return Box({"saved_rule_no": ruleNo})我得到下面的错误,说变量不能被识别。此方法适用于Tavern 0.34.0,但不适用于1.0.0。对于1.0.0,您将如何做到这一点?
scenario/test_.tavern.yaml::Insert Rules PASSED [ 25%]
scenario/test_.tavern.yaml::Get Rules by ProductId PASSED [ 50%]
scenario/test_.tavern.yaml::Get Rules by ServiceCode PASSED [ 75%]
scenario/test_.tavern.yaml::Update Rule FAILED [100%]
========================================================================= FAILURES =========================================================================
Format variables:
saved_rule_no = '???'发布于 2020-10-07 02:28:30
Tavern不支持在测试之间使用保存的变量。
保存的变量仅可用于单个测试中的所有阶段。
发布于 2021-01-06 09:46:42
1.创建一个scope = session的fixture
2. fixture应该返回您将在所有测试中使用的数据
3.在测试中连接fixture
文档:https://tavern.readthedocs.io/en/latest/basics.html#usefixtures
https://stackoverflow.com/questions/62565431
复制相似问题