介面自動化測試學習筆記
介面自動化測試
學習資料(一):
使用postman+newman+python做介面自動化測試
通過postman+newman+python可以批量執行除錯介面,達到自動化測試的效果。
1、postman安裝與使用
2、newman安裝與使用
安裝Newman
newman是為Postman而生,專門用來執行Postman編寫好的指令碼; 使用newman,你可以很方便的用命令列來執行postman collections
- cnpm install newman --global #安裝newman
- npm install -g newman-reporter-html #安裝html
驗證安裝成功:
-
newman -v
官方幫助文件地址:
https://www.npmjs.com/package/newman
1)需要安裝nodejs,並配置好環境
2)開啟控制檯,執行:npm install -g newman
3)校驗是否安裝成功,執行:newman --version
Newman 執行指令碼
a)
newman run <collection-file-source> [options]
run 後面跟上要執行的json檔案或者URL(json 和 URL 都由postman匯出生成),再後面跟一些引數,例如環境變數,測試報告,介面請求超時時間等等。最後給兩個完整的例子做參考:
newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html
b)
1.匯出指令碼,選中合集後右鍵點選-Export
2.進入到匯出的指令碼的路徑下執行命令:newman run 執行順序.postman_collection.json -r html
3.執行完之後,開啟路徑可以看到生成了html的結果
整合到jenkins
1.可以檢視之前寫的一篇通過tomcat的方式安裝jenkins:https://www.cnblogs.com/zengxuejie/p/13626723.html
2.進入Jenkins構建一個專案:postman_api
3.進入專案的配置頁面,構建-Execute Windows batch command.然後輸入命令,這個命令就是在cmd裡執行的命令,儲存。
4.儲存後,直接構建專案,執行完成。
3、使用python指令碼執行newman
# coding=utf-8
import time
import os
class postmanApiTest:
#執行postman生成報告
#通過newman
def postman(self):
jSONfname = 'D:/htmlOut' + time.strftime('%Y-%m-%d', time.gmtime())+'.html'
# cmd = 'newman run D:/Buddy_Test_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-html-export '+jSONfname
cmd='newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html'
os.system(cmd)
print('------------------------------------------------------------')
print(jSONfname)
if os.path.isfile(jSONfname):
return jSONfname
print(jSONfname)
else:
return False
if __name__ == '__main__':
a=postmanApiTest()
a.postman()
學習資料(二)
簡單實現介面自動化測試(基於python+unittest)
優化 第二版
介面呼叫異常處理,增加try,except處理,對於返回response.status_code,返回200進行結果比對,不是200資料異常資訊。
#!/usr/bin/env python
#coding: utf-8
'''
unittest merchant backgroud interface
@author: zhang_jin
@version: 1.0
@see:http://www.python-requests.org/en/master/
'''
import json
import traceback
import requests
url = "http://cpright.xinhua-news.cn/api/match/image/getjson"
querystring = {
"category": "image",
"offset": "0",
"limit": "30",
"sourceId": "0",
"metaTitle": "",
"metaId": "0",
"classify": "unclassify",
"startTime": "",
"endTime": "",
"createStart": "",
"createEnd": "",
"sourceType": "",
"isTracking": "true",
"metaGroup": "",
"companyId": "0",
"lastDays": "1",
"author": ""
}
headers = {
'cache-control': "no-cache",
'postman-token': "e97a99b0-424b-b2a5-7602-22cd50223c15"
}
try:
#Post介面呼叫
response = requests.request("POST", url, headers=headers, params=querystring)
#對http返回值進行判斷,對於200做基本校驗
if response.status_code == 200:
results = json.loads(response.text)
if results['total'] == 191:
print "Success"
else:
print "Fail"
print results['total']
else:
#對於http返回非200的code,輸出相應的code
raise Exception("http error info:%s" %response.status_code)
except:
traceback.print_exc()
學習資料(三)
介面自動化測試框架:python+requests+pytest+allure實現(CSDN)
眾所周知,目前市面上大部分的企業實施介面自動化最常用的有兩種方式:
1.基於工具類的介面自動化,如:
Postman+Newman+Jenkins+Git/svn
Jmeter+Ant+Jenkins+Git/svn
2.基於程式碼類的介面自動化,如:
Python+Requests+Pytest+Allure報告定製
待完善......