当当当!好久没更新了,是不是特别想念,哈哈,下面为大家带来一篇超级简单的接口自动化工具文章~
这篇文章出处不属于我,这边只是拿来共同分享下,比较简单,适合初学者,我本人觉得整体的逻辑和JMeter还是很类似的,只是它的脚本维护比JMeter方便很多,也更加轻便。
运行环境
HttpRunner 是一个基于 Python 开发的测试框架,可以运行在 macOS、Linux、Windows 系统平台上。HttpRunner 支持 Python 2.7 和 Python 3.3 以上的所有版本。
安装方式
- 解压提供的安装包(下载链接:https://pan.baidu.com/s/1yPglfEVGrRKS-Td6OyJ7kg)
- 进入目录下运行安装。
- 运行如下命令,若正常显示版本号,则说明 HttpRunner 安装成功。
录制生成用例
为了简化测试用例的编写工作,HttpRunner 实现了测试用例生成的功能,对应的转换工具为一个独立的项目:har2case。
获取 HAR 数据包
在转换生成测试用例之前,需要先将抓取得到的数据包导出为 HAR 格式的文件。在fiddler中的操作方式为,选中需要转换的接口(可多选或全选),点击菜单栏file-Export Sessions-Selected Sessions(选中HTTPArchive v1.1)-next,保存该har格式文件
转换生成测试用例
由于我们公司接口加密,生成的测试用例需要自己稍微修改
运行测试
可以在Report文件中查看测试报告
用例编写语法介绍
- config:
name: BaseServer
parameters:
- type: ["gold", ""]
request:
base_url: https://base.sojex.net
headers:
Content-Type: application/json
- test:
name: GetETFList
request:
url: /BaseServer/FinanceBaseApi/GetETFList
method: GET
params:
type: $type
validate:
- eq: [status_code, 200]
- type_match: [status_code, int]
- eq: [content.status, 1000]
- eq: [content.desc, "OK"]
- contains: [content.data.list,{"as":"29112014.13","d":"823.87000","dat":"2019-01-30","v":"34624068366.41","ud":"8.23"}]
- config:作为整个测试用例的全局配置项,作用域为整个测试用例
- test:测试步骤的变量空间(context)会继承或覆盖 config 中定义的内容
- 若某变量在 config 中定义了,在某 test 中没有定义,则该 test 会继承该变量
- 若某变量在 config 和某 test 中都定义了,则该 test 中使用自己定义的变量值
- name: 测试用例名称
- request
- base_url:测试用例请求url的公共host
- validate:结果断言
| comparator | Description | A(check), B(expect) | examples | | -- | -- | -- | -- | | `eq`, `==` | value is equal | A == B | 9 eq 9 | | `lt` | less than | A < B | 7 lt 8 | | `le` | less than or equals | A <= B | 7 le 8, 8 le 8 | | `gt` | greater than | A > B | 8 gt 7 | | `ge` | greater than or equals | A >= B | 8 ge 7, 8 ge 8 | | `ne` | not equals | A != B | 6 ne 9 | | `str_eq` | string equals | str(A) == str(B) | 123 str_eq '123' | | `len_eq`, `count_eq` | length or count equals | len(A) == B | 'abc' len_eq 3, [1,2] len_eq 2 | | `len_gt`, `count_gt` | length greater than | len(A) > B | 'abc' len_gt 2, [1,2,3] len_gt 2 | | `len_ge`, `count_ge` | length greater than or equals | len(A) >= B | 'abc' len_ge 3, [1,2,3] len_gt 3 | | `len_lt`, `count_lt` | length less than | len(A) < B | 'abc' len_lt 4, [1,2,3] len_lt 4 | | `len_le`, `count_le` | length less than or equals | len(A) <= B | 'abc' len_le 3, [1,2,3] len_le 3 | | `contains` | contains | [1, 2] contains 1 | 'abc' contains 'a', [1,2,3] len_lt 4 | | `contained_by` | contained by | A in B | 'a' contained_by 'abc', 1 contained_by [1,2] | | `type_match` | A is instance of B | isinstance(A, B) | 123 type_match 'int' | | `regex_match` | regex matches | re.match(B, A) | 'abcdef' regex 'a\w+d' | | `startswith` | starts with | A.startswith(B) is True | 'abc' startswith 'ab' | | `endswith` | ends with | A.endswith(B) is True | 'abc' endswith 'bc' |
- 参数化
- 直接指定参数列表
- 引用csv数据文件
- 文件需放置在与测试用例文件相同的目录中;
- CSV 文件中的第一行必须为参数名称,从第二行开始为参数值,每个(组)值占一行;
- 若同一个csv文件中具有多个参数,则参数名称和数值的间隔符需实用英文逗号。
```
- config:
name: BaseServer
parameters:
- type: ${parameterize(ETF.csv)}
- type: ${P(ETF.csv)} # 简写方式
request:
base_url: https://base.sojex.net
headers:
Content-Type: application/json
- test:
name: GetETFList
request:
url: /BaseServer/FinanceBaseApi/GetETFList
method: GET
params:
type: $type
validate:
- eq: [status_code, 200]
- type_match: [status_code, int]
- eq: [content.status, 1000]
- eq: [content.desc, "OK"]
```
- 自定义函数生成参数列表
对于没有现成参数列表,或者需要更灵活的方式动态生成参数的情况,可以通过在debugtalk.py中自定义函数生成参数列表,并在用例引用自定义函数的方式。
- config:
parameters:
- username-password: ${get_account(10)}
- 参数关联
- config:
name: testcase description
variables: {}
- test:
name: /api/get-token
request:
headers:
Content-Type: application/json
json:
sign:9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
method: POST
url: http://127.0.0.1:5000/api/get-token
extract:
token: content.token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- test:
name: /api/users/1000
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: $token
json:
name: user1
password: '123456'
method: POST
url: http://127.0.0.1:5000/api/users/1000
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]