HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,便可实现自动化测试。html
具备如下优势:python
httprunner目前有2个版本,1.x和2x版本,本篇以1.5.8版本为学习的基础版本django
使用pip安装json
pip install httprunner==1.5.8api
安装完成后检查版本号app
hrun -V框架
登陆以后获取token这是最多见的场景了,接下来以独立接口为案例,登陆接口这个是访问我本地的接口,大家是无法访问的, 具体的登陆接口开发须要用到django,查看这篇https://www.cnblogs.com/yoyoketang/p/11517213.html 登陆接口相关文档信息以下:分布式
http://127.0.0.1:8000/api/v1/login/
使用httpapi命令行工具,访问后测试接口报文信息以下函数
C:\Users\dell>http http://127.0.0.1:8000/api/v1/login/ username=test password=123456 -v POST /api/v1/login/ HTTP/1.1 Accept: application/json, */* Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 42 Content-Type: application/json Host: 127.0.0.1:8000 User-Agent: HTTPie/1.0.3 { "password": "123456", "username": "test" } HTTP/1.1 200 OK Allow: POST, OPTIONS Content-Length: 109 Content-Type: application/json Date: Thu, 19 Sep 2019 15:15:18 GMT Server: WSGIServer/0.2 CPython/3.6.0 Vary: Accept, Cookie X-Frame-Options: SAMEORIGIN { "code": 0, "msg": "login success!", "token": "000038efc7edc7438d781b0775eeaa009cb64865", "username": "test" }
接下来转换成httprunner的YAML格式脚本用例,保存为test_login.yml工具
# 上海悠悠,QQ交流群:750815713 - config: name: logincase variables: {} - test: name: login case1 request: url: http://127.0.0.1:8000/api/v1/login/ method: POST headers: Content-Type: application/json User-Agent: python-requests/2.18.4 json: username: test password: 123456 validate: - eq: [status_code, 200] - eq: [headers.Content-Type, application/json] - eq: [content.msg, login success!] - eq: [content.code, 0]
若是你不喜欢yaml格式,用json也是能够的。新建一个test_login2.json文件,内容以下
# 上海悠悠,QQ交流群:750815713 [{ "config": { "name": "logincase", "variables": {} } }, { "test": { "name": "login case1", "request": { "url": "http://127.0.0.1:8000/api/v1/login/", "method": "POST", "headers": { "Content-Type": "application/json", "User-Agent": "python-requests/2.18.4" }, "json": { "username": "test", "password": "123456" } }, "validate": [{ "eq": ["status_code", 200] }, { "eq": ["headers.Content-Type", "application/json"] }, { "eq": ["content.msg", "login success!"] }, { "eq": ["content.code", 0] } ] } } ]
运行用例很简单,直接在cmd里面,cd到test_login.yml目录,运行
hrun test_login.yml
或者执行json文件
hrun test_login2.json
执行结果以下
D:\soft\untitled>hrun test_login.yaml login case1 INFO POST http://127.0.0.1:8000/api/v1/login/ INFO status_code: 200, response_time(ms): 414.33 ms, response_length: 109 bytes INFO start to validate. . ---------------------------------------------------------------------- Ran 1 test in 0.419s OK INFO Start to render Html report ... INFO Generated Html report: D:\soft\untitled\reports\1568906898.html D:\soft\untitled>
运行完成后会在当前目录生成一个report文件夹,里面会有一个html格式的报告文件,按时间戳生成的
点开log能够查看详情
请求(request)
返回 (response)
断言 (Validators)