Operations 5 min read

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.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using HttpRunner and Locust for API Performance Testing

1. Preparation

Ensure Python (3.7 or higher) is installed and install HttpRunner and Locust via pip: pip install httprunner locust 2. 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-locust For example: hrun test_performance.yml --to-locust This 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.py Locust 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 seconds

You 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.

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.

PythonLoad TestingAPILocustHttpRunner
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.