12.数据驱动ddt
安装ddt:pip install ddt
安装xlrd:pip install xlrd
注:ddt不能脱离unittest使用,unittest框架会循环执行excel表中数据,无需写for循环
一、数据在脚本中维护(unittest框架,ddt)
从脚本中读取数据,基本思路:
1.数据data1写成list的格式
2.脚本中加2行修饰符:类前加@ddt.ddt; 测试用例前加@ddt.data(*data1)
3.在test_开头的测试用例中,给一个参数testdata接收数据
4.脚本中读取data1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding:utf-8 2 import ddt 3 import unittest 4 5 data1 = [ 6 {"user": "test11", "psw": "123"}, 7 {"user": "test22", "psw": "456"}, 8 {"user": "test33", "psw": "789"} 9 ] 10 11 @ddt.ddt #用到ddt需要在类上边加修饰符 12 class GetData(unittest.TestCase): 13 14 @ddt.data(*data1) #方法上边加修饰符,分开传入data1的数据 15 def test_get_data(self, testdata): #传变量testdata去接受数据 16 print("测试数据为:%s" %testdata) 17 18 if __name__ == "__main__": 19 unittest.main()
二、数据在Excel中维护(unittest框架,ddt)
Excel操作基本方法:
data = xlrd.open_workbook(‘test.xlsx’) #打开excel表格,参数是文件路径
# table = data.sheets()[0] #通过索引顺序获取
# table = data.sheet_by_index(0) #通过索引顺序获取
table = data.sheet_by_name(u‘Sheet’) #通过名称获取
nrows = table.nrows #获取总行数
ncols = table.ncols #获取总列数
# 获取一行或一列的值,参数是第几行
table.row_values(0) #获取第一行值
table.col_values(0) #获取第一列值
注意:excel中的数字内容,设置文本格式保存
从excel中读取数据,基本思路:
1.封装方法,读取excel。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding:utf-8 2 import xlrd 3 class ExcelUtil(): 4 def __init__(self, excelPath, sheetName): 5 self.data = xlrd.open_workbook(excelPath) 6 self.table = self.data.sheet_by_name(sheetName) 7 # 获取第一行作为key值 8 self.keys = self.table.row_values(0) 9 # 获取总行数 10 self.rowNum = self.table.nrows 11 # 获取总列数 12 self.colNum = self.table.ncols 13 14 def dict_data(self): 15 if self.rowNum <= 1: 16 print("总行数小于1") 17 else: 18 r = [] 19 j=1 20 for i in range(self.rowNum-1): 21 s = {} 22 # 从第二行取对应values值 23 values = self.table.row_values(j) 24 for x in range(self.colNum): 25 s[self.keys[x]] = values[x] 26 r.append(s) 27 j+=1 28 return r 29 30 if __name__ == "__main__": 31 filepath = r"D:\apidatadriver\data.xlsx" 32 sheetName = "Sheet1" 33 data = ExcelUtil(filepath, sheetName) 34 print(data.dict_data())
2.以list格式打印出来,list中是字典格式
3.脚本中加2行修饰符:类前加@ddt.ddt; 测试用例前加@ddt.data(*data1)
4.在test_开头的测试用例中,给一个参数testdata接收数据
5.脚本中读取data1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding:utf-8 2 import requests 3 4 class Login(): 5 #登录,返回跳转的url地址 6 def login(self, s, username="admin", pwd="123456"): 7 url = "http://xxxx/login" 8 h = { 9 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0" 10 } 11 body = { 12 "username": username, 13 "pwd": pwd, 14 "kaptcha": None 15 } 16 r = s.post(url, headers=h, json=body) 17 newurl = r.json()["redirectUrl"] 18 # print(r.content.decode("utf-8")) 19 # print(r.status_code) 20 return newurl 21 22 #打开上一步返回的url地址,获取cookies,用s去自动管理 23 def get_cookies(self, s, newurl): 24 r = s.get(newurl, verify=False) 25 # print(r.content.decode("utf-8")) 26 27 def get_version(self, s): 28 url = "http://xxxxx/getversion" 29 r = s.post(url) 30 return r.json()["data"]["version"] 31 32 33 if __name__ == "__main__": 34 login1 = Login() 35 s = requests.session() 36 newurl = login1.login(s) 37 login1.get_cookies(s, newurl) 38 login1.get_version(s)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding:utf-8 2 from version.erp_01_login import Login 3 from version.readexcel import ExcelUtil 4 import requests 5 import ddt 6 import unittest 7 8 # 从文件中读取数据,赋值给data1,以list中字典格式显示,需要维护excel表 9 d = ExcelUtil("userinfo.xlsx", sheetName="Sheet1") 10 data1 = d.dict_data() 11 print(data1) 12 13 @ddt.ddt 14 class TestGetVersion(unittest.TestCase): 15 ''' 16 获取租户的版本号 17 ''' 18 @ddt.data(*data1) 19 def test_version(self, info): 20 erplogin = Login() 21 s = requests.session() 22 username = info['code'] 23 code = info['username'] 24 password = info['password'] 25 26 try: 27 newurl = erplogin.login(s, username, code, password) 28 erplogin.get_cookies(s, newurl) 29 ver = erplogin.get_version(s) 30 print("租户%s的版本号为:%s" % (username.center(10), ver)) #% (username.ljust(10), ver)左对齐,填充到10位 31 except: 32 print("租户%s登录失败" % username.center(10)) 33 34 if __name__ == "__main__": 35 unittest.main()
维护的excel表如下:
三、从excel中读取数据(无ddt)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding: utf-8 2 from readfromexcel.readexcel import ExcelUtil 3 4 class ReadFromExcel(): 5 def readFromExcel(self): 6 d = ExcelUtil("userinfo.xlsx", sheetName="Sheet1") 7 data1 = d.dict_data() 8 print(data1) 9 print(type(data1)) 10 11 if __name__ == "__main__": 12 dt = ReadFromExcel() 13 dt.readFromExcel()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding: utf-8 2 from readfromexcel.readexcel import ExcelUtil 3 4 class ReadFromExcel(): 5 def readFromExcelBySheetName(self): 6 '''第一种方式读取多个Sheet中的数据''' 7 sheets = ["Sheet1", "Sheet2", "Sheet3"] 8 for i in sheets: 9 try: 10 d = ExcelUtil("userinfo.xlsx", sheetName=i) 11 data1 = d.dict_data() 12 print("%s的数据:" %i) 13 print(data1) 14 except: 15 print("%s无数据:" %i) 16 17 def readFromExcelBySheetName1(self): 18 '''第二种方式读取多个Sheet中的数据''' 19 for i in range(1, 4): 20 sheetName = "Sheet%s" %i 21 try: 22 d = ExcelUtil("userinfo.xlsx", sheetName=sheetName) 23 data1 = d.dict_data() 24 print("Sheet%s"%i,"的数据") 25 print(data1) 26 except: 27 print("Sheet%s"%i,"无数据") 28 29 if __name__ == "__main__": 30 dt = ReadFromExcel() 31 dt.readFromExcelBySheetName() 32 dt.readFromExcelBySheetName1()