使用postman+newman+python做介面自動化測試

來源:互聯網
上載者:User

標籤:length   分享圖片   body   status   自動化   npm   測試的   FN   運行命令   

postman是一款API調試工具,可用於測試介面,相類似的工具還有jmeter、soupUI。通過postman+newman+python可以批量運行調試介面,達到自動化測試的效果。

1、PostMan安裝
共有兩種方式,一種是chrome瀏覽器上的外掛程式,一種是postman用戶端。我使用的是postman用戶端。
1)在Chrome瀏覽器怎麼安裝Postman
https://www.cnblogs.com/mafly/p/postman.html
2)安裝Postman用戶端
a、下載軟體https://www.getpostman.com/apps
b、安裝
2、使用
1)發送請求、查看響應
2)環境變數、全域變數
環境變數:只作用於設定的環境
設定環境變數:pm.environment.set("variable_key", "variable_value");
擷取環境變數:pm.environment.get("variable_key");
全域變數:作用於所有環境
設定全域變數:pm.globals.set("variable_key", "variable_value");
擷取全域變數:pm.globals.get("variable_key");
使用例子:
var data=JSON.parse(responseBody);
var act=data.data.accessToken;
postman.setGlobalVariable("accessToken", act);
postman.setGlobalVariable("userId", data.data.userId);
postman.setGlobalVariable("refreshToken", data.data.refreshToken);
var afterUrl="?access_token="+act+"&userId="+data.data.userId;
pm.globals.set("afterUrl", afterUrl);
console.log(afterUrl)
使用變數:
在使用的變數地方用 {{variableName}}代替
具體查看文檔:https://www.getpostman.com/docs/postman/environments_and_globals/variables
3)設定斷言
tests["Your test nickName"] = data.data.nickName === "2589" //響應內容 nickName =2589
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
}); //返回為200

var responseJSON=JSON.parse(responseBody);
tests[‘response matches the data posted‘] = (responseJSON.data && responseJSON.data.length === 10);
//返回data資料共10條
4)調試console
需要在postman用戶端,點擊 view->show postman console 調出
在test或 Pre-request Script中寫指令碼列印出有疑問的值
console.log(variableName); 之後運行請求
5)collection
需要在postman用戶端,點擊collection->Runner ,運行
具體查看文檔:https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run

6)具體使用如

\

7)匯出json檔案

2、newman安裝

官方協助文檔地址:https://www.npmjs.com/package/newman
1)需要安裝nodejs,並配置好環境
2)開啟控制台,運行:npm install -g newman
3)校正是否安裝成功,運行:newman --version
Newman 執行指令碼
  Newman在3版本後做了比較大的改動,但是運行命令越來越簡單如下:

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

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()

4、最終產生報告如下:

 

 


 

使用postman+newman+python做介面自動化測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.