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.
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 httprunnerStep 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.ymlTypical 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: 1Step 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.ymlStep 5: Use environment variables
Define variables in a .env file, e.g.:
BASE_URL=https://api.example.comReference 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.ymlStep 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.htmlConclusion
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.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.