Fundamentals 7 min read

Using Variables in HttpRunner 4.x: Declaration, Reference, and Dynamic Assignment

HttpRunner 4.x enables flexible variable declaration and usage at both configuration and test-step levels, supporting $ and {{}} syntax, dynamic assignment from responses, parameterization, request customization, extraction, custom functions, and validation, allowing more maintainable and powerful API test cases.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Variables in HttpRunner 4.x: Declaration, Reference, and Dynamic Assignment

In HttpRunner 4.x, variables are a core feature that let you store and reuse data across test cases, whether for input parameters, extracting key response data, or building dynamic request payloads.

Variable Declaration

Variables can be declared at the configuration level (available throughout the test case) or at the test‑step level (scoped to that step only).

Configuration‑level variable declaration example:

config:
  name: Test Case Example
  base_url: http://example.com
  variables:
    username: testuser
    password: testpass
    api_key: your_api_key
  teststeps:
    - name: Login
      request:
        url: /login
        method: POST
        json:
          username: $username
          password: $password

Test‑step‑level variable declaration example:

teststeps:
  - name: Get Token
    variables:
      token_endpoint: /token
    request:
      url: ${{vars.token_endpoint}}
      method: GET

Variable Reference

HttpRunner supports two syntaxes for referencing variables: the simple $variable_name form and the double‑brace {{vars.variable_name}} form (mainly for step‑level variables).

Variable reference example:

teststeps:
  - name: Get User Info
    variables:
      user_id: 12345
    request:
      url: /users/$user_id
      method: GET

Dynamic Variable Assignment

You can assign variables dynamically during a test step, typically after extracting data from a response.

Dynamic assignment example:

teststeps:
  - name: Login and Get Token
    request:
      url: /login
      method: POST
      json:
        username: $username
        password: $password
    validate:
      - eq: [status_code, 200]
    extract:
      token: content.data.token
  - name: Use Token
    request:
      url: /protected
      headers:
        Authorization: Bearer $token
      method: GET

Example 1: Parameterization

config:
  name: Test parameterization
  base_url: http://httpbin.org
  parameters:
    - name: [John, Jane, Doe]
    - age: [25, 30, 35]
teststeps:
  - name: Greeting
    variables:
      greeting: Hello, $name!
      age_group: $age
    request:
      url: /get
      method: GET
      params:
        greeting: $greeting
        age: $age_group
    validate:
      - eq: [status_code, 200]

Example 2: Using variables in a request

config:
  name: Test variable usage in request
  base_url: http://httpbin.org
  variables:
    endpoint: /get
    header_key: X-Custom-Header
    header_value: MyHeaderValue
teststeps:
  - name: Custom Request
    request:
      url: $endpoint
      method: GET
      headers:
        $header_key: $header_value
    validate:
      - eq: [status_code, 200]

Example 3: Extracting variables from a response

config:
  name: Test extracting variables from response
  base_url: http://httpbin.org
teststeps:
  - name: Post Data
    request:
      url: /post
      method: POST
      json:
        key: value
    extract:
      post_response: content.json
    validate:
      - eq: [status_code, 200]
  - name: Check Post Response
    request:
      url: /get
      method: GET
      params:
        response: $post_response
    validate:
      - eq: [status_code, 200]

Example 4: Using a custom function to generate a variable

config:
  name: Test using custom functions
  base_url: http://httpbin.org
teststeps:
  - name: Get Random String
    request:
      url: /get
      method: GET
      params:
        random_str: ${generate_random_string()}
    validate:
      - eq: [status_code, 200]

Example 5: Using variables in validation

config:
  name: Test using variables in validation
  base_url: http://httpbin.org
teststeps:
  - name: Get Response
    request:
      url: /get
      method: GET
    extract:
      response_time: response.elapsed.total_seconds
    validate:
      - lt: [$response_time, 1]

These examples demonstrate the many ways variables can be leveraged in HttpRunner, including parameterization, request customization, response extraction, custom function calls, and validation, enabling the creation of more powerful and dynamic test cases.

Summary

By using variables, you make your test cases more flexible and maintainable; they allow you to store and reuse data, which is especially useful for handling dynamic or complex data flows. Proper declaration and referencing of variables greatly enhance the robustness and effectiveness of your HttpRunner test suites.

variablesAPI testingparameterizationHttpRunnerdynamic assignment
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.