Using HttpRunner 2.0.1 for API Automation: Setup, Core Commands, Common Issues and Practical Tips
This article introduces HttpRunner 2.0.1 for API automation, explains the environment setup, project structure, essential CLI commands and Python integration, and shares solutions to frequent problems such as timestamp handling, variable overriding, extraction, and execution repeats, along with useful tips for efficient test layering.
Background – While preparing for API automation, the author discovered HttpRunner, a lightweight framework that allows non‑programmers to write tests using JSON or YAML files.
Environment – The demo uses Python 3.7 with HttpRunner 2.0.1.
Project file structure – A project scaffold is created with hrun --startproject demo , where demo is the project name.
Core HttpRunner commands
hrun : alias for the HttpRunner CLI.
locusts : performance testing based on Locust.
har2case : converts HAR files to YAML/JSON test cases.
Frequently used options include -V (version), --log-level , --dot-env-path , --report-template , --report-dir , --html-report-name , --validate , and --prettify .
Running tests
Command‑line mode: hrun XXX.yml --dot-env-path env/staging.env --log-level debug .
Python integration: from httprunner.api import HttpRunner runner = HttpRunner(failfast=False) runner.run("XX.yml") .
Key initialization parameters for HttpRunner include failfast , report_template , report_dir , log_level , and log_file . The run method accepts path_or_tests , dot_env_path , and mapping .
Common issues and solutions
Timestamp expiration – move time‑sensitive parameters to the API layer to generate fresh timestamps at each run.
Variable overriding bug – the parser mistakenly reversed the replacement order; fixing the logic resolves the conflict.
Extracting values not returned by the API – avoid modifying the API response; instead, use post‑request queries or database lookups.
Redundant variables in cases – define default values in the API layer’s variables field and override only needed ones in cases.
Suite layer cannot call APIs directly – the _parse_testsuite method does not process test fields, so APIs must be invoked from lower layers.
Boolean literals in request keys – wrap yes / no in quotes to prevent them being parsed as True / False .
Repeating steps – times works only on API objects; for whole‑case repetition, implement a custom Python loop.
Suite layer cannot use validate – this limitation is inherent to the current version.
Workflow diagram – The article includes a visual representation of HttpRunner’s runtime workflow (image omitted).
Overall, the author shares practical tips and workarounds gathered from real‑project experience to help teams adopt HttpRunner efficiently.
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
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.