1、首先安裝dtt模塊app
數據驅動原理測試
1.測試數據爲多個字典的list類型spa
2.測試類前加修飾@ddt.ddtexcel
3.case前加修飾@ddt.data()code
4.運行後用例會自動加載成N個單獨的用例blog
2、實例utf-8
ExcellData.pyget
1 # coding:utf-8 2 import xlrd 3 4 5 class excel: 6 def __init__(self): 7 self.test_data_path = 'F:\\testdata.xlsx' 8 9 def open_excel(self,file): 10 u'''讀取excel文件''' 11 try: 12 data = xlrd.open_workbook(file) 13 return data 14 except Exception, e: 15 raise e 16 17 def excel_table(self,file, sheetName): 18 u'''裝載list''' 19 data = self.open_excel(file) 20 # 經過工做表名稱,獲取到一個工做表 21 table = data.sheet_by_name(sheetName) 22 # 獲取行數 23 Trows = table.nrows 24 # 獲取 第一行數據 25 Tcolnames = table.row_values(0) 26 lister = [] 27 for rownumber in range(1,Trows): 28 row = table.row_values(rownumber) 29 if row: 30 app = {} 31 for i in range(len(Tcolnames)): 32 app[Tcolnames[i]] = row[i] 33 lister.append(app) 34 return lister 35 36 def get_list(self,sheetname): 37 try: 38 data_list = self.excel_table(self.test_data_path, sheetname) 39 assert len(data_list)>=0,u'excel標籤頁:'+sheetname+u'爲空' 40 return data_list 41 except Exception as e: 42 raise e
test.pyit
1 # coding:utf-8 2 __author__ = 'Helen' 3 ''' 4 project:ddt數據驅動 5 ''' 6 import unittest,ddt 7 from ExcelData import excel 8 9 # 測試數據 10 excel_data = excel() 11 testdata = excel_data.get_list('userData') 12 13 @ddt.ddt 14 class TestDtt(unittest.TestCase): 15 @classmethod 16 def setUpClass(cls): 17 print u'開始測試---------------' 18 19 @ddt.data(*testdata) 20 def test(self, data): 21 print 'username:'+data['username']+',password:'+data['password'] 22 23 @classmethod 24 def tearDownClass(cls): 25 print u'結束測試------------------' 26 27 if __name__=='__main__': 28 unittest.main()