2. 项目结构
2.1. 初代项目结构
3. 用例结构 - pytest
3.1. 单接口用例结构
# NOTE: Generated By HttpRunner v4.3.5
# FROM: testcases\data\data_20240229_test.json
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseData20240229Test(HttpRunner):
config = Config("testcase description")
teststeps = [
Step(
RunRequest("请求标题:最近的充值记录")
.get(
"https://ng-apptest.transspay.net/api/cfront/airtime/recharge/records/latest"
)
.with_headers(
**{
"accept-encoding": "gzip",
"content-type": "application/json",
"if-modified-since": "Wed, 28 Feb 2024 06:30:02 GMT",
"pp_channel": "googleplay",
"pp_client_ver": "5.3.0&602280702",
"pp_device_id": "bee648d4b0a5ab70012345",
"pp_device_type": "ANDROID",
"pp_req_sign": "ZDfAAOuQ4o8I8UVzS80MrwGv7ZHRHwPyZL27ZuWCbkM45wxQ41DMokcYx3P7%2BNZxuIlgjDZqPKlOe0u3lCiOeN6CxNqgxN%2FUCyHrFYCjNqHQt7A%2FQsSt6c9rog0j6aF1rZTK7K5kqTESmpvVlbPOkUh4cmjHVxWlotIkO3%2FO2aQ%3D",
"pp_timestamp": "1709185674115",
"pp_token": "22761d93-cea3-47cf-a8f9-a8087e540409",
"user-agent": "PalmPay/5.3.0&602280702 (Android 13)",
}
)
.validate()
.assert_equal("status_code", 200, "assert response status code")
.assert_equal("body.respCode", "00000000", "assert response body respCode")
.assert_equal("body.respMsg", "success", "assert response body respMsg")
),
]
if __name__ == "__main__":
TestCaseData20240229Test().test_start()
4. 用例转换
4.1. har文件生成
fiddler 或者 Charles 导出生成
4.2. har -> pytest
根据抓包工具的jar包,生成pytest用例
命令:
hrp convert --from-har .\loginV2.har --to-pytest --output-dir .
备注:如果缺失参数:--from-har,会出现转换失败的情况(版本差异调整)
转换效果:
D:\httprunner\demo\testcases> hrp convert --from-har .\loginV2.har --to-pytest --output-dir .
3:21PM INF Set log to color console
3:21PM INFO Set log level
3:21PM INF ensure python3 venv packages=["httprunner==v4.3.5"] python3="C:\\Users\\Administrator\\.hrp\\venv\\Scripts\\python3.exe"
3:21PM INF python package is ready name=httprunner version=v4.3.5
3:21PM INF set python3 executable path Python3Executable="C:\\Users\\Administrator\\.hrp\\venv\\Scripts\\python3.exe"
3:21PM INF convert testcase fromType=har outputType=pytest path=".\\loginV2.har"
3:21PM INF load file path=".\\loginV2.har"
3:21PM INF convert teststep method=POST url=https://ng-apptest.transspay.net/api/c-bff-product/start/loginV2
3:21PM INF dump data to json path="D:\\httprunner\\demo\\testcases\\loginV2_test.json"
3:21PM INF exec command cmd="C:\\Windows\\system32\\cmd.exe"
2024-02-25 15:21:24.591 | INFO | make path: D:\httprunner\demo\testcases\loginV2_test.json
2024-02-25 15:21:24.607 | INFO | ensure compatibility with testcase format v2/v3
2024-02-25 15:21:24.607 | INFO | Loading environment variables from D:\httprunner\demo\.env
2024-02-25 15:21:24.607 | INFO | start to make testcase: D:\httprunner\demo\testcases\loginV2_test.json
2024-02-25 15:21:24.607 | INFO | generated testcase: D:\httprunner\demo\testcases\loginV2_test_test.py
2024-02-25 15:21:24.607 | INFO | format pytest cases with black ...
reformatted D:\httprunner\demo\testcases\loginV2_test_test.py
All done! ✨ 🍰 ✨
1 file reformatted.
3:21PM INF conversion completed outputFile=loginV2_test.py
5. 用例执行
5.1.3. pytest 直接执行
pytest .\testcases\requests_test.py --html=reports/result.html
生成报告:
6. allure报告集成
6.1. 生成allure数据
pytest .\testcases\requests_test.py --alluredir allure_result
6.2. 生成allure报告
allure generate .\allure_result\ -o .\allure_report\
6.3. 查看报告
allure open allure-report
allure serve allure-result