- 什么是DDT
data driver test數(shù)據(jù)驅(qū)動測試。就是從數(shù)據(jù)文件中:Excel乌叶,csv, yaml凤巨,讀取測試數(shù)據(jù)砚作,然后把讀取到的數(shù)據(jù)傳入到用例里面纱控,通過改變數(shù)據(jù)文件中的數(shù)據(jù)來驅(qū)動自動化測試用例去執(zhí)行 - 為什么要用DDT
能夠讓我門的用例覆蓋正反例 - DDT使用方式(通過裝飾器來使用)
在函數(shù)或者類上面加一個裝飾器用來實現(xiàn)一些特定的功能
@ddt :裝飾類,用于聲明當前類使用ddt數(shù)據(jù)驅(qū)動
@data:裝飾函數(shù),給函數(shù)傳值
@unpack:裝飾函數(shù),給數(shù)據(jù)解包
@file_data:裝飾函數(shù)黍氮,直接讀取yaml, json文件 -
代碼實例
exel測試數(shù)據(jù)
data
-loginweb_data.xlsx
image.png
# testcase
#-test_loginweb.py
from ddt import ddt, data, unpack
from base.base_util import BaseUtil
from commons.excel_util import ExcelUtil
from pageobject.login_page import LoginPage
@ddt
class TestLoginWeb(BaseUtil):
@data(*ExcelUtil().read_excel())
@unpack
def test_01_open_page(self, index, username, password):
lp = LoginPage(self.driver)
lp.login_web(username, password)
if index == 1:
print('正例')
# commons
# -excel_util.py
import os.path
import openpyxl
class ExcelUtil:
def get_object_path(self):
return os.path.abspath(os.path.dirname(__file__)).split('commons')[0]
def read_excel(self):
# openpyxl, xlrd
# 加載excel工作簿
wb = openpyxl.load_workbook(self.get_object_path()+'data/loginweb_data.xlsx')
# 加載sheet對象
sheet = wb['login']
# 獲得excel的行數(shù)和列數(shù)
print(sheet.max_row, sheet.max_column)
# 循環(huán)
all_list = []
for rows in range(2, sheet.max_row+1):
temp_list = []
for cols in range(1, sheet.max_column+1):
temp_list.append(sheet.cell(rows, cols).value)
all_list.append(temp_list)
return all_list