httprunner 3.x最大的改变是执行用例用的是 python 的 pytest 框架,支持3种格式的用例:YAML/JSON/pytest 代码 对比 httprunner 2.x 以前版本,早期版本用的是 unittest 框架,只支持2种格式的用例: YAML/JSON
HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试、性能测试、线上监控、持续集成等多种测试需求。 主要特征
使用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 -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 | 使用模板结构创建新项目 |
har2case | HAR(HTTP Archive) 转成 YAML/JSON |
make | YAML/JSON 测试用例转成 pytest用例 |
最大的变化是新增了make参数,把 YAML/JSON 测试用例转成 pytest用例
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 是把 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 命令实际上有 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 是把抓包抓到的 .har 文件转成 YAML/JSON 格式用例,参考前面的博客https://www.cnblogs.com/yoyoketang/p/11564028.html 跟之前老版本功能一样,不再重复讲
hrun
是 httprunner run
的别名,所以
httprunner run testcases
等价于
hrun testcases
hmake 是 httprunner make
的别名,所以
httprunner make testcases
等价于
hmake testcases
利用locust 运行性能测试, 参考前面locust相关教程https://www.cnblogs.com/yoyoketang/p/11644998.html
如果想更好的掌握 httprunner 3.x 框架,最好有以下知识储备