From Zero to Automation: Mastering API Testing with HttpRunner and Apifox

This article shares a small team's journey from scratch to advanced API testing, detailing how they adopted HttpRunner, upgraded to its pytest‑compatible v3.x, and later integrated Apifox for streamlined documentation, mocking, and automated tests, while highlighting practical setup, hooks, and lessons learned.

BaiPing Technology
BaiPing Technology
BaiPing Technology
From Zero to Automation: Mastering API Testing with HttpRunner and Apifox

This article introduces a small testing team's experiences with API testing, describing the tools and practices they adopted.

From 0 to 0.1 – Introducing HttpRunner

Starting from almost zero, the team chose HttpRunner after seeing its popularity on TesterHome. The project structure is simple and beginner‑friendly.

├── .env
├── .gitignore
├── debugtalk.py
├── data
├── reports
└── testcases
    ├── demo_testcase1.yml
    └── demo_testcase2.yml

Installation and basic usage steps:

Run pip3 install httprunner;

Check version with hrun -V;

Create a project quickly using httprunner startproject PROJECT_NAME;

Modify the generated demo according to the project;

Execute hrun to view the report.

The team used hooks to handle time‑sensitive or encrypted parameters: setup_hooks for preparation (e.g., signing) and teardown_hooks for cleanup (e.g., saving tokens).

-   name: /xxxx/xxx/v1/codeLogin --验证码登录
    request:
        headers:
            signature: ??????
            aaaaaaaaa: ??????
            bbbbbbbbb: ??????
        json:
            CCCCCCCCC: ??????
            DDDDDDDDD: ??????
            phone: $phone
        method: POST
        url: xxxx/xxx/v1/codeLogin
    setup_hooks:
        - ${set_signatures($request)}
    teardown_hooks:
        - ${save_token($response)}

With these hooks, the login test case encrypts parameters before the request and saves the token after the response.

0.1 to 1.0 – HttpRunner V3.x

As the team grew, repetitive manual steps became tedious. Learning pytest, they upgraded to HttpRunner 3.x, which integrates seamlessly with pytest, allowing the whole team to contribute and reinforcing their Python skills.

During the migration, they discovered that test cases must be independent; test suites are used for batch execution. Proper modular design from the start avoids dependency errors.

1.0 to 2.0 – Apifox

Facing limited resources, the team introduced Apifox, an all‑in‑one platform for API documentation, debugging, mocking, and automated testing, eliminating the need to redefine data across tools.

Apifox combines Postman, Swagger, Mock, and JMeter into a single system, enabling API documentation, debugging, mocking, and automated testing with one set of data.

Key steps for using Apifox:

Project settings → Import data → Modify configuration → Import.

Configure global variables and environments.

Set up pre‑ and post‑scripts. Example pre‑script:

var sign = createSignatureJson(pm.request.method, req);
pm.request.body.raw = JSON.stringify(sign.sortData);
pm.request.headers.add({ key: 'signature', value: sign.signature });

Example post‑script:

var jsonData = pm.response.json();
var data = JSON.parse(jsonData.data);
pm.globals.set('token', data.token);
pm.globals.set('member_id', data.member_id);

Additional workflow:

Add new interfaces via Interface Management.

Create test cases under Automated Testing.

Run cases and view reports.

Summary

The article concludes that the choice of tools should prioritize ease of use and team familiarity rather than chasing the latest technology; tools are meant to boost efficiency, not become the focus.

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.

hooksAPI testingpytestHttpRunnerApifox
BaiPing Technology
Written by

BaiPing Technology

Official account of the BaiPing app technology team. Dedicated to enhancing human productivity through technology. | DRINK FOR FUN!

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.