1.Excel内容
路径下的文件
![路径下的文件](https://i-blog.csdnimg.cn/blog_migrate/85c91573dec32aa6354b873dd143c20a.png)
Sheet1
![Sheet1内容](https://i-blog.csdnimg.cn/blog_migrate/905314798b1b9cf69cb237de1570f6c2.png#pic_center)
Sheet2
![Sheet2](https://i-blog.csdnimg.cn/blog_migrate/e42e872f3f20f6701e12c0f057c22062.png#pic_center)
2.Py文件数据驱动
class DataDriver:
NAME = 'xuzhu'
AGE = '16'
ADDRESS = '汤臣一品'
if __name__ == '__main__':
print(DataDriver.NAME)
3.log.ini
[loggers]
keys=root
[handlers]
keys=fileHandler,streamHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=INFO
handlers=fileHandler,streamHandler
[handler_fileHandler]
class=FileHandler
level=INFO
formatter=simpleFormatter
args=('mylog.log','a','utf-8')
[handler_streamHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
[formatter_simpleFormatter]
format=%(asctime)s %(filename)s %(levelname)s %(funcName)s %(message)s
4.生成日志器的配置
'''
生成日志器的配置
'''
import logging.config
def get_log(path):
logging.config.fileConfig(path)
return logging.getLogger()
5.用于配置excel的写入格式内容
'''
用于配置excel的写入格式内容
把断言的结果Pass和Failed更加突出一些
PatternFill类用于定义颜色
Font类用于定义格式
'''
from openpyxl.styles import Alignment
from openpyxl.styles import Font
from openpyxl.styles import PatternFill
def pass_(cell,row,column):
cell(row=row, column=column).value = 'Pass'
cell(row=row, column=column).fill = PatternFill('solid',fgColor='AACF91')
cell(row=row, column=column).font = Font(bold = 'True')
cell(row=row, column=column).alignment = Alignment(horizontal='center', vertical='center')
def failed(cell,row,column):
cell(row=row, column=column).value = 'Failed'
cell(row=row, column=column).fill = PatternFill('solid',fgColor='FF0000')
cell(row=row, column=column).font = Font(bold = 'True')
cell(row=row, column=column).alignment = Alignment(horizontal='center', vertical='center')
6.excel文件读取类,用于实现测试用例文件的读取与执行
'''
excel文件读取类,用于实现测试用例文件的读取与执行
'''
import pathlib
import openpyxl
from class25.excel_driver import excel_conf
from class26 import log_conf
from class24.web_keys import Keys
def arguments(value):
data = dict()
if value:
str_temp = value.split(';')
for temp in str_temp:
t = temp.split("=", 1)
data[t[0]] = t[1]
else:
pass
return data
def path():
file = pathlib.Path(__file__).resolve().parents[1] / 'data/自动化测试用例demo.xlsx'
return file
def read(file,log):
excel = openpyxl.load_workbook(file)
failed_cases = list()
passed = 0
failed = 0
for name in excel.sheetnames:
sheet = excel[name]
log.info('*************正在执行{}Sheet页***************'.format(name))
try:
for values in sheet.values:
if type(values[0]) is int:
log.info('***********************正在执行:{}******************************'.format(values[3]))
data = arguments(values[2])
'''
调用的函数values[1],这是固定的。
操作行为的调用分为以下几种不同类型:
1.实例化
2.基于实例化对象进行的操作行为
3.断言机制:有预期与实际的对比,以及有单元格测试结果的写入
'''
'''
第一行open_browser
第二行open:getattr(key,'open')(**data)
key.open(**data)
key.open(url = 'http://www.baidu.com')
第三行input:getattr(key,'open')(**data)
key.input(**data)
key.input(by='id',value='kw',txt='虚竹的excel')
第四行click
'''
if values[1] == 'open_browser':
key = Keys(**data)
elif 'assert' in values[1]:
status = getattr(key, values[1])(expected=values[4],**data)
if status:
excel_conf.pass_(sheet.cell,row=values[0]+2,column=6)
passed+=1
else:
excel_conf.failed(sheet.cell,row=values[0]+2,column=6)
failed+=1
failed_cases.append(sheet)
excel.save(file)
else:
getattr(key, values[1])(**data)
except:
failed+=1
excel.close()
sum_=passed+failed
log.info('**************执行完毕*******************')
log.info('本次总计执行{0}条用例,成功{1}条,失败{2}条,通过率为{3}'.format(sum_,passed,failed,100*passed/sum_))
if len(failed_cases)>0:
failed_cases.insert(0,file)
return failed_cases
return None
def rerun(case):
excel = openpyxl.load_workbook(case[0])
for sheet in case:
for values in sheet.values:
7.main函数
import os
from class25.excel_driver.excel_read import path, read
from class26 import log_conf
if __name__ == '__main__':
log = log_conf.get_log('./conf/log.ini')
repeat = list()
cases = list()
for path,dir,files in os.walk('../class25/data/'):
for file in files:
file_type = os.path.splitext(file)[1]
file_name = os.path.splitext(file)[0]
if file_type == '.xlsx':
if 'old' not in file_name:
case_path = path+file
cases.append(case_path)
else:
log.error('文件类型错误:{}'.format(file))
for case in cases:
log.info('********正在执行{}文件*********'.format(case))
f = read(case,log)
if f:
repeat.append(f)
if len(repeat)>0:
pass
8.运行结果
![运行结果](https://i-blog.csdnimg.cn/blog_migrate/08dedcfa8f686aaa74c5f335038b00ee.png)