Using HttpRunner and Locust for API Performance Testing
This guide walks through installing HttpRunner and Locust, creating a simple YAML test case, converting it to a Locust script, running load tests, customizing behavior, and analyzing performance metrics to evaluate API reliability under load.
1. Preparation
Ensure Python (3.7 or higher) is installed and install HttpRunner and Locust via pip:
pip install httprunner locust2. Write a basic test case
Create a YAML file test_performance.yml that sends a GET request to https://jsonplaceholder.typicode.com/posts and validates that the response status code is 200:
# test_performance.yml
config:
name: "Performance Test Case"
base_url: "https://jsonplaceholder.typicode.com"
teststeps:
- name: "获取帖子列表"
request:
method: GET
url: "/posts"
headers:
Content-Type: "application/json"
validate:
- eq: ["status_code", 200]3. Convert HttpRunner test case to a Locust script
Use the following command to generate locustfile.py :
hrun {your_testcase_path} --to-locustFor example:
hrun test_performance.yml --to-locustThis creates a locustfile.py that defines the performance test tasks for Locust.
4. Execute the performance test
Run Locust with the generated script:
locust -f locustfile.pyLocust starts a web UI at http://localhost:8089 where you can set the total number of users, hatch rate, and start the test.
5. Advanced configuration and optimization
Customize Locust behavior by editing locustfile.py . For example, add think time between requests:
from locust import between
class MyUser(HttpUser):
wait_time = between(1, 5) # random wait between 1 and 5 secondsYou can also use environment variables to pass configuration options such as target host or user count.
6. Analyze the performance test report
During the test, Locust’s UI displays metrics including Requests per second (RPS), average response time, failures, current RPS, and median response time. You can download a CSV report for deeper analysis.
7. Summary
By following these steps, even beginners can combine HttpRunner and Locust to perform API performance testing, evaluate system behavior under different loads, and adjust parameters to better simulate real‑world usage scenarios.
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.