Fundamentals 7 min read

Understanding the Test Case Structure of HttpRunner 4.x for API Automation Testing

This article explains the hierarchical structure of HttpRunner 4.x test cases, detailing configuration, parameters, test steps, variables, extraction, validation, hooks, dependencies, and reuse features for effective API automation testing, and provides practical YAML/JSON examples to illustrate each component.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding the Test Case Structure of HttpRunner 4.x for API Automation Testing

HttpRunner 4.x is a framework for HTTP(S) interface automation testing that supports YAML and JSON formats to define test cases. A typical test case contains multiple hierarchical levels, each with a specific purpose.

1. Configuration (config)

The configuration section is the top-level field of a test case, containing metadata and global settings such as test name, base URL, timeout, and SSL verification.

config:
  name: Test Suite Name
  base_url: https://api.example.com
  verify: False
  export: ['token', 'session_id']

2. Parameters (parameters)

The optional parameters section defines parameterized data for the test case, which can be a list or dictionary.

config:
  parameters:
    - user:
        username: ["user1", "user2"]
        password: "secret"

3. Test Steps (teststeps)

Test steps are the core of the test case, containing specific HTTP requests and validation logic. Each step includes a request and a validation part.

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

4. Variables (variables)

The variables section defines variables used in test steps that can be referenced by subsequent steps.

teststeps:
  - name: Get Token
    request:
      url: /token
      method: GET
    extract:
      token: content.token
    variables:
      token: $token

5. Extraction (extract)

The extraction part extracts values from responses and stores them as variables for later steps.

extract:
  token: content.token

6. Validation (validate)

The validation section defines expected response results, typically including status code checks and body content verification.

validate:
  - eq: [status_code, 200]
  - check: 'token' in content

7. Hooks (hooks)

Hooks allow custom code execution at certain points in the test case lifecycle, such as before_all or after_all.

config:
  hooks:
    - before_all: setup_environment()
    - after_all: teardown_environment()

8. Dependencies (dependencies)

HttpRunner supports dependencies between test steps, meaning a step’s execution depends on the success of a previous step.

teststeps:
  - name: Step 1
    request:
      url: /step1
      method: GET
  - name: Step 2
    dependencies:
      - Step 1
    request:
      url: /step2
      method: GET

9. Reuse (reuse)

Reuse enables importing and reusing test steps from other test cases to reduce duplication.

include:
  - path: common_steps.yml

Complete Example

Combining all elements, a full HttpRunner test case may look like:

config:
  name: Test Suite for API
  base_url: https://api.example.com
  verify: False
  export: ['token']
teststeps:
  - name: Login
    variables:
      username: admin
      password: secret
    request:
      url: /login
      method: POST
      headers:
        Content-Type: application/json
      json:
        username: $username
        password: $password
    validate:
      - eq: [status_code, 200]
      - check: 'token' in content
    extract:
      token: content.token
  - name: Get User Info
    request:
      url: /user
      method: GET
      headers:
        Authorization: Bearer $token
    validate:
      - eq: [status_code, 200]
      - check: 'username' in content

Advanced Features

The framework also supports function calls within test steps, allowing dynamic data generation or complex logic via custom functions.

teststeps:
  - name: Generate Payload
    request:
      url: /create-resource
      method: POST
      json:
        id: ${generate_random_id()}

In summary, HttpRunner 4.x provides a flexible, hierarchical structure for building API automation tests, combining configuration, parameterization, stepwise execution, data extraction, validation, hooks, dependencies, and reuse to accommodate various testing scenarios while maintaining readability and maintainability.

test automationJSONyamlAPI TestingHttpRunnerTest case structure
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.