系列文章目录
提示:阅读本章之前,请先阅读目录
前言
httprunner是一个面向https协议的通用接口自动化测试框架,在你进行接口自动化测试工作的时候,你只需要维护好一份标准符合httprunner格式的json,yaml文件,那么就可以实现接口自动化的测试,无需编写脚本代码。
httprunner,不单单只是可以实现接口的测试,还可以实现性能测试,线上监控,持续集成,
一、httprunner架构图
二、设计理念
- 充分利用开源项目,不追求重复造轮子,而是将强大的轮子组装成战车
- 遵循的是约定大于配置
- 配置文件yaml,json组织测试用例
三、安装
使用pip安装即可
pip install httprunner
以及,我们还要安装把har转换成yaml,json的插件
pip install har2case
四、核心五个命令
- httprunner,主命令,用于运行yaml,json,pytest的测试用例
- hrun,是httprunner的别名,效果是一致的
- hmake,主要是把yaml和json转换成pytest的测试用例
- har2case,主要是把har文件转换成yaml,json,pytest的测试用例
- locust,进行性能测试
五、快速上手
- 通过 Fiddler 抓包,并导出har文件
第一步,选中抓到的包
第二步
file>Export Sessions>Selected Sessions
第三步,
选择,HTTPArchive v1.2
导出之后,把文件拷贝到项目中,这里命名为login_request.har
- 通过执行命令生成测试用例
har2case login_request.har # 转为pytest测试用例的文件
har2case login_request.har -2y # 转为yaml格式的测试用例
har2case login_request.har -2j # 转为json格式的测试用例
运行结果
C:\test> har2case -2y login_request.har
INFO:root:Start to generate testcase.
INFO:root:dump testcase to YAML format.
INFO:root:Generate YAML testcase successfully: login_request.yml
生成的标准yml测试用例格式
config:
name: testcase description
variables: {}
teststeps:
- name: /api/Login/login
request:
headers:
Content-Type: application/json;charset=UTF-8
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/103.0.0.0 Safari/537.36
sec-ch-ua: '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"'
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: '"Windows"'
json:
password: '12221122'
phone: '15819583458'
method: POST
url: https://www.16huixuan.com/api/Login/login
validate:
- eq:
- status_code
- 200
- eq:
- headers.Content-Type
- application/json; charset=utf-8
- eq:
- content.code
- 0
- eq:
- content.msg
- 密码必须是字母数字符号两种形式以上的组合
- eq:
- content.count
- ''
- 执行hrun,执行测试用例
hrun login_request.yml
如果是运行整个文件夹的测试用例,直接写路径即可
hrun ../test
运行结果
2022-07-17 12:04:06.106 | INFO | make path: C:\login_request.yml
2022-07-17 12:04:06.117 | INFO | ensure compatibility with testcase format v2/v3
2022-07-17 12:04:06.125 | INFO | start to make testcase: C:\login_request.yml
2022-07-17 12:04:06.132 | INFO | generated testcase: C:\login_request.py
2022-07-17 12:04:06.134 | INFO | format pytest cases with black ...
reformatted C:\login_request.py
All done! ✨ 🍰 ✨
1 file reformatted.
2022-07-17 12:04:07.209 | INFO | start to run tests with pytest. HttpRunner version: v4.1.6
================================================================================= test session starts =================================================================================
platform win32 -- Python 3.9.5, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\test
plugins: html-3.1.1, metadata-2.0.2
collected 1 item
login_request_test.py . [100%]
================================================================================== 1 passed in 0.94s ==================================================================================
2022-07-17 12:04:08.2652022-07-17 12:04:08.2772022-07-17 12:04:08.5552022-07-17 12:04:08.5592022-07-17 12:04:08.5622022-07-17 12:04:08.5642022-07-17 12:04:08.5672022-07-17 12:04:08.569
2022-07-17 12:04:08.5702022-07-17 12:04:08.572
六、断言
在yaml文件的格式
写在关键字,validate
eq 相等
validate:
- eq:
- status_code
- 200
contains 包含
validate:
- contains :
- status_code
- 200
以及一些其他的,在上方的图片
更新日志
提示:将会持续优化更新
20220716,httprunner架构图,设计理念,安装,核心五个命令,快速上手,断言