Getting Started with httprunner 4.x for API Automation Testing in Python

This guide explains how to set up httprunner 4.x, write YAML/JSON test cases, run them, use parameterization and environment variables, and generate HTML reports for efficient Python API automation testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Getting Started with httprunner 4.x for API Automation Testing in Python

httprunner 4.x is a powerful Python framework for API automation testing, offering improved asynchronous support, concise YAML/JSON test case formats, and robust parameterization features.

Step 1: Install httprunner

Ensure Python 3.7+ is installed, then run: pip install httprunner Step 2: Create a test case

Define a test case in YAML (or JSON). Example:

# testcases/simple_get.yml
config:
  name: Simple GET request
  base_url: https://httpbin.org
  verify: False
teststeps:
  - name: GET request
    request:
      url: /get
      method: GET
      params:
        hello: world
    validate:
      - eq: [status_code, 200]
      - eq: [headers.Content-Type, "application/json"]

This test sends a GET request to https://httpbin.org/get and validates the status code and Content‑Type header.

Step 3: Run the test case httprunner run testcases/simple_get.yml Typical output:

TestRunner: testcases/simple_get.yml
Total tests: 1
Ran 1 test in 0.123s, 8.120 tests/s
Summary:
- success: 1
- failure: 0
- error: 0
- skipped: 0
- total: 1

Step 4: Parameterized testing

Use lists or files as parameter sources. Example:

# testcases/param_test.yml
config:
  name: Parameterized test
  parameters:
    - user:
        username: ["user1", "user2"]
        password: "secret"
  base_url: https://httpbin.org
  verify: False
teststeps:
  - name: Login with different users
    variables:
      username: $user.username
      password: $user.password
    request:
      url: /post
      method: POST
      json:
        username: $username
        password: $password
    validate:
      - eq: [status_code, 200]

Run it with: httprunner run testcases/param_test.yml Step 5: Use environment variables

Define variables in a .env file, e.g.: BASE_URL=https://api.example.com Reference them in a test case:

# testcases/env_test.yml
config:
  name: Environment test
  base_url: $BASE_URL
  verify: False
teststeps:
  - name: GET request using env var
    request:
      url: /get
      method: GET
    validate:
      - eq: [status_code, 200]

Execute with: httprunner run testcases/env_test.yml Step 6: Generate an HTML test report

Run the test with the --html option to produce a report:

httprunner run testcases/simple_get.yml --html reports/report.html

Conclusion

httprunner 4.x provides a flexible framework for Python API testing; by following these steps you can quickly create, run, and report on test cases, and later extend them with advanced assertions, hooks, and logging as needed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonautomationYAMLAPI testingparameterization
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

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.