|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 叶小贤 于 2020-5-7 17:11 编辑
主要就是这3个文件的交互,我最后在abc.xlsx中通过的用例写入PASS,未通过的写入失败
一共4条数据,1条PASS 3条失败 , 我跑了断点,每次结果都有写入进excel,但是都未保存,程序跑完后,只保存了最后一条写入的数据 ‘失败’
ID excepted data name result
1 {'code':0,'msg':'登录成功!'} ('python', '123456') max
2 {'code':0,'msg':'密码位数不足!'} ('python', '12345') jack
3 {'code':0,'msg':'账号错误!'} ('newpython', '123456') tom
4 {'code':0,'msg':'密码错误!'} ('python', '1234567') alen 失败
------------------------------------------------------
import openpyxl
import unittest
from HTMLTestRunner import HTMLTestRunner
class Case:
def __init__(self,result):
for item in result:
setattr(self,item[0],item[1])
class ReadExcel(object):
'''用来读取excel数据'''
def __init__(self,file_name,sheet_name):
'''
初始化读取对象
:param file_name: 文件名 --> str
:param sheet_name: 表单名 --> str
'''
self.file_name = file_name
self.wb = openpyxl.load_workbook(file_name)
self.sheet = self.wb[sheet_name]
def __del__(self):
self.wb.close()
def write_data(self,row,column,msg):
self.sheet.cell(row=row,column=column,value=msg)
self.wb.save(self.file_name)
def read_data_obj2(self,list2): #存对象里
max_r = self.sheet.max_row
titles = []
result = []
data_all = []
for row in range(1, max_r + 1):
if row != 1:
cases = []
for column in list2:
case = self.sheet.cell(row, column).value
# case = self.sheet.cell(row, column).value
cases.append(case)
#result.append(dict(zip(titles, cases))) 存储对象只是在这步存数据不同
result= list(zip(titles,cases)) #利用定义的空类 Case ,将一条用例存入一个对象中(每一列对应对象的一个属性)
case_obj = Case(result)
# for i in result:
# setattr(case_obj,i[0],i[1])
data_all.append(case_obj)
else:
for column in list2:
title = self.sheet.cell(row, column).value
titles.append(title)
return data_all
------------------------------------------------------------------------------------
import unittest
from UnittestTest.INTERFACE import login_test
from UnittestTest.EXCELCLASS import ReadExcel
class LoginTest(unittest.TestCase):
def __init__(self,method_name,case_obj):
self.excpeted = eval(case_obj.excepted)
self.data = eval(case_obj.data)
self.row = case_obj.ID + 1
super().__init__(method_name)
self.wb = ReadExcel('abc.xlsx','Sheet1')
def setUp(self):
print('每一条用例执行之前,都会执行这个方法,做测试之前的环境准备工作...')
def tearDown(self):
print('每一条用例执行之后,都会执行这个方法,可以用作恢复环境')
def test_login(self):
res = login_test(*self.data)
try:
self.assertEqual(res,self.excpeted,res)
except AssertionError as e:
res = '失败'
raise e
else:
res = 'pass'
finally:
#在ECEL中回写
self.wb.write_data(row=self.row,column=5,msg=res)
-------------------------------------------------------------------------------------------
import openpyxl
import unittest
from UnittestTest.TestClass import LoginTest
from HTMLTestRunner import HTMLTestRunner
from UnittestTest.EXCELCLASS import ReadExcel
suite = unittest.TestSuite()
r = ReadExcel('abc.xlsx','Sheet1')
cases = r.read_data_obj2([1,2,3])
#遍历用例对象
for case in cases:
suite.addTest(LoginTest('test_login',case))
with open('report.html','wb') as f:
runner = HTMLTestRunner(
stream=f, # 传入文件句柄
verbosity=0, # 写入等级 0,1,2 三个选项 2最全
title='Login测试', # 报告名称
description='第一份HTML测试报告', # 报告相关描述
)
runner.run(suite)
|
|