1、准備工作
a、本機已配置好Python 3.X開發環境。開發工具推薦PyCharm
b、安裝requests庫,命令:pip install requests。
c、准備好要測試接口的相關信息:請求url,header,body等。
本次測試使用的接口是使用Django框架開發的簡單接口,header為空,不涉及cookie,實際的接口測試過程中header一般不為空。
2、編寫接口測試代碼
以post請求為例,測試代碼如下:

1 #coding:utf-8 2 #導入request庫 3 import requests 4 #請求URL 5 url = "http://127.0.0.1:8089/login/" 6 #請求數據 7 data = { 8 "username":"stephen", 9 "password":123456 10 } 11 #發送請求,一般還需要提供header信息,此處為demo,暫不關注 12 reponse1 = requests.post(url=url,data=data) 13 res_json = reponse1.json() 14 print(res_json) 15 flag = reponse1.status_code == 200 and res_json["status"] == 200 and res_json["msg"]=="login sucesss" 16 #假如http code為200,status為“200” msg為“login sucesss”則測試通過 17 if flag: 18 print("pass") 19 else: 20 print("fail")
運行結果:
以get接口為例,測試代碼如下:

1 #coding:utf-8 2 #導入request庫 3 import requests 4 #請求URL 5 url = "http://127.0.0.1:8089/login?username=vicent&tel=15209876533" 6 res = requests.get(url=url) 7 print(res.json())
運行結果:
3、優化
上面的代碼雖然實現了接口自動化,但是代碼不可復用。GET請求發送使用的是方法:requests.get,POST請求發送使用的是方法:requests.psot。
可以編寫一個函數,傳入請求的類型是GET,還是POST或者其它的類型。
1 #coding:utf-8 2 #導入request庫 3 import requests 4 def http_run(method,url,data=None,headers=None): 5 if method == "GET": 6 res = requests.get(url=url,data=data,headers=headers) 7 else: 8 res = requests.post(url=url,data=data,headers=headers) 9 return res 10 11 #case1 psot 12 url = "http://127.0.0.1:8089/login/" 13 data = { 14 "username":"stephen", 15 "password":123456 16 } 17 #發送請求 18 res = http_run("POST",url=url,data=data) 19 #校驗結果 20 21 assert res.json()["status"] == 200 22 #case2 get 23 #發送請求 24 url1 = "http://127.0.0.1:8089/login?username=vicent&tel=15209876533" 25 res1= http_run("GET",url=url1) 26 #校驗結果 27 assert res1.json()["tel"] =="15209876533"
4、引入unittest單元測試框架管理用例
先將上面優化過的http_run方法,抽象成一個class,方便后續調用。
1 import requests 2 class http_run: 3 def http_run(self,method,url,data=None,headers=None): 4 if method == "GET": 5 res = requests.get(url=url,data=data,headers=headers) 6 else: 7 res = requests.post(url=url,data=data,headers=headers) 8 return res
1 #導入unittest包 2 import unittest 3 #定義一個class http_test,繼承於unittest.TestCase 4 class http_test(unittest.TestCase): 5 #在每個測試用例執行前運行的方法 6 def setUp(self): 7 print("run before test case") 8 #實例化 http_run 9 self.run = http_run() 10 11 #測試用例,必須以test開頭 12 def test_get(self): 13 url1 = "http://127.0.0.1:8089/login?username=vicent&tel=15209876533" 14 res = self.run.http_run("GET",url1) 15 print(res.json()) 16 self.assertEqual(res.json()["tel"],"15209876533") 17 def test_post(self): 18 url = "http://127.0.0.1:8089/login/" 19 data = { 20 "username":"stephen", 21 "password":123456 22 } 23 res = self.run.http_run("POST",url,data) 24 print(res.json()) 25 self.assertEqual(res.json()["status"],200) 26 #在每個測試用例執行后運行的方法 27 def tearDown(self): 28 print("run after test case") 29 if __name__ == "__main__": 30 #創建測試套 31 suite = unittest.TestSuite() 32 #往測試套中添加用例 33 suite.addTest(http_test("test_get")) 34 suite.addTest(http_test("test_post")) 35 runner = unittest.TextTestRunner() 36 #運行測試用例 37 runner.run(suite)
運行結果:
unittest除了用於寫單元測試外,還可以完成接口自動化,基於unittest可以完成接口自動化框架的開發。