打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
httprunner 3.x学习1 - 环境安装与准备

前言

httprunner 3.x最大的改变是执行用例用的是 python 的 pytest 框架,支持3种格式的用例:YAML/JSON/pytest 代码
对比 httprunner 2.x 以前版本,早期版本用的是 unittest 框架,只支持2种格式的用例: YAML/JSON

简介

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完美结合。

环境安装

使用pip 安装

pip3 install httprunner

如果之前装过低版本的,可以加 -U 参数升级到最新版

pip3 install httprunner -U

也可以支持 github 源码安装

pip3 install git+https://github.com/httprunner/httprunner.git@master

查看版本号 v3.1.4

C:\Users\dell>pip show httprunner
Name: httprunner
Version: 3.1.4
Summary: One-stop solution for HTTP(S) testing.
Home-page: https://github.com/httprunner/httprunner
Author: debugtalk
Author-email: debugtalk@gmail.com
License: Apache-2.0
Location: e:\python36\lib\site-packages
Requires: loguru, pytest-html, pydantic, pytest, requests, black, jinja2, sentry-sdk, jmespath, pyyaml
Required-by:

相关命令

安装 HttpRunner 后,以下 5 个命令会写入系统环境变量配置。

  • httprunner:

    主命令,用于所有功能。

  • hrun:

    指令 httprunner run 的别名,用于运行 YAML/JSON/Pytest 测试用例。

  • hmake: 指令 httprunner make 的别名,将 YAML/JSON 用例转换成 pytest 用例。

  • har2case:

    指令 httprunner har2case 的别名,将 HAR 文件转换成 YAML/JSON 用例。

  • locust: 利用 locust 运行性能测试。

httprunner -h 查看相关命令

C:\Users\dell>httprunner -h
usage: httprunner [-h] [-V] {run,startproject,har2case,make} ...

One-stop solution for HTTP(S) testing.

positional arguments:
{run,startproject,har2case,make}
sub-command help
run Make HttpRunner testcases and run with pytest.
startproject Create a new project with template structure.
har2case Convert HAR(HTTP Archive) to YAML/JSON testcases for
HttpRunner.
make Convert YAML/JSON testcases to pytest cases.

optional arguments:
-h, --help show this help message and exit
-V, --version show version

主要有四个参数 run,startproject,har2case,make

参数名功能
-V   —version查看版本号
-h, —help查看帮助
run用于运行YAML/JSON/Pytest 测试用例
startproject使用模板结构创建新项目
har2caseHAR(HTTP Archive) 转成 YAML/JSON
makeYAML/JSON 测试用例转成 pytest用例

最大的变化是新增了make参数,把 YAML/JSON 测试用例转成 pytest用例

startproject 创建项目

startproject 参数可以快速创建一个 demo 项目,方便了解整个项目结构

D:\demo\hrun3x>httprunner startproject yoyo_demo
2021-06-10 22:29:30.482 | INFO | httprunner.scaffold:create_scaffold:43 - Create new project: yoyo_demo
Project Root Dir: D:\demo\hrun3x\yoyo_demo

created folder: yoyo_demo
created folder: yoyo_demo\har
created folder: yoyo_demo\testcases
created folder: yoyo_demo\reports
created file: yoyo_demo\testcases\demo_testcase_request.yml
created file: yoyo_demo\testcases\demo_testcase_ref.yml
created file: yoyo_demo\debugtalk.py
created file: yoyo_demo\.env
created file: yoyo_demo\.gitignore

$ tree yoyo_demo -a
2021-06-10 22:29:30.504 | 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

需注意的是这里用的是httprunner 命令(并不是hrun, 运行完成后可以看到整个项目结构如下

make 转 pytest 用例

make 是把 YAML/JSON 测试用例转成 pytest用例, 需指定 yaml 文件路径或文件夹路径

D:\demo\hrun3x\yoyo_demo>httprunner make testcases
2021-06-10 22:40:08.048 | INFO | httprunner.make:__make:512 - make path: D:\demo\hrun3x\yoyo_demo\testcases
2021-06-10 22:40:08.053 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-06-10 22:40:08.057 | INFO | httprunner.loader:load_dot_env_file:127 - Loading environment variables from D:\demo\hrun3x\yoyo_demo\.env
2021-06-10 22:40:08.060 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME
2021-06-10 22:40:08.062 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD
2021-06-10 22:40:08.064 | INFO | httprunner.make:make_testcase:349 - start to make testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_ref.yml
2021-06-10 22:40:08.075 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-06-10 22:40:08.077 | INFO | httprunner.make:make_testcase:349 - start to make testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request.yml
2021-06-10 22:40:08.080 | INFO | httprunner.make:make_testcase:442 - generated testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request_test.py
2021-06-10 22:40:08.082 | INFO | httprunner.make:make_testcase:442 - generated testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_ref_test.py
2021-06-10 22:40:08.094 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-06-10 22:40:08.097 | INFO | httprunner.make:make_testcase:349 - start to make testcase: D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request.yml
2021-06-10 22:40:08.098 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
reformatted D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_ref_test.py
reformatted D:\demo\hrun3x\yoyo_demo\testcases\demo_testcase_request_test.py
All done! ✨✨ ���� ✨✨
2 files reformatted.

执行后会生成对应的 pytest 用例

testcases\demo_testcase_request_test.py 完整代码示例如下

# NOTE: Generated By HttpRunner v3.1.4
# FROM: testcases\demo_testcase_request.yml

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

class TestCaseDemoTestcaseRequest(HttpRunner):

config = (
Config("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 = [
Step(
RunRequest("get with params")
.with_variables(
**{"foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two(1, 2)}"}
)
.get("/get")
.with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"})
.with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
.extract()
.with_jmespath("body.args.foo2", "foo3")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.args.foo1", "bar11")
.assert_equal("body.args.sum_v", "3")
.assert_equal("body.args.foo2", "bar21")
),
Step(
RunRequest("post raw text")
.with_variables(**{"foo1": "bar12", "foo3": "bar32"})
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/${get_httprunner_version()}",
"Content-Type": "text/plain",
}
)
.with_data(
"This is expected to be sent back as part of response body: $foo1-$foo2-$foo3."
)
.validate()
.assert_equal("status_code", 200)
.assert_equal(
"body.data",
"This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32.",
)
),
Step(
RunRequest("post form data")
.with_variables(**{"foo2": "bar23"})
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/${get_httprunner_version()}",
"Content-Type": "application/x-www-form-urlencoded",
}
)
.with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.form.foo1", "$expect_foo1")
.assert_equal("body.form.foo2", "bar23")
.assert_equal("body.form.foo3", "bar21")
),
]

if __name__ == "__main__":
TestCaseDemoTestcaseRequest().test_start()

run 执行用例

run 命令实际上有 2 个动作,一个是把 YAML/JSON 测试用例转成 pytest 用例,同上一步 make 的功能一样。
第二个动作是用 pytest 执行测试用例,使用命令如下

httprunner run testcases

运行结果可以看出是用的 pytest 运行的测试用例

collected 2 items

testcases\demo_testcase_request_test.py . [ 50%]
testcases\demo_testcase_ref_test.py .

执行的过程中会先把 yaml 文件转成对应的 py 文件,在 testcases 目录可以看到

har2case

har2case 是把抓包抓到的 .har 文件转成 YAML/JSON 格式用例,参考前面的博客https://www.cnblogs.com/yoyoketang/p/11564028.html
跟之前老版本功能一样,不再重复讲

hrun

hrun 是  httprunner run 的别名,所以

httprunner run testcases

等价于

hrun testcases

hmake

hmake 是 httprunner make 的别名,所以

httprunner make testcases

等价于

hmake testcases

locust

利用locust 运行性能测试, 参考前面locust相关教程https://www.cnblogs.com/yoyoketang/p/11644998.html

知识储备

如果想更好的掌握 httprunner 3.x 框架,最好有以下知识储备

  • python3 基础语法,毕竟httprunner 3.x 主要新增pytest用例,鼓励大家写代码了

  • requests 库,发 HTTP(HTTPS) 请求基本库

  • YAML/JSON 文件语法

  • jmespath 提取返回结果,类似于 jsonpath 库,可以先了解下

  • pytest 框架,执行用例最终用的是 pytest 框架,熟练掌握 pytest 框架是很有必要的

  • allure 测试报告用的 allure 框架

  • httprunner 2.x 语法,之前学的老版本 1.x 和 2.x 的语法并不过时,有这个基础学起来更容易掌握

  • locust 如果想做性能测试,可以掌握这个框架

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存
猜你喜欢
类似文章
接口自动化框架对比 | 质量工程
Python 如何使用 HttpRunner 做接口自动化测试
【HttpRunner v3.x】笔记
tep集成HttpRunner与Flask实现开箱即用
pytest之通过yaml实现数据驱动
Python Requests Pytest YAML Allure实现接口自动化
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服