Backend Development 8 min read

Using extract and validate in HttpRunner for API Automation Testing

This article explains how the extract and validate features of HttpRunner can be used to retrieve data from HTTP responses and assert response correctness, providing multiple practical code examples for token handling, pagination, response time checks, and custom validation functions in API testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using extract and validate in HttpRunner for API Automation Testing

When performing API automation testing with HttpRunner, extract and validate are two essential concepts: extract pulls data from HTTP responses and stores it as variables, while validate defines assertions to ensure the response meets expected criteria.

Extract data : This step extracts key data from the response and saves it as a variable for later use. For example, extracting a token after a successful login:

teststeps:
  - name: Login
    request:
      url: /api/login
      method: POST
      json:
        username: $username
        password: $password
    extract:
      token: content.token

The variable token now holds the extracted value for subsequent steps.

Validate response : This step defines a series of assertions such as status code, response headers, and presence of fields. Continuing the login example:

teststeps:
  - name: Login
    request:
      url: /api/login
      method: POST
      json:
        username: $username
        password: $password
    extract:
      token: content.token
    validate:
      - eq: [status_code, 200]
      - eq: [headers.Content-Type, "application/json"]
      - check: "token" in content

The three assertions verify the status code, content type, and that the token field exists in the response.

Practical application : Combining extract and validate enables complex test flows. For instance, extracting a user ID and then fetching user details:

config:
  name: User Details Test
  base_url: $BASE_URL
teststeps:
  - name: Get User ID
    request:
      url: /api/users
      method: GET
    extract:
      user_id: json[0].id
  - name: Get User Details
    request:
      url: /api/users/$user_id
      method: GET
    validate:
      - eq: [status_code, 200]
      - eq: [json.name, "John Doe"]
      - eq: [json.email, "[email protected]"]

This sequence first retrieves a list of users, extracts the first user's ID, then validates the details of that user.

Extract token and validate status code and headers : For APIs requiring authentication, you can extract a token and use it in subsequent requests:

config:
  name: Test Token Extraction
  base_url: https://api.example.com
teststeps:
  - name: Login and Extract Token
    request:
      url: /login
      method: POST
      json:
        username: user123
        password: pass123
    extract:
      token: content.token
    validate:
      - eq: [status_code, 200]
      - eq: [headers.Content-Type, "application/json"]
  - name: Get User Info Using Token
    request:
      url: /user
      method: GET
      headers:
        Authorization: Bearer $token
    validate:
      - eq: [status_code, 200]
      - check: 'username' in content

The token is reused to access protected resources, and the response is validated.

Validate pagination data : When testing paginated endpoints, you can verify the number of items per page and the last page's boundary condition:

config:
  name: Test Pagination
  base_url: https://api.example.com
teststeps:
  - name: Get First Page
    request:
      url: /items
      method: GET
      params:
        page: 1
        per_page: 10
    validate:
      - eq: [status_code, 200]
      - eq: [json.items | length, 10]
  - name: Get Last Page
    request:
      url: /items
      method: GET
      params:
        page: $last_page_number
        per_page: 10
    extract:
      last_page_items: json.items | length
    validate:
      - eq: [status_code, 200]
      - lt: [$last_page_items, 10]

The first step checks that a full page contains ten items; the second ensures the final page has fewer than ten items.

Validate response time and specific fields : Beyond status codes, you can assert that response time is within limits and that particular fields have expected values:

config:
  name: Test Response Time and Field Validation
  base_url: https://api.example.com
teststeps:
  - name: Get Product Details
    request:
      url: /product/123
      method: GET
    validate:
      - eq: [status_code, 200]
      - lt: [response_time_ms, 500]
      - eq: [json.price, 99.99]
      - eq: [json.in_stock, true]

This verifies the response is fast and that the product data matches expectations.

Use custom functions for validation : HttpRunner allows custom Python functions for complex checks. For example, validating a date string:

config:
  name: Test Custom Function Validation
  base_url: https://api.example.com
teststeps:
  - name: Get Event Date
    request:
      url: /event/next
      method: GET
    validate:
      - eq: [status_code, 200]
      - check: ${is_valid_date(json.event_date)} == True

The custom function is_valid_date ensures the returned event date follows a valid format.

Conclusion : extract and validate are key components in HttpRunner for data extraction and response verification. Proper use of these features enables the creation of complex, reliable API automation test cases that ensure your APIs behave as expected.

BackendAutomationAPI testingHttpRunnerextractvalidate
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.