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)