HttpRunner is an open-source API automation testing framework designed to simplify API testing and performance testing. It supports writing test cases in YAML/JSON format, featuring high usability and strong extensibility, making it very suitable for testing engineers and developers.
Below, we will introduce this testing framework from the following aspects: First, let’s intuitively experience the usage of HttpRunner –> Comparison with other testing frameworks like pytest and unittest
1. Usage of HttpRunner
In HttpRunner,runner.run
is the core method for running tests through the Python API. It allows you to directly call and execute test cases in the code, making it very suitable for integration into automated testing processes. The specific usage is as follows:
1.1. Install HttpRunner
Make sure to install HttpRunner:
pip install httprunner
1.2. Basic Usage
The basic steps to run test cases using runner.run
are as follows:
from httprunner import HttpRunner
# Initialize HttpRunner
runner = HttpRunner()
# Run test cases
result = runner.run("tests/testcase.yml")
# Output test results
print(result)
1.3. Parameter Explanation
The <span>runner.run</span>
method supports the following parameters:
-
path: Path to the test case file (e.g., YAML/JSON file).
-
dot_env_path: Path to the environment variable file (optional).
-
log_level: Log level (e.g., “INFO”, “DEBUG”, optional).
-
failfast: Whether to stop at the first failure (default is
<span>False</span>
). -
save_tests: Whether to save test results (default is
<span>True</span>
).
Example: Calling with Parameters
result = runner.run(
path="tests/testcase.yml",
dot_env_path=".env",
log_level="DEBUG",
failfast=True)
1.4. Test Case File
Test cases for HttpRunner are usually written in YAML or JSON format. For example: testcase.yml
config:
name: "Example Test Case"
base_url: "https://httpbin.org"
teststeps:
- name: "GET Request Example"
request:
method: GET
url: /get
validate:
- eq: [status_code, 200]
1.5. Integration into Automated Testing
You can integrate <span>runner.run</span>
into automated testing frameworks, such as:
-
Calling in CI/CD pipelines
-
Combining with
unittest
orpytest
for automated testing:
<span><span>Combining </span><strong>HttpRunner</strong><span> with </span><strong>pytest</strong><span> or </span><strong>unittest</strong><span> is mainly to compensate for the shortcomings of HttpRunner in certain scenarios while fully utilizing the flexibility and powerful features of pytest or unittest:</span></span>
-
<span>A,</span>
<span>HttpRunner is responsible for API testing</span>
: Using YAML/JSON to write simple API test cases.
Example: Continuous Integration
Create a Pipeline task in Jenkins and define the following in the <span>Jenkinsfile</span>
:
pipeline {
agent any
stages {
stage('Setup') {
steps {
sh 'pip install httprunner'
}
}
stage('Run Tests') {
steps {
sh 'hrun testcases/testcase.yml'
}
}
stage('Archive Report') {
steps {
archiveArtifacts artifacts: 'reports/**/*', fingerprint: true
}
}
}
}
Example: Combining with unittest
import unittest
from httprunner import HttpRunner
class TestHttpRunner(unittest.TestCase):
def test_example(self):
runner = HttpRunner()
result = runner.run("tests/testcase.yml")
self.assertTrue(result["success"], "Test case execution failed")
if __name__ == "__main__":
unittest.main()
Example: Combining with pytest
import pytest
from httprunner import HttpRunner
def test_httprunner():
runner = HttpRunner()
result = runner.run("tests/testcase.yml")
assert result["success"], "Test case execution failed"
1.6. Test Results
runner.run
returns a result that is a dictionary containing the following key information:-
success: Whether the test was successful ( True
/False
). -
stat: Test statistics (e.g., total number of cases, number passed, number failed). -
details: Detailed information for each test step.
Example Result
{
"success": True,
"stat": {
"total": 1,
"success": 1,
"fail": 0
},
"details": [
{
"name": "GET Request Example",
"status": "success",
"response": {
"status_code": 200,
"body": {...}
}
}
]
}
1.7. Advanced Usage
-
Dynamic generation of test cases: You can dynamically generate test cases through code and run them. -
Multi-environment support: Load different environment configurations through <span>dot_env_path</span>
. -
Test reports: Generate test reports in conjunction with <span>pytest-html</span>
or<span>allure</span>
.
Summary
<span>runner.run</span>
is a powerful feature provided by HttpRunner, allowing you to run test cases directly in the code, making it very suitable for integration into automated testing processes. By combining with testing frameworks (such as <span>unittest</span>
or <span>pytest</span>
or self-developed automated testing platforms), more complex testing scenarios and pipelines can be constructed. Further updates will be provided regarding the combination of HttpRunner with testing frameworks.