今年確定是要把Python學到必定程度的,不然感受本身混不下去了,那就開始半掙扎的鹹魚生活吧。html
------------------------------------------------------------------------------------------------------------------------------------python
這裏用的接口相關參數看這裏:https://www.sojson.com/blog/305.html,提供了一個免費調用的天氣api。git
接口測試嘛,通常先把這個接口調通,而後進行一些測試用例的設計(能夠用等價類、邊界值等方法),以後執行測試用例查看response是否符合接口文檔中的預期。按照這個邏輯,開始: github
一、先調通,用到requests庫,那就先import(須要先在設置中添加requests),而後發起請求。json
import requests r=requests.get('http://t.weather.sojson.com/api/weather/city/101250101') response_data=r.json() print(response_data)
還能夠把一些信息打印出來,能夠用做斷言api
#獲取日期,響應信息,狀態,城市 print(response_data['date']) print(response_data['message']) print(response_data['status']) print(response_data['cityInfo']['city']) #獲取當日天氣具體信息 print(response_data['data']['forecast'][0]['ymd']) print(response_data['data']['forecast'][0]['type']) print(response_data['data']['forecast'][0]['high']) print(response_data['data']['forecast'][0]['low'])
二、接口這樣就算是調通了,就開始設計測試用例(這裏示例正常的、空參、參數值錯誤三種狀況),而後符合預期(預期就用斷言去判斷了),這裏用python的單元測試框架unittest來集成,關於這個框架的介紹,能夠百度不少資料,也能夠直接按照Ctrl,而後點擊「unittest」查看它的源碼說明。理清邏輯,那就開始:框架
import requests import unittest from time import sleep class WeatherTest(unittest.TestCase): def setUp(self): pass #正常查詢長沙的天氣,斷言 def test_weather_changsha(self): r=requests.get('http://t.weather.sojson.com/api/weather/city/101250101') result= r.json() #斷言 self.assertEqual(result['status'],200) self.assertEqual(result['message'],'Success !') self.assertEqual(result['cityInfo']['city'],'長沙市') #設置間隔時間,避免IP被封,這個接口自己有限制的 sleep(5) # 不傳city_code,斷言 def test_weather_no_reference(self): r=requests.get('http://t.weather.sojson.com/api/weather/city/') result=r.json() self.assertEqual(result['status'], 404) self.assertEqual(result['message'], 'Request resource not found.') sleep(5) #傳入一個不存在的city_code,斷言 def test_weather_reference_error(self): r=requests.get('http://t.weather.sojson.com/api/weather/city/100250101') result = r.json() self.assertEqual(result['status'], 403) self.assertEqual(result['message'], 'no_city_id') sleep(5) if __name__ == '__main__': unittest.main()
稍微瞭解一下unittest,就能把最上面調通接口的代碼改爲在unittest中這樣了。其實我是想把city_code作成參數化,而後傳進每一個def中(url='http://t.weather.itboy.net/api/weather/city/'+'city_code'),無奈效果不理想,後續再看吧,運行結果以下:單元測試
三、都到這了,順手加個報告吧,這裏用BSTestRunner(HTMLTestRunner)。另建立一個Python File,代碼以下:測試
先在這裏(https://github.com/easonhan007/HTMLTestRunner)下載BSTestRunner.py,而後放到.\python\lib目錄下,代碼中引用就好了。url
import unittest from BSTestRunner import BSTestRunner import time #指定測試用例和測試報告的路徑 test_dir='C:\\Users\\16520\\Desktop\\test_case' report_dir='C:\\Users\\16520\\Desktop\\reports' #加載測試用例 discover=unittest.defaultTestLoader.discover(test_dir,pattern='Weather_api.py') #定義報告的文件格式 now=time.strftime("%Y-%m-%d %H-%M-%S") report_name=report_dir+'/'+'test_report.html' #運行測試用例生成報告 with open(report_name,'wb') as f: runner=BSTestRunner(stream=f,title="Weather API Test Report",description="China City Weather Test Report") runner.run(discover)
執行以後在「C:\Users\16520\Desktop\reports」這個文件夾裏面就能看到一個html文件了,打開就能看到詳細的東西了
PS:網上有不少二開的HTMLTestRunner,加了不少東西,也有用Allure2作測試報告,集成Jenkins的,有興趣均可以瞭解一下。