Backend Development 7 min read

HttpRunner Overview: Features, Sample Test Cases, and Integration with Requests

This article introduces the open‑source HttpRunner API testing tool, outlines its key features such as YAML/JSON test definitions, parameterization, assertions, reporting and performance testing, and provides step‑by‑step Python examples showing how to write, run, and integrate HttpRunner tests with existing requests‑based projects.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
HttpRunner Overview: Features, Sample Test Cases, and Integration with Requests

HttpRunner is a powerful open‑source API testing tool that builds on the Python requests library and adds capabilities for automated functional testing, performance testing, and extensible plugins.

Key Features

Simple YAML/JSON format for defining test cases, making them easy to read and write.

High extensibility: integrates smoothly with existing Python environments, supports custom functions and variables.

Parameterized testing: data‑driven tests via CSV files or inline parameters.

Rich assertion mechanisms to validate response content.

Detailed test reports for result analysis.

Performance testing support through integration with the Locust framework.

Plugin ecosystem for adding extra functionality.

Example YAML Test Case

# test_user_info.yml
config:
  name: "Test User Info API"
  base_url: "http://api.example.com"
teststeps:
- name: "获取用户信息"
  request:
    method: GET
    url: "/users/1"
    headers:
      Content-Type: "application/json"
  validate:
    - eq: ["status_code", 200]
    - contains_string: ["response.body.username", "testuser"]

This example defines a test named "Test User Info API" with a base URL, sends a GET request to /users/1 , and validates that the status code is 200 and the response body contains the username "testuser".

Running the Test

hrun test_user_info.yml

Integrating HttpRunner into an Existing Project

After installing HttpRunner ( pip install httprunner ), you can load and execute test cases from Python code:

from httprunner import loader, runner

def main():
    # Load test cases from the ./tests directory
    testcases = loader.load_folder_files("./tests")
    # Create a runner instance
    test_runner = runner.Runner()
    for testcase in testcases:
        result = test_runner.run(testcase)
        print(f"Test case {testcase['config']['name']} executed with result: {result}")

if __name__ == "__main__":
    main()

This script loads all .yml or .json test files in the specified folder and runs them sequentially.

Full Project Structure Example

my_test_framework/
│
├── tests/
│   ├── test_user_info.yml
│   └── test_another_api.yml
│
├── utils/
│   └── api_client.py
│
└── run_tests.py

utils/api_client.py

import requests

class APIClient:
    def __init__(self, base_url):
        self.base_url = base_url

    def get(self, endpoint, headers=None):
        return requests.get(f"{self.base_url}{endpoint}", headers=headers)

    def post(self, endpoint, data=None, headers=None):
        return requests.post(f"{self.base_url}{endpoint}", json=data, headers=headers)

run_tests.py

from httprunner import loader, runner
from utils.api_client import APIClient

def load_and_run_httprunner_tests(tests_dir="./tests"):
    testcases = loader.load_folder_files(tests_dir)
    test_runner = runner.Runner()
    results = []
    for testcase in testcases:
        result = test_runner.run(testcase)
        results.append((testcase['config']['name'], result))
    return results

def main():
    client = APIClient("http://api.example.com")
    response = client.get("/users/1")
    print(f"Direct Request Response Status Code: {response.status_code}")
    results = load_and_run_httprunner_tests()
    for name, result in results:
        print(f"HttpRunner Test Case '{name}' Result: {result}")

if __name__ == "__main__":
    main()

The above examples demonstrate how to combine direct requests calls with HttpRunner‑managed test cases, giving you flexibility to choose the most suitable approach for different API testing scenarios.

pythonautomationAPI testingrequestsHttpRunner
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.