Python接口測試之數據驅動

Python Excel JSON 操作系統 川石信息 2019-06-23
Python接口測試之數據驅動

在接口的自動化測試中,客戶端發送請求給服務端,在客戶端發送請求的時候,包含了請求地址,請求方法,以及請求參數等數據,那麼在接口的自動化測試中如何來分離這些請求地址和請求參數了,最好的方式是以數據驅動的方式分離到excel中,這樣在excel中直接維護,即使後期由於某些原因修改了請求參數,在excel中修改也是很快的。在下來的案例中,一個系統,請求登錄成功後,服務端返回token給客戶端,客戶端再次請求的時候需要帶著這個token。關於HTTP的請求流程,token,session這些的處理,在前面的文章中有很詳細的介紹,這裡就不再介紹,下面會直接引入代碼實戰這部分。

首先安裝處理excel的庫,這裡使用的Python版本是3.6,所以先安裝第三方庫,安裝的命令為(已安裝的會提示已存在):

Python接口測試之數據驅動

安裝xlrd庫好之後,創建excel文件,把請求地址和請求參數分離到excel中,見excel的數據:

Python接口測試之數據驅動

現在來編寫讀取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函數後,見執行的結果截圖:

Python接口測試之數據驅動

在截圖中可以看到,數據類型是列表,並且返回了所有的數據,再次編寫函數,返回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中,第二個接口這些成功後,直接失敗,見失敗信息:

Python接口測試之數據驅動

問題在於數據分離後,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)

相關推薦

推薦中...