【问题标题】:How to save response value to an variable in Tavern?如何将响应值保存到 Tavern 中的变量?
【发布时间】:2019-08-26 00:41:55
【问题描述】:

我正在使用Tavern 在python3 中测试我的网络服务器。 Tavern 是基于预定义的yaml 文件测试服务。当向客户端响应自动生成的 id 值时,我有一个后端 API。如何测试自动生成的响应?以及如何将响应保存到变量并将其用于其他测试?

test_name: Test phonebook service

stages:
  - name: Make sure we can create new contact
    request:
     url: http://localhost:8080/v2/contact
     method: POST
     json:
      username: testUser
      first_name: first
      last_name: last
      email: test@email.com
      password: "123456"
      phone: "111111"
    response:
     status_code: 200
     body:
      contact_id: # This is auto generated id from backend

如上例,它向服务器发送一个 POST 请求,并在响应中期望 200 状态码。但我不知道如何检查后端生成的整数 contact_id。 另外,我想将 contact_id 保存在一个变量中,作为其他 API 的输入。

【问题讨论】:

    标签: python tavern


    【解决方案1】:

    可以通过在响应中使用save 来解决,例如:

    response:
         status_code: 200
         headers:
          content-type: application/json
         save:
           body:
            contact_id: id
    

    对于tavern >= 1.0,使用json 而不是body:

    response:
         status_code: 200
         headers:
          content-type: application/json
         save:
           json:
            contact_id: id
    

    它将响应json对象中的id值保存到一个名为contact_id的变量中。然后,您可以通过"{contact_id}" 或"{contact_id:d}" 引用此变量,如果您想将该变量类型转换为“十进制整数”。

    【讨论】:

    • 我可以在不同的测试文件中使用这个变量吗?
    猜你喜欢
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2013-04-26
    • 2014-11-09
    • 1970-01-01
    • 2016-03-31
    • 2021-03-31
    • 1970-01-01
    相关资源
    最近更新 更多