Python unittest excel數據驅動

<span style="font-family: 微軟雅黑; widows: auto; background-color: rgb(255, 255, 255);">這兩天在網上找關於Python的unittest框架的數據驅動,一直沒有找到一個比較詳細的資料,在這裏就自己寫一個啦。。。</span>

1. 首先安裝ddt,ddt可以實現多數據的處理
C:\windows\system32>pip install wheel
You are using pip version 6.0.8, however version 6.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting wheel
  Downloading wheel-0.24.0-py2.py3-none-any.whl (63kB)
    100% |################################| 65kB 476kB/s
Installing collected packages: wheel

Successfully installed wheel-0.24.0

C:\windows\system32>pip install C:\Users\***\Downloads\ddt-1.0.0-py2.py3-none-
any.whl
You are using pip version 6.0.8, however version 6.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Processing c:\users\***\downloads\ddt-1.0.0-py2.py3-none-any.whl
Installing collected packages: ddt

Successfully installed ddt-1.0.0

2.安裝xlrd,excel處理包

3.對excel數據進行處理,這個類主要是將excel中所有的數據封裝成一個數據,數組裏面append所有列的dic,代碼如下:
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
4.在unittest框架中使用ddt進行迭代
import unittest

import ddt
from driver.ExcelUtil import ExcelUtil

excel = ExcelUtil('excelPath', '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)

@ddt.data接收一個可迭代的類型,來判斷執行的次數,excel.next()返回的是一個數組

excel數據如下
username password country
a 1 CH
b 2 USA

執行結果:
Finding files... done.
Importing test modules ... done.


start
a
1.0
CH
----------------------
b
2.0
USA
----------------------
stop
----------------------------------------------------------------------
Ran 2 tests in 0.001s


OK
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章