所謂數據驅動測試,簡單的理解爲數據的改變從而驅動自動化測試的執行,最終引起測試結果的改變。通過使用數據驅動測試的方法,可以在需要驗證多組數據測試場景中,使用外部數據源實現對輸入輸出與期望值的參數化,避免在測試中使用硬編碼的數據。
這種方法對於測試步驟相同而使用不同的輸入值和期望值的測試場景尤爲重要,比如設計一個ATM機取款的測試用例設計。
因此只需要創建一個測試腳本就可以處理上表的測試數據和條件的組合,使用數據驅動的模式,根據業務邏輯分解測試數據,並且定義變量,使用外部的excel裏的數據使其參數化,從而避免使用源測試腳本中的固定數據,這種方式可以將測試腳本與測試數據分開,使得測試腳本在不同的數據集合下高度複用。
數據驅動的模式不僅可以幫助增加類似複雜條件場景下的測試覆蓋,還可以極大的減少對測試代碼的編寫和維護工作。
數據驅動測試的有以下幾個原理:
1 .測試數據爲多個字典的list類型
2.測試類前加修飾@ddt.ddt
3.case前加修飾@ddt.data()
4.運行後用例會自動加載成三個單獨的用例
- 1
- 2
- 3
- 4
1、安裝ddt庫
ddt(Data Driver Test)的庫可以將測試中的變量進行參數化,使用之前先進行在線安裝。
爲了創建數據驅動測試,需要在測試類上使用 @ddt 裝飾符,在測試方法上使用 @data 裝飾符。@data 裝飾符可以把參數當做測試數據,參數可以是單個值,列表,元組,字典,對於列表和元組,需要使用 @unpack 裝飾符把元組和列表解析成多個參數。
2、安裝xlrd庫
爲了支持對Excel 數據表的支持,需要安裝 xlrd 庫
xlrd 庫提供了對工作簿,工作表以及單元格的讀取方法,如果需要往表格中寫入數據,需要安裝xlwt 庫,此外 openpyxl 庫提供了對錶格的讀寫操作。
3、實例
對於如何使用上述的幾個裝飾符,通過以下實例進行剖析
import unittest
from ddt import ddt,data,unpack
@ddt
class MyTestCase(unittest.TestCase):
@data(1,2,3)
def test_something(self,value):
self.assertEqual(value, 2)
if __name__ == '__main__':
unittest.main(verbosity=2)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
輸出結果:
對於有兩個參數的:
import unittest
from ddt import ddt,data,unpack
@ddt
class MyTestCase(unittest.TestCase):
@data((1,2),(2,3))
@unpack
def test_something(self,value1,value2):
print value1,value2
self.assertEqual(value2, value1+1)
if __name__ == '__main__':
unittest.main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
輸出結果:
基於以上的基礎,現在從 Excel 中讀取數據,數據表如下:
設計一個模塊對 excel 中的數據進行讀取和簡單的處理,首先看看在excel中如何操作的
# 打開exlce表格,參數是文件路徑
data = xlrd.open_workbook('test.xlsx')
# table = data.sheets()[0] # 通過索引順序獲取
# table = data.sheet_by_index(0) # 通過索引順序獲取
table = data.sheet_by_name(u'Sheet1') # 通過名稱獲取
nrows = table.nrows # 獲取總行數
ncols = table.ncols # 獲取總列數
# 獲取一行或一列的值,參數是第幾行
print table.row_values(0) # 獲取第一行值
print table.col_values(0) # 獲取第一列值
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
讀取上述的 Excel 表格
# coding:utf-8
import xlrd
class ExcelUtil():
def __init__(self, excelPath, sheetName):
self.data = xlrd.open_workbook(excelPath)
self.table = self.data.sheet_by_name(sheetName)
self.keys = self.table.row_values(0)
self.rowNum = self.table.nrows
self.colNum = self.table.ncols
def dict_data(self):
if self.rowNum <= 1:
print("the sum of line is less than 1")
else:
r = []
j=1
for i in range(self.rowNum-1):
s = {}
values = self.table.row_values(j)
for x in range(self.colNum):
s[self.keys[x]] = values[x]
r.append(s)
j+=1
return r
if __name__ == "__main__":
filepath = "information.xlsx"
sheetName = "Sheet1"
data = ExcelUtil(filepath, sheetName)
print data.dict_data()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
輸出結果:
C:\Python27\python.exe F:/py_test/UnitTest/importexcel.py
[{u'username': u'Jack', u'country': u'US', u'password': 123456.0},
{u'username': u'Pan', u'country': u'CHINA', u'password': u'abcdef'},
{u'username': u'John', u'country': u'UK', u'password': u'su39n3'}]
Process finished with exit code 0
- 1
- 2
- 3
- 4
- 5
- 6
現在導入excel測試數據,模塊爲:ExcelUtil.py
import xlrd
class ExcelUtil(object):
def __init__(self, excelPath, sheetName):
self.data = xlrd.open_workbook(excelPath)
self.table = self.data.sheet_by_name(sheetName)
# get titles
self.row = self.table.row_values(0)
# get rows number
self.rowNum = self.table.nrows
# get columns number
self.colNum = self.table.ncols
# the current column
self.curRowNo = 1
def next(self):
r = []
while self.hasNext():
s = {}
col = self.table.row_values(self.curRowNo)
i = self.colNum
for x in range(i):
s[self.row[x]] = col[x]
r.append(s)
self.curRowNo += 1
return r
def hasNext(self):
if self.rowNum == 0 or self.rowNum <= self.curRowNo:
return False
else:
return True
if __name__ == "__main__":
filepath = "information.xlsx"
sheetName = "Sheet1"
data = ExcelUtil(filepath, sheetName)
print data.hasNext()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
測試模塊爲:
import unittest
import ddt
#from driver.ExcelUtil import ExcelUtil
from ExcelUtil import ExcelUtil
excel = ExcelUtil('information.xlsx', 'Sheet1')
@ddt.ddt
class DataTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('start')
@classmethod
def tearDownClass(cls):
print('stop')
@ddt.data(*excel.next())
def testLogin(self, data):
print(data['username'])
print(data['password'])
print(data['country'])
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(DataTest)
unittest.TextTestRunner(verbosity=2).run(suite)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
這裏在這兩個模塊對應的目錄下創建一個名爲information.xlsx的Excel數據表格
輸出結果爲: