文章目录
- 前言
- 主要特征
- 安装
- 安装检查
- 查看httprunner版本
- 创建项目
- 创建新项目
- 运行脚手架项目
前言
HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试、性能测试、线上监控、持续集成等多种测试需求。
中文文档:点击跳转
主要特征
- 继承的所有强大功能requests ,只需以人工方式获得乐趣即可处理HTTP(S)。
- 以YAML或JSON格式定义测试用例,pytest 以简洁优雅的方式运行。
- 在HAR 支持下记录并生成测试用例。
- 支持variables/ extract/ validate/hooks机制,以创建非常复杂的测试方案。
- 使用debugtalk.py插件,任何功能都可以在测试用例的任何部分使用。
- 使用jmespath ,提取和验证json响应从未如此简单。
- 有了pytest ,数百个插件随时可用。
- 使用allure ,测试报告可以非常强大。
- 通过重复使用locust ,您可以进行性能测试,而无需进行额外的工作。
- 支持CLI命令,与CI/CD完美结合。
安装
HttpRunner使用python开发,支持Python 3.6+和大多数操作系统。 在Python 3.6/3.7/3.8上 和 MacOS/Linux/Windows上,基于GitHub-Actions 上进行了测试。
- HttpRunner存储于PyPI, 支持通过pip安装。
pip3 install httprunner
- 如果你想保持最新版本,可用github资源地址安装
pip3 install git+https://github.com/httprunner/httprunner.git@master
- 如果你之前已安装过HttpRunner,想更新到最新版本,可以使用-U选项。
pip3 install -U httprunner
pip3 install -U git+https://github.com/httprunner/httprunner.git@master
安装检查
安装HttpRunner后,以下5个命令会写入系统环境变量配置。
- httprunner:主命令,用于所有功能。
- hrun:指令httprunner run的别名,用于运行YAML/JSON/Pytest 测试用例。
- hmake: 指令httprunner make的别名,将YAML/JSON用例转换成pytest用例。
- har2case:指令httprunner har2case的别名,将HAR文件转换成 YAML/JSON 用例。
- locust: 利用locust 运行性能测试。
查看httprunner版本
$ httprunner -V # hrun -V
3.1.0
创建项目
如果你想创建一个测试项目,可以使用脚手架快速创建。
帮助
$ httprunner startproject -h
用法: httprunner startproject [-h] [project_name]
positional arguments:
project_name 指定新的项目名称
optional arguments:
-h, --help 展示帮助信息并退出
创建新项目
(ceshi) D:\python\httprun>httprunner startproject demo1
2023-03-07 10:31:47.867 | INFO | httprunner.scaffold:create_scaffold:43 - Create new project: demo1
Project Root Dir: D:\python\httprun\demo1
created folder: demo1
created folder: demo1\har
created folder: demo1\testcases
created folder: demo1\reports
created file: demo1\testcases\demo_testcase_request.yml
created file: demo1\testcases\demo_testcase_ref.yml
created file: demo1\debugtalk.py
created file: demo1\.env
created file: demo1\.gitignore
$ tree demo1 -a
2023-03-07 10:31:47.878 | WARNING | httprunner.scaffold:show_tree:29 - tree command not exists, ignore.
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-Break to quit
运行脚手架项目
脚手架项目有几个有效的测试用例,所以可以无需编辑直接运行。