irpas技术客

【httprunner】httprunner4.x版本的部署与应用_wpc_1992_httprunner版本

网络 8179

前情提要:推荐阅读官方中文文档,路径https://httprunner.com/docs/introduction/ 1、安装下载 linux系统下快速安装命令:bash -c “$(curl -ksSL https://httprunner.com/script/install.sh)” 对于windows用户,最好到官网进行下载。 下载地址:https://github.com/httprunner/httprunner/releases 下载后的tar.gz文件手动改为zip格式并解压。

2、部署配置 a)在C盘根目录下创建tmp/hrp目录(自定义目录),将hrp.exe文件放在目录下。 b) 配置hrp服务的环境变量,我的电脑——属性——高级系统设置——环境变量,在PATH下新增系统变量。

3、快速入门 安装成功后,你将获得一个 hrp 命令行工具,执行 hrp -h 即可查看到参数帮助说明。

$ hrp -h ██╗ ██╗████████╗████████╗██████╗ ██████╗ ██╗ ██╗███╗ ██╗███╗ ██╗███████╗██████╗ ██║ ██║╚══██╔══╝╚══██╔══╝██╔══██╗██╔══██╗██║ ██║████╗ ██║████╗ ██║██╔════╝██╔══██╗ ███████║ ██║ ██║ ██████╔╝██████╔╝██║ ██║██╔██╗ ██║██╔██╗ ██║█████╗ ██████╔╝ ██╔══██║ ██║ ██║ ██╔═══╝ ██╔══██╗██║ ██║██║╚██╗██║██║╚██╗██║██╔══╝ ██╔══██╗ ██║ ██║ ██║ ██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║██║ ╚████║███████╗██║ ██║ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝ HttpRunner is an open source API testing tool that supports HTTP(S)/HTTP2/WebSocket/RPC network protocols, covering API testing, performance testing and digital experience monitoring (DEM) test types. Enjoy! ? 🚀 ? License: Apache-2.0 Website: https://httprunner.com Github: https://github.com/httprunner/httprunner Copyright 2017 debugtalk Usage: hrp [command] Available Commands: boom run load test with boomer completion generate the autocompletion script for the specified shell convert convert JSON/YAML testcases to pytest/gotest scripts har2case convert HAR to json/yaml testcase files help Help about any command pytest run API test with pytest run run API test with go engine startproject create a scaffold project Flags: -h, --help help for hrp --log-json set log to json format -l, --log-level string set log level (default "INFO") -v, --version version for hrp Use "hrp [command] --help" for more information about a command.

脚手架创建项目 HttpRunner 支持使用脚手架创建示例项目。 执行 hrp startproject 命令,即可初始化指定名称的项目工程。

$ hrp startproject demo 10:13PM INF Set log to color console other than JSON format. 10:13PM ??? Set log level 10:13PM INF create new scaffold project force=false pluginType=py projectName=demo 10:13PM INF create folder path=demo 10:13PM INF create folder path=demo/har 10:13PM INF create file path=demo/har/.keep 10:13PM INF create folder path=demo/testcases 10:13PM INF create folder path=demo/reports 10:13PM INF create file path=demo/reports/.keep 10:13PM INF create file path=demo/.gitignore 10:13PM INF create file path=demo/.env 10:13PM INF create file path=demo/testcases/demo_with_funplugin.json 10:13PM INF create file path=demo/testcases/demo_requests.yml 10:13PM INF create file path=demo/testcases/demo_ref_testcase.yml 10:13PM INF start to create hashicorp python plugin 10:13PM INF create file path=demo/debugtalk.py 10:13PM INF ensure python3 venv packages=["funppy==v0.4.3"] python=/Users/debugtalk/.hrp/venv/bin/python 10:13PM INF python package is ready name=funppy version=0.4.3 10:13PM INF create scaffold success projectName=demo

4、初览测试用例 接下来我们以 demo_requests.yml 为例,初步预览下 HttpRunner 的测试用例结构。

config: name: "request methods testcase with functions" variables: foo1: config_bar1 foo2: config_bar2 expect_foo1: config_bar1 expect_foo2: config_bar2 base_url: "https://postman-echo.com" verify: False export: ["foo3"] teststeps: - name: get with params variables: foo1: bar11 foo2: bar21 sum_v: "${sum_two(1, 2)}" request: method: GET url: /get params: foo1: $foo1 foo2: $foo2 sum_v: $sum_v headers: User-Agent: HttpRunner/${get_httprunner_version()} extract: foo3: "body.args.foo2" validate: - eq: ["status_code", 200] - eq: ["body.args.foo1", "bar11"] - eq: ["body.args.sum_v", "3"] - eq: ["body.args.foo2", "bar21"] - name: post raw text variables: foo1: "bar12" foo3: "bar32" request: method: POST url: /post headers: User-Agent: HttpRunner/${get_httprunner_version()} Content-Type: "text/plain" data: "This is expected to be sent back as part of response body: $foo1-$foo2-$foo3." validate: - eq: ["status_code", 200] - eq: ["body.data", "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32."] - name: post form data variables: foo2: bar23 request: method: POST url: /post headers: User-Agent: HttpRunner/${get_httprunner_version()} Content-Type: "application/x-www-form-urlencoded" data: "foo1=$foo1&foo2=$foo2&foo3=$foo3" validate: - eq: ["status_code", 200] - eq: ["body.form.foo1", "$expect_foo1"] - eq: ["body.form.foo2", "bar23"] - eq: ["body.form.foo3", "bar21"]

HttpRunner 测试用例包括且仅有两部分: config:测试用例的公共配置部分,包括用例名称、base_url、参数化数据源、是否开启 SSL 校验等 teststeps:有序步骤的集合;采用了 go interface 的设计理念,支持进行任意协议和测试类型的拓展(甚至包括 UI 自动化) 在上面的案例中,每个 step 都是一个 HTTP 请求;可以看到,描述信息仅包含了 HTTP 请求和结果校验的核心要素,没有任何累赘的内容。 同时,需要重点关注的是,虽然上面的用例是 YAML 文本,但同样支持引用变量和调用函数。 变量引用:约定通过 ${} 或 $ 的形式来引用变量,例如 $foo1 或 ${foo1} 函数调用:约定通过 ${} 的形式来调用插件函数,例如 ${sum_two(1, 2)} 变量的申明定义在 step 或 config 的 variables 中,并且遵循优先级的要求。 函数的申明定义在项目根目录的 debugtalk.py 中,基于「约定大于配置」的设计理念,我们无需在测试用例中进行配置。 import funppy

def get_httprunner_version(): return “v4.0.0-alpha”

def sum_two_int(a: int, b: int) -> int: return a + b

在 debugtalk.py 中,我们可以编写实现任意自定义逻辑的函数,只需通过 funppy 进行 register 和 serve() 即可。

PS:后续版本中自定义函数的注册和使用由后台自动完成,不需要单独维护。

5、运行接口测试 测试用例就绪后,通过 hrp run 命令即可执行指定的测试用例;如需生成 HTML 测试报告,可附带 --gen-html-report 参数(也可以简化为 -g)。

$ hrp run demo/testcases/demo_requests.yml demo/testcases/demo_ref_testcase.yml --gen-html-report

6、总结 httprunner4版本重新引入了2.x版本的默认测试报告,这是我最喜欢的地方。除此之外,新版本还加入了非常多的新功能,希望接下来和大家一起进行学习。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #httprunner版本 #C #curl #ksSL