在接口的自動化測試中,用戶端發送請求給服務端,在用戶端發送請求的時候,包含了請求位址,請求方法,以及請求參數等資料,那麼在接口的自動化測試中如何來分離這些請求位址和請求參數了,最好的方式是以資料驅動的方式分離到excel中,這樣在excel中直接維護,即使後期由于某些原因修改了請求參數,在excel中修改也是很快的。在下來的案例中,一個系統,請求登入成功後,服務端傳回token給用戶端,用戶端再次請求的時候需要帶着這個token。關于HTTP的請求流程,token,session這些的處理,在前面的文章中有很詳細的介紹,這裡就不再介紹,下面會直接引入代碼實戰這部分。
首先安裝處理excel的庫,這裡使用的Python版本是3.6,是以先安裝第三方庫,安裝的指令為(已安裝的會提示已存在):
安裝xlrd庫好之後,建立excel檔案,把請求位址和請求參數分離到excel中,見excel的資料:
現在來編寫讀取excel中的資料,主要思路為讀取excel的資料後,把資料類型轉為字典的資料類型,并且是按行的方式讀取,實作的代碼:
#!/usr/bin/env python
#-*-coding:utf-8-*-
#author:wuya
import os
import xlrd
import json
def readExcel(rowx, filePath='data.xlsx'):
'''
讀取excel中資料并且傳回
:parameter filePath:xlsx檔案名稱
:parameter rowx:在excel中的行數
'''
book = xlrd.open_workbook(filePath)
sheet = book.sheet_by_index(0)
return sheet.row_values(rowx)
複制
來讀取excel中的資料,并且檢視它的資料類型是否是期望的字典類型,調用readExcel函數後,見執行的結果截圖:
在截圖中可以看到,資料類型是清單,并且傳回了所有的資料,再次編寫函數,傳回XX行的請求位址和請求參數,在excel中,存在的共同點是不管資料是在那一行,第二列永遠是請求位址,第三列是請求參數,編寫擷取請求位址和請求參數的函數,見源碼:
#!/usr/bin/env python
#-*-coding:utf-8-*-
#author:wuya
import os
import xlrd
import json
def readExcel(rowx, filePath='data.xlsx'):
'''
讀取excel中資料并且傳回
:parameter filePath:xlsx檔案名稱
:parameter rowx:在excel中的行數
'''
book = xlrd.open_workbook(filePath)
sheet = book.sheet_by_index(0)
return sheet.row_values(rowx)
def getUrl(rowx):
'''
擷取請求URL
:parameter rowx:在excel中的行數
'''
return readExcel(rowx)[1]
def getData(rowx):
'''
擷取請求參數
:parameter rowx:在excel中的行數
'''
return json.loads(readExcel(rowx)[2])
複制
在上面代碼中,新增了擷取擷取請求位址和請求參數,因為請求參數資料類型是字典,是以進行了反序列化的處理。
下來編寫接口用例,見資料未分離的接口用例,見實作的代碼:
import unittest
import time as t
import requests
class ApiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
t.sleep(1)
@classmethod
def tearDownClass(cls):
pass
def getHeaders(self):
return {
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb',
'Content-Type':'application/json;charset=UTF-8'}
def test_login_001(self):
'''登入業務:登入成功'''
r=requests.post(
url='http://180.97.80.42:9090/v5/login',
json={"username":"6666666666","password":"8144ed050cd8d053f24a1e179d7529e17c3a2ba9cfcfcd7d3bda9ec6a8156758"},
headers=self.getHeaders())
self.assertEqual(r.status_code,200)
self.assertEqual(r.json()['status'],0)
with open('token','w') as f:
f.write(r.json()['data']['token'])
def getToken(self):
with open('token','r') as f:
return f.read()
def test_login_002(self):
'''登入業務:檢視使用者資訊'''
r=requests.post(
url='http://180.97.80.42:9090/v5/infoGet',
json={"token":self.getToken()},
headers=self.getHeaders())
self.assertEqual(r.status_code,200)
self.assertEqual(r.json()['status'],0)
if __name__ == '__main__':
unittest.main(verbosity=2)
複制
下來使用資料驅動的方式把請求位址和請求參數分離出來,見修改後的源碼:
#!/usr/bin/env python
#-*-coding:utf-8-*-
#author:wuya
import os
import xlrd
import json
def readExcel(rowx, filePath='data.xlsx'):
'''
讀取excel中資料并且傳回
:parameter filePath:xlsx檔案名稱
:parameter rowx:在excel中的行數
'''
book = xlrd.open_workbook(filePath)
sheet = book.sheet_by_index(0)
return sheet.row_values(rowx)
def getUrl(rowx):
'''
擷取請求URL
:parameter rowx:在excel中的行數
'''
return readExcel(rowx)[1]
def getData(rowx):
'''
擷取請求參數
:parameter rowx:在excel中的行數
'''
return json.loads(readExcel(rowx)[2])
import unittest
import time as t
import requests
class ApiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
t.sleep(1)
@classmethod
def tearDownClass(cls):
pass
def getHeaders(self):
return {
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb',
'Content-Type':'application/json;charset=UTF-8'}
def test_login_001(self):
'''登入業務:登入成功'''
r=requests.post(
url=getUrl(1),
json=getData(1),
headers=self.getHeaders())
self.assertEqual(r.status_code,200)
self.assertEqual(r.json()['status'],0)
with open('token','w') as f:
f.write(r.json()['data']['token'])
def getToken(self):
with open('token','r') as f:
return f.read()
def test_login_002(self):
'''登入業務:檢視使用者資訊'''
r=requests.post(
url=getUrl(2),
json=getData(2),
headers=self.getHeaders())
print(r.text)
self.assertEqual(r.status_code,200)
self.assertEqual(r.json()['status'],0)
if __name__ == '__main__':
unittest.main(verbosity=2)
複制
資料分離後,維護資料統一是在excel中,第二個接口這些成功後,直接失敗,見失敗資訊:
問題在于資料分離後,test_login_002的測試用例請求資料與登入成功後的token不一緻,導緻了錯誤,那麼如何對這些動态參數進行處理了,處理的思路是:
- 從excel中讀取資料
- 對如token這些動态參數再次進行指派
- 傳回指派後的資料
- 調用指派後的資料
依據如上的思路,對getToken方法進行修改,對token再次進行指派,新增setToken方法,見修改後的該方法代碼:
def getToken(self):
'''讀取token檔案裡面的内容'''
with open('token','r') as f:
return f.read()
def setToken(self,rowx):
'''
對動态參數token進行指派
:parameter rowx:在excel中的行數
'''
dict1=getData(rowx)
#對tokek指派
dict1['token']=self.getToken()
return dict1
複制
在test_login_002的接口用例中,調用請求參數直接調用setToken方法,這樣就不會出現錯誤了,見完整的代碼:
#!/usr/bin/env python
#-*-coding:utf-8-*-
#author:wuya
import os
import xlrd
import json
import unittest
import time as t
import requests
def readExcel(rowx, filePath='data.xlsx'):
'''
讀取excel中資料并且傳回
:parameter filePath:xlsx檔案名稱
:parameter rowx:在excel中的行數
'''
book = xlrd.open_workbook(filePath)
sheet = book.sheet_by_index(0)
return sheet.row_values(rowx)
def getUrl(rowx):
'''
擷取請求URL
:parameter rowx:在excel中的行數
'''
return readExcel(rowx)[1]
def getData(rowx):
'''
擷取請求參數
:parameter rowx:在excel中的行數
'''
return json.loads(readExcel(rowx)[2])
class ApiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
t.sleep(1)
@classmethod
def tearDownClass(cls):
pass
def getHeaders(self):
return {
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb',
'Content-Type':'application/json;charset=UTF-8'}
def test_login_001(self):
'''登入業務:登入成功'''
r=requests.post(
url=getUrl(1),
json=getData(1),
headers=self.getHeaders())
self.assertEqual(r.status_code,200)
self.assertEqual(r.json()['status'],0)
with open('token','w') as f:
f.write(r.json()['data']['token'])
def getToken(self):
'''讀取token檔案裡面的内容'''
with open('token','r') as f:
return f.read()
def setToken(self,rowx):
'''
對動态參數token進行指派
:parameter rowx:在excel中的行數
'''
dict1=getData(rowx)
#對tokek指派
dict1['token']=self.getToken()
return dict1
def test_login_002(self):
'''登入業務:檢視使用者資訊'''
r=requests.post(
url=getUrl(2),json=self.setToken(2),headers=self.getHeaders())
self.assertEqual(r.status_code,200)
self.assertEqual(r.json()['status'],0)
if __name__ == '__main__':
unittest.main(verbosity=2)
複制