今年肯定是要把Python學到一定程度的,否則感覺自己混不下去了,那就開始半掙扎的咸魚生活吧。
------------------------------------------------------------------------------------------------------------------------------------
這里用的接口相關參數看這里:https://www.sojson.com/blog/305.html,提供了一個免費調用的天氣api。
接口測試嘛,一般先把這個接口調通,然后進行一些測試用例的設計(可以用等價類、邊界值等方法),之后執行測試用例查看response是否符合接口文檔中的預期。按照這個邏輯,開始:
1、先調通,用到requests庫,那就先import(需要先在設置中添加requests),然后發起請求。
import requests r=requests.get('http://t.weather.sojson.com/api/weather/city/101250101') response_data=r.json() print(response_data)
還可以把一些信息打印出來,可以用作斷言
#獲取日期,響應信息,狀態,城市 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'])
2、接口這樣就算是調通了,就開始設計測試用例(這里示例正常的、空參、參數值錯誤三種情況),然后符合預期(預期就用斷言去判斷了),這里用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'),無奈效果不理想,后續再看吧,運行結果如下:
3、都到這了,順手加個報告吧,這里用BSTestRunner(HTMLTestRunner)。另創建一個Python File,代碼如下:
先在這里(https://github.com/easonhan007/HTMLTestRunner)下載BSTestRunner.py,然后放到.\python\lib目錄下,代碼中引用就行了。
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的,有興趣都可以了解一下。