Backend Development 7 min read

Using HttpRunner for API Automation: Setup, Commands, Workflow, and Common Issues

This article introduces HttpRunner for API automation, covering environment setup, project structure, core commands, execution methods, workflow, and a collection of practical problems and solutions encountered during real‑world usage.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Using HttpRunner for API Automation: Setup, Commands, Workflow, and Common Issues

Background : While preparing for API automation, the author discovered HttpRunner, a lightweight framework that allows non‑programmers to quickly write tests using JSON or YAML files.

Environment : The automation uses Python 3.7 with HttpRunner 2.0.1.

Project file structure : Create a layered test case structure with the command hrun --startproject demo , where demo is the project name.

Core HttpRunner commands :

-V : show the current HttpRunner version

--startproject : generate a project skeleton

--log-level : set log level (default info , use debug for detailed logs)

--dot-env-path : specify a .env file to load host variables for different environments

--report-template and --report-dir : customize report template and output directory

--html-report-name : rename the generated HTML report

--validate : validate JSON/YAML test case format

--prettify : format JSON test cases

Running HttpRunner :

Command‑line mode: hrun XXX.yml --dot-env-path env/staging.env --log-level debug

Programmatic mode: import and use the API in Python: from httprunner.api import HttpRunner runner = HttpRunner(failfast=False) runner.run("XX.yml")

The HttpRunner constructor accepts parameters such as failfast , save_tests , report_template , report_dir , log_level , and log_file . The run method takes path_or_tests , dot_env_path , and mapping arguments.

Known issues and solutions :

Timestamp expiration : Move time‑sensitive parameters from the case layer to the API layer so they are generated at runtime.

Variable overriding bug : The framework incorrectly replaces variables when the same name appears in both config and a test; the replacement logic was reversed.

Extracting missing response fields : Instead of modifying the API to return extra data, use a separate query (e.g., SQL) to obtain the needed value and store it via extract .

Reducing redundant variables : Define default values for all parameters in the API layer’s variables field and reference them with $ ; cases only need to override the ones they change.

Suite layer limitations : Suites cannot directly call APIs, cannot use the validate field, and cannot apply the times keyword; work‑arounds include writing a custom Python loop to repeat calls.

Boolean key handling : Enclose keys like yes or no in quotes to avoid being parsed as booleans.

Workflow diagram (illustrated in the original article) shows the sequence from project initialization, test case definition, execution, to report generation.

Overall, the article shares practical tips and troubleshooting experiences for using HttpRunner 2.0.1 in a real project.

BackendpythonAutomationTest FrameworkAPI testingHttpRunner
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

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.