首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Tavern 1.0.0中保存变量

在Tavern 1.0.0中保存变量
EN

Stack Overflow用户
提问于 2020-06-25 07:11:06
回答 2查看 417关注 0票数 0

我正在使用Tavern测试我的REST端点。基本上,我所做的就是执行一个GET请求,尝试保存响应,然后将该响应的一部分用于后续的更新测试。

代码语言:javascript
复制
# 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
代码语言:javascript
复制
# 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,您将如何做到这一点?

代码语言:javascript
复制
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 = '???'
EN

回答 2

Stack Overflow用户

发布于 2020-10-07 02:28:30

Tavern不支持在测试之间使用保存的变量。

保存的变量仅可用于单个测试中的所有阶段。

票数 1
EN

Stack Overflow用户

发布于 2021-01-06 09:46:42

1.创建一个scope = session的fixture

2. fixture应该返回您将在所有测试中使用的数据

3.在测试中连接fixture

文档:https://tavern.readthedocs.io/en/latest/basics.html#usefixtures

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62565431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档