httprunenr 3.x(一)环境安装与准备

简介

httprunner是一款面向HTTP(s)协议的通用测试框架,只需要维护一份YAML/JSON脚本,即可实现自动化测试,性能测试,线上监控,持续集成等多种测试需求。

 1、集成requests所有功能。

 2、以YAML或JSON格式定义测试用例,pytest方式运行

 3、再HAR支持下记录并生成测试用例

 4、支持variables/extract/validate/hooks机制,创建负责的测试方案

 5、使用debugtalk.py插件,任何功能都可以在测试用例的任何部门使用

 6、使用jmespath,提取和验证json响应

 7、使用pytest数百插件

 8、使用allure,生成完美测试报告

 9、通过重复使用locust,可以进行性能测试

 10、支持CLI命令,与CI/CD完美结合

环境安装

使用pip安装

pip3 intsall httprunner

升级版本命令

pip3 install httprunner -U

查看版本号

pip show httprunner

相关命令

 1、httprunner :主命令,用于所有功能。

 2、hrun : 指令 httprunner run的别名,用于运行 YAML/JSON/pytest 测试用例。

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

 4、har2case : 指令 httprunenr har2case的别名 , 将HAR文件转换成yaml/JSON 用例。

 5、locust : 利用locust运行性能测试

httprunner -h 查看相关命令
C:\Users\pc>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

C:\Users\pc>

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

参数名 功能
-V, --version 查看版本号
-h,--help 查看帮助
run 运行yaml/json/project用例
startproject 使用模板结构创建项目
har2case HAR转换成yaml/json
make yanl/josn转换成pytest

 

 

 

 

 

 

 

 

 

 

 

startproject创建项目

D:\>httprunner startproject KLL_demo
2021-12-09 14:23:57.138 | INFO     | httprunner.scaffold:create_scaffold:43 - Create new project: KLL_demo
Project Root Dir: D:\KLL_demo

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

$ tree KLL_demo -a
2021-12-09 14:23:57.151 | 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

D:\>

make转换pytest用例

D:\KLL_demo>httprunner make testcases
2021-12-09 14:28:06.501 | INFO     | httprunner.make:__make:512 - make path: D:\KLL_demo\testcases
2021-12-09 14:28:06.505 | INFO     | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-12-09 14:28:06.507 | INFO     | httprunner.loader:load_dot_env_file:127 - Loading environment variables from D:\KLL_demo\.env
2021-12-09 14:28:06.507 | DEBUG    | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME
2021-12-09 14:28:06.507 | DEBUG    | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD
2021-12-09 14:28:06.511 | INFO     | httprunner.make:make_testcase:349 - start to make testcase: D:\KLL_demo\testcases\demo_testcase_ref.yml
2021-12-09 14:28:06.519 | INFO     | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-12-09 14:28:06.520 | INFO     | httprunner.make:make_testcase:349 - start to make testcase: D:\KLL_demo\testcases\demo_testcase_request.yml
2021-12-09 14:28:06.526 | INFO     | httprunner.make:make_testcase:442 - generated testcase: D:\KLL_demo\testcases\demo_testcase_request_test.py
2021-12-09 14:28:06.528 | INFO     | httprunner.make:make_testcase:442 - generated testcase: D:\KLL_demo\testcases\demo_testcase_ref_test.py
2021-12-09 14:28:06.537 | INFO     | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-12-09 14:28:06.538 | INFO     | httprunner.make:make_testcase:349 - start to make testcase: D:\KLL_demo\testcases\demo_testcase_request.yml
2021-12-09 14:28:06.539 | INFO     | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
reformatted D:\KLL_demo\testcases\demo_testcase_ref_test.py
reformatted D:\KLL_demo\testcases\demo_testcase_request_test.py
All done! ✨ 🍰 ✨
2 files reformatted.

D:\KLL_demo>

run执行用例

run命令实际有2个命令。一个是吧yaml/josn测试用例转换成pytest用例,同make功能一样

第二个是用pytest执行测试用例

httprunner run testcases

 

posted @ 2021-12-09 14:34  测试-继亮  阅读(61)  评论(0编辑  收藏  举报