2

I am using Tavern to test my web server in python3. Tavern is to test the service based on a predefined yaml file. I have a backend API when response an auto generated id value to client. How can I test the auto-generated response? And how can I save the response to an variable and use it for other tests?

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

As the above example, it sends a POST request to the server and expect 200 status code in the response. But I don't know how to check the contact_id which is an integer generated by the backend. Also, I'd like to save the contact_id in a variable as the input for other APIs.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

1 Answers1

4

It can be solved by using save in the response like:

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

For tavern >= 1.0, use json instead of body:

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

it will save the value of id from the response json object into a variable called contact_id. You can then reference this variable by "{contact_id}", or "{contact_id:d}" if you want to typecast the variable as 'decimal integer'.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523