httprunner3.x--框架使用分享 - Go语言中文社区

httprunner3.x--框架使用分享


httprunner3.x框架使用分享

Httprunner说明文档路径:

https://docs.httprunner.org/

HttpRunner v3.x支持三种测试用例格式pytest,YAML和JSON。强烈建议以pytest格式而不是以前的YAML/JSON格式编写和维护测试用例

格式关系如下图所示:
在这里插入图片描述

1.安装httprunner最新版本

httprunner3,要求python>=3.6,<4.0

pip3 install httprunner

# 已安装,升级到最新版本

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测试用例

locusts:用于对locusts进行负载测试

2.创建新项目

httprunner startproject -h

D:workwy_onlyhttprunnerDemo>httprunner startproject -h
usage: httprunner startproject [-h] [project_name]

positional arguments:
  project_name  Specify new project name.

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


创建项目只需要一个参数(项目名称)

如: httprunner startproject httprunnerDemo

3. 用har2case生成测试用例

使用charles、fiddler抓包工具进行录屏,并导出(export)

在这里插入图片描述

文件类型选择为.har后缀。

在这里插入图片描述

使用har2case命令用法

D:workwy_onlyhttprunnerDemo>har2case -h
usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER]
                         [--exclude EXCLUDE]
                         [har_source_file]

positional arguments:
  har_source_file       Specify HAR source file

optional arguments:
  -h, --help            show this help message and exit
  -2y, --to-yml, --to-yaml
                        Convert to YAML format, if not specified, convert to
                        pytest format by default.
  -2j, --to-json        Convert to JSON format, if not specified, convert to
                        pytest format by default.
  --filter FILTER       Specify filter keyword, only url include filter string
                        will be converted.
  --exclude EXCLUDE     Specify exclude keyword, url that includes exclude
                        string will be ignored, multiple keywords can be
                        joined with '|'

使用har2case命令将har文件转化为json/yml/pytest文件

har2case ./har/dial.har -2y —生成dial.yml

har2case ./har/dial.har -2j —生成dial.json

har2case ./har/dial.har —生成dial_test.py
在这里插入图片描述
由于HttpRunner 3.0.7以后的版本,har2case默认情况下会将HAR文件转换为pytest

运行测试用例

注意,hrun是的命令别名httprunner run,它们具有相同的作用。

hrun = httprunner run

hrun = make + pytest

运行命令等价:

$ hrun ./testcases/login_test.py

$ pytest ./testcases/login_test.py

$ hrun ./testcases/login.yml

在这里插入图片描述

参数驱动

主要讲解CSV格式,通过parameters关键字,参数列表中间加“-”串行。${P(CSV文件相对路径)}

config:
    name: "business type info test"
    variables:
        username: "admin"
        password: "2wsx@WSX"
    parameters:
        ids-combineNe-realNeType-slNeType:  ${parameterize(data/base_ne/base_ne_ids.csv)}
        # 两种格式:ids-combineNe-realNeType-slNeType:  ${P(data/base_ne/base_ne_ids.csv)}
    base_url: "http://192.xxx.xxx.xxx:8080"
    verify: False

参数使用情况

-   name: /base-ne/ids

    request:
        headers:
            Authorization: $token
            Content-Type: application/json
        method: GET
        params:
            ids: $ids
        url: /base-ne/ids
    validate:
      - eq: ["status_code", 200]
      - eq:
          - body.result[0].combineNe
          - $combineNe
      - eq:
          - body.result[0].realNeType
          - $realNeType
      - eq:
          - body.result[0].slNeType
          - $slNeType

CSV文件格式如下:

ids,combineNe,realNeType,slNeType
1,eNode/MME,MME,eNode
2,gNode/AMF,AMF,gNode
3,PGW/PBC,PBC,PGW
4,PSBC/I-CSF,I-CSF,PSBC
5,S-CSC/HSS,HSS,S-CSC
6,S-CSC/AS,AS,S-CSC
7,eNode/SGW,SGW,eNode

用例执行时,会运行七次。

allure报告

Httprunner提供pytest默认的报告格式

hrun ./testcases/login.yml --html=reports/demo.html

运行用例:

pytest testcases/base_ne/base_ne_ids.pytest --alluredir reports (最终报告可以看到运行日志)

hrun testcases/base_ne --alluredir=reports (没有运行日志)

生成报告命令:

allure generate reports

allure generate reports -o allure-report --clean

-o allure-report:是指定清空测试报告的文件

–clean:是为了清空已有的测试报告

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wangying202/article/details/109053268
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢