【问题标题】:Saving Variables in Tavern 1.0.0在 Tavern 1.0.0 中保存变量
【发布时间】:2020-06-24 23:11:05
【问题描述】:

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

# 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 = '???'

【问题讨论】:

    标签: python api rest testing tavern


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      1.创建一个范围=会话的fixture

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

      3.在测试中连接夹具

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-07
        • 2017-12-31
        • 1970-01-01
        • 2022-01-27
        • 1970-01-01
        相关资源
        最近更新 更多