Tavern 是一个开源的 API 测试工具,它使用 YAML 文件来编写测试用例。在 Tavern 1.0.0 中,保存变量可以通过几种方式实现,主要涉及到测试用例中的 variables
和 extract
关键字。
extract
关键字用于从响应中提取数据并保存到变量中。以下是一个简单的 Tavern 测试用例示例,展示了如何保存和使用变量:
test_name: Save and use a variable
stages:
- name: Get a token
request:
method: POST
url: /auth/token
json:
username: user
password: pass
response:
status_code: 200
save:
body:
token: token
- name: Use the token to access a protected resource
request:
method: GET
url: /resource
headers:
Authorization: "Bearer {{token}}"
response:
status_code: 200
原因:
extract
或 save
关键字使用不正确。解决方法:
response:
status_code: 200
save:
body:
token: token
debug: true # 添加此行以启用调试输出
通过以上步骤,可以有效地在 Tavern 中保存和使用变量,确保测试用例的可靠性和可维护性。
领取专属 10元无门槛券
手把手带您无忧上云