httprunner3源码解读(一):简介+使用介绍
一、简介
1、httprunner是什么?
HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 pytest/YAML/JSON 脚本,即可实现自动化测试、性能测试、线上监控、持续集成等多种测试需求。
2、设计哲学
3、主要特征
- 继承的所有强大功能requests ,只需以人工方式获得乐趣即可处理HTTP(S)。
- 以YAML或JSON格式定义测试用例,pytest 以简洁优雅的方式运行。
- 在HAR 支持下记录并生成测试用例。
- 支持variables/ extract/ validate/hooks机制,以创建非常复杂的测试方案。
- 使用debugtalk.py插件,任何功能都可以在测试用例的任何部分使用。
- 使用jmespath ,提取和验证json响应从未如此简单。
- 有了pytest ,数百个插件随时可用。
- 使用allure ,测试报告可以非常强大。
- 通过重复使用locust ,您可以进行性能测试,而无需进行额外的工作。
- 支持CLI命令,与CI/CD完美结合。
4、框架流程
二、框架使用
1、下载安装
使用pip命令进行安装
pip install httprunner
安装后效验是否安装成功,可以使用如下命令进行效验
httprunner -V 3.1.6
httprunner:主命令,用于所有功能
httprunner run,用于运行YAML / JSON / pytest测试用例
httprunner make,用于将YAML / JSON测试用例转换为pytest文件
httprunner har2case,用于将HAR转换为YAML / JSON测试用例
locusts:用于对locusts进行负载测试
2、源码获取
# https://github.com/httprunner/httprunner git clone https://github.com/httprunner/httprunner.git
3、官方文档: https://docs.httprunner.org/
4、测试用例格式关系
5、创建新项目
PS D:\private\src\hruntest> httprunner startproject -h usage: httprunner startproject [-h] [project_name] positional arguments: project_name Specify new project name. optional arguments: -h, --help show this help message and exit
PS D:\private\src\hruntest> httprunner startproject demo
6、用har2case生成测试用例
使用charles、fiddler抓包工具进行录屏,并导出(export)
导出文件类型选择为.har后缀。
使用har2case命令用法
PS D:\private\src\hruntest> httprunner har2case -h usage: httprunner har2case [-h] [-2y] [-2j] [--filter FILTER] [--exclude EXCLUDE] [har_source_file] positional arguments: har_source_file Specify HAR source file optional arguments: -h, --help show this help message and exit -2y, --to-yml, --to-yaml Convert to YAML format, if not specified, convert to pytest format by default. -2j, --to-json Convert to JSON format, if not specified, convert to pytest format by default. --filter FILTER Specify filter keyword, only url include filter string will be converted. --exclude EXCLUDE Specify exclude keyword, url that includes exclude string will be ignored, multiple keywords can be joined with '|'
7、运行测试用例
httprunner run = make + pytest
PS D:\private\src\hruntest> httprunner run D:\private\src\hruntest\demo\testcases 2021-10-30 11:24:56.584 | INFO | httprunner.make:__make:512 - make path: D:\private\src\hruntest\demo\testcases 2021-10-30 11:24:56.590 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-10-30 11:24:56.595 | INFO | httprunner.loader:load_dot_env_file:127 - Loading environment variables from D:\private\src\hruntest\demo\.env 2021-10-30 11:24:56.595 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME 2021-10-30 11:24:56.596 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD 2021-10-30 11:24:56.597 | INFO | httprunner.make:make_testcase:349 - start to make testcase: D:\private\src\hruntest\demo\testcases\demo_testcase_ref.ym l 2021-10-30 11:24:56.618 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-10-30 11:24:56.620 | INFO | httprunner.make:make_testcase:349 - start to make testcase: D:\private\src\hruntest\demo\testcases\demo_testcase_reques t.yml 2021-10-30 11:24:56.620 | INFO | httprunner.make:make_testcase:442 - generated testcase: D:\private\src\hruntest\demo\testcases\demo_testcase_request_te st.py 2021-10-30 11:24:56.622 | INFO | httprunner.make:make_testcase:442 - generated testcase: D:\private\src\hruntest\demo\testcases\demo_testcase_ref_test.p y 2021-10-30 11:24:56.628 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-10-30 11:24:56.629 | INFO | httprunner.make:make_testcase:349 - start to make testcase: D:\private\src\hruntest\demo\testcases\demo_testcase_reques t.yml 2021-10-30 11:24:56.629 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ... reformatted D:\private\src\hruntest\demo\testcases\demo_testcase_ref_test.py reformatted D:\private\src\hruntest\demo\testcases\demo_testcase_request_test.py All done! ✨ � ✨ 2 files reformatted. 2021-10-30 11:24:57.237 | INFO | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.1.6 =================================================================== test session starts =================================================================== platform win32 -- Python 3.9.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: D:\private\src\hruntest plugins: html-2.1.1, metadata-1.11.0 collected 2 items demo\testcases\demo_testcase_request_test.py . [ 50%] demo\testcases\demo_testcase_ref_test.py . [100%] ==================================================================== 2 passed in 4.68s ==================================================================== PS D:\private\src\hruntest>
8、参数驱动
主要讲解CSV格式,通过parameters关键字,参数列表中间加“-”串行。${P(CSV文件相对路径)}
config: name: "business type info test" variables: username: "admin" password: "2wsx@WSX" parameters: ids-combineNe-realNeType-slNeType: ${parameterize(data/base_ne/base_ne_ids.csv)} # 两种格式:ids-combineNe-realNeType-slNeType: ${P(data/base_ne/base_ne_ids.csv)} base_url: "http://192.xxx.xxx.xxx:8080" verify: False
参数使用情况
- name: /base-ne/ids request: headers: Authorization: $token Content-Type: application/json method: GET params: ids: $ids url: /base-ne/ids validate: - eq: ["status_code", 200] - eq: - body.result[0].combineNe - $combineNe - eq: - body.result[0].realNeType - $realNeType - eq: - body.result[0].slNeType - $slNeType
CSV文件格式如下:
ids,combineNe,realNeType,slNeType 1,eNode/MME,MME,eNode 2,gNode/AMF,AMF,gNode 3,PGW/PBC,PBC,PGW 4,PSBC/I-CSF,I-CSF,PSBC 5,S-CSC/HSS,HSS,S-CSC 6,S-CSC/AS,AS,S-CSC 7,eNode/SGW,SGW,eNode
用例执行时,会运行七次。
9、allure报告
Httprunner提供pytest默认的报告格式
hrun ./testcases/login.yml --html=reports/demo.html
运行用例:
pytest testcases/base_ne/base_ne_ids.pytest --alluredir reports (最终报告可以看到运行日志) hrun testcases/base_ne --alluredir=reports (没有运行日志)
生成报告命令:
allure generate reports allure generate reports -o allure-report --clean -o allure-report:是指定清空测试报告的文件 –clean:是为了清空已有的测试报告
-------- THE END --------