- postman是一個跨平台的接口測試工具,下載鏈接在這里:https://www.getpostman.com/
- unittest是一個單元測試框架,python中安裝:pip install unittest
- requests是一個發送http請求的庫,安裝:pip install requests
官方文檔:http://docs.python-requests.org/en/master/user/quickstart/,
中文文檔:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
以測試http://www.kuaidi100.com/query 為例:
有一個匯通快遞的單號:350757819118
請求的url為http://www.kuaidi100.com/query?type=huitongkuaidi&postid=350757819118
用postman調試接口:

點擊右上角的code,如下圖:

以python--requests的格式復制:

復制得到的代碼如下:
import requests url = "http://www.kuaidi100.com/query" querystring = {"type":"huitongkuaidi","postid":"350757819118"} headers = { 'cache-control': "no-cache", 'postman-token': "9794775a-f3eb-0322-1365-01b775fa9925" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
新建py文件,命名為kuaidi.py,對復制的代碼稍作調整:
import requests import unittest class KuaiDi(unittest.TestCase): def test_huitong_api(self): url = "http://www.kuaidi100.com/query" querystring = {"type":"huitongkuaidi","postid":"350757819118"} headers = { 'cache-control': "no-cache", 'postman-token': "9794775a-f3eb-0322-1365-01b775fa9925" } response = requests.request("GET", url, headers=headers, params=querystring).json() #print(response) self.assertEqual(response['status'],'200') #斷言 self.assertEqual(response['message'],'ok') #斷言 if __name__ == '__main__': unittest.main()
運行結果:

舉例測試:
import requests import json import unittest import time from HTMLTestRunner import HTMLTestRunner class MyTest(unittest.TestCase): def setUp(self): print("[+]start") def tearDown(self): print("[+]end") def zhongtong(self,type = "zhongtong", id = "719857434111"): self.url = "http://www.kuaidi100.com/query" self.params = { "type":type, "postid":id } self.headers={'user-agent': 'my-app/0.0.1'} r = requests.get(url = self.url, params = self.params , headers = self.headers) print(r.status_code) return r class ExpressInquiry(MyTest): def test001_type_valid(self): print("00001") zhongtong = self.zhongtong(type = "shentong") self.assertIn("快遞公司參數異常",zhongtong.text) def test002_type_invalid(self): print("00002") zhongtong = self.zhongtong(type = "sssssssssssss") self.assertIn("參數錯誤",zhongtong.text) def test003_id_valid(self): print("00003") id = self.zhongtong(id = "719857434155") self.assertIn("交通工程學院菜鳥驛站",id.text) def test004_id_invalid(self): print("00004") id = self.zhongtong(id = "123") self.assertIn("參數錯誤",id.text) def test005_type_id_invalid(self): print("00005") type_and_id = self.zhongtong(type = "dads",id = "7777") #print(type_and_id.url) #print(type_and_id.text) self.assertIn("參數錯誤",type_and_id.text) def test006_type_id_null(self): print("00006") null = self.zhongtong(type = "", id = "") #print(null.url) #print(null.text) self.assertIn("參數錯誤",null.text) def suite(): now = time.strftime("%Y-%m-%d %H_%M_%S") filename = './' + now + 'test_result.html' fp = open(filename,'wb') runner = HTMLTestRunner(stream = fp, title = "快遞查詢接口測試報告", description = "測試用例執行情況:") suite = unittest.TestSuite() suite.addTest(ExpressInquiry("test001_type_valid")) suite.addTest(ExpressInquiry("test002_type_invalid")) suite.addTest(ExpressInquiry("test003_id_valid")) suite.addTest(ExpressInquiry("test004_id_invalid")) suite.addTest(ExpressInquiry("test005_type_id_invalid")) suite.addTest(ExpressInquiry("test006_type_id_null")) #unittest.TextTestRunner().run(suite) runner.run(suite) fp.close() if __name__ == '__main__': #unittest.main(exit = False , verbosity = 2) #它是全局方法,把它屏蔽后,不在suite的用例就不會跑,exit = False表示中間有用例失敗也繼續執行;還有比較常用的verbosity=2,表示顯示def名字 suite()
總結:
postman可以幫助完成一半的工作
unittest+requests可以實現斷言,方便持續集成
順便提一下
如果你也喜歡Python 這里有一群Python愛好者匯集在此。
關注微信公眾號:【軟件測試技術】,回復 888,獲取QQ群號。
