if len(line[0]) > 0 or len(line[1]) > 0: # 分组信息,不用执行 pass else: # 执行用例 logger.info(line) runcase(line, obj) writer.set_sheet(sheetname[0]) endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) writer.write(1, 4, endtime) writer.save_close() # 结果统计 res = Res() details = res.get_res('./lib/result-%s.xls' % casename) r = res.get_groups('./lib/result-%s.xls' % casename) # 关闭jvm shutdown() # 邮件处理 mail = Mail() htmlmodule = Txt('./conf/' + config.config['mailtxt']) html = htmlmodule.read()[0] # 对模块文本进行处理 # 替换总体统计信息 sumlist = ['status', 'passrate', 'starttime', 'endtime'] for s in sumlist: html = html.replace(s, details[s])
# 设置当前读取的sheet页面 reader.set_sheet(sheet) # 设置写的sheet页,保持读和写是同一个sheet writer.set_sheet(sheet) for i in range(reader.rows): writer.row = i line = reader.readline() runcase(line, http) # 写入结束时间 endTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) writer.write(1, 4, str(endTime)) writer.save_close() # 得到报告数据 res = Res() r = res.get_res('./lib/results/result-' + casename + '.xls') # 读取配置文件 config.get_config('./conf/conf.properties') logger.info(config.config) logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title', r['title']) # 替换模板中的数据 html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('status', r['status']) html = html.replace('casecount', r['casecount']) html = html.replace('starttime', r['starttime']) html = html.replace('endtime', r['endtime']) if r['status'] == 'Fail':
for sheet in sheetname: #设置当前读取的sheet页 read.set_sheets(sheet) #保持读写在同一个sheet页 writer.set_sheet(sheet) for i in range(read.rows): writer.row = i line = read.readline() runcase(line,http) writer.set_sheet(sheetname[0]) writer.write(1, 4, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) writer.save_close() #读取解析结果 res =Res() result = res.get_res('./lib/results/result-'+xslname +'.xls') logger.info(result) config.get_config('./conf/conf.properties') logger.info(config.config) #替换html模板中的数据 html = str(config.config['mailtxt']) html = html.replace('status',result['status']) if result['status'] == 'Fail': html = html.replace('#00d800','red') html = html.replace('title', result['title']) html = html.replace('runtype',result['runtype']) html = html.replace('passrate',result['passrate']) html = html.replace('starttime',result['starttime']) html = html.replace('casecount',result['casecount']) html = html.replace('endtime',result['endtime']) mail = Mail()
http = Browser(writer) for sheet in sheetnames: reader.set_sheet(sheet) # 保持读写在同一个sheet页面 writer.set_sheet(sheet) for i in range(reader.rows): writer.row = i line = reader.readline() runcase(line, http) writer.save_close() # 解析结果,得到报告数据 res = Res() r = res.get_res(result_path) logger.info(r) # 读取配置文件 config.get_config(conf_path) logger.info(config.config) # 修改邮件数据 html = config.config['mailtxt'] html = html.replace('title', r['title']) html = html.replace('status', r['status']) if r['status'] == "Fail": html = html.replace('#00d800', 'red') html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('casecount', r['casecount'])
# 设置当前读取的sheet页面 reader.set_sheet(sheet) # 设置写的sheet页,保持读和写是同一个sheet writer.set_sheet(sheet) for i in range(reader.rows): writer.row = i line = reader.readline() runcase(line) # 写入结束时间 endTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) writer.write(1,4,str(endTime)) writer.save_close() # 得到报告数据 res = Res() r = res.get_res('./lib/results/result-basic接口测试用例.xls') # 读取配置文件 config.get_config('./conf/conf.properties') logger.info(config.config) logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title',r['title']) # 替换模板中的数据 html = html.replace('runtype',r['runtype']) html = html.replace('passrate',r['passrate']) html = html.replace('status',r['status']) html = html.replace('casecount',r['casecount']) html = html.replace('starttime',r['starttime']) html = html.replace('endtime',r['endtime']) if r['status'] == 'Fail':
except Exception as e: # pass logger.exception(e) writer.set_sheet(sheetname[0]) # 获取结束时间 t = str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) # 设置shet页 # 写入结果文件 writer.write(1, 4, t) writer.save_close() res = Res() # result = res.get_res('./lib/result-HTTP接口用例.xls') result = res.get_res('./lib/result-' + casename) print(result) mail = Mail() mail.mail_info['mail_subject'] = result['title'] mailtxt = config.config['mailtxt'] mailtxt = mailtxt.replace('title', result['title']) mailtxt = mailtxt.replace('status', result['status']) mailtxt = mailtxt.replace('runtype', result['runtype']) mailtxt = mailtxt.replace('passrate', result['passrate']) mailtxt = mailtxt.replace('starttime', result['starttime']) mailtxt = mailtxt.replace('casecount', result['casecount']) mailtxt = mailtxt.replace('endtime', result['endtime']) if result['status'] == 'Fail': mailtxt = mailtxt.replace('#00d800', 'red')
# else: # # 如果是绝对路径,就使用绝对路径 # if casepath.find(':') >= 0: # # 获取用例文件名 # resultpath = path + '/lib/results/result-' + casepath[casepath.rfind('\\') + 1:] # else: # logger.error('非法用例路径') config.get_config(path + '/lib/conf/conf.txt') # logger.info(config.config) # 跑测试用例前将数据库还原 # mysql = Mysql() # mysql.init_mysql(path + '/lib/conf/userinfo.sql') runCases() res = Res() r = res.get_res(resultpath) text = config.config['mailtext'] # 根据执行结果的pass与fail对字体颜色进行设置 if r['status'] == 'Pass': text = text.replace('status', r['status']) else: text = text.replace( '<font style="font-weight: bold;font-size: 14px;color: #00d800;">status</font>', '<font style="font-weight: bold;font-size: 14px;color: red;">Fail</font>' ) text = text.replace('passrate', r['passrate'] + '%') text = text.replace('casecount', r['casecount']) text = text.replace('title', r['title']) text = text.replace('runtype', r['runtype']) text = text.replace('starttime', r['starttime'])
time.localtime(time.time())) writer.write(1, 3, str(start_time)) for sheet in sheetname: # 设置当前读取的sheet页面 reader.set_sheet(sheet) writer.set_sheet(sheet) for i in range(reader.rows): writer.row = i writer.clo = 7 line = reader.readline() runCases(line, runType) end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) writer.set_sheet(sheetname[0]) writer.write(1, 4, str(end_time)) writer.save_close() r = res.get_res(result + ctime + '.xls') print(r) config.get_config('./lib/conf/conf.properties') mysql = Mysql() mysql.init_mysql( 'C:\\Users\\leez\\Documents\\Navicat\\MySQL\\servers\\112\\test_project\\userinfo.sql' ) mail = Mail() mail.mail_info['mail_subject'] = r['title'] + '_' + ctime mail.mail_info['filepaths'] = [result + ctime + '.xls'] mail.mail_info['filenames'] = ['result-' + name + '用例' + ctime + '.xls'] config.config['mail_html'] = config.config['mail_html'].replace( 'title', r['title']) if r['status'] == 'Fail':
# unittest.main() # suite = unittest.TestSuite() # suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(baidu)) suite = unittest.defaultTestLoader.discover("./utest/", pattern="WebTest.py", top_level_dir=None) # 生成执行用例的对象 runner = bf(suite) runner.report(filename='./test.html', description='自动化测试报告') sheetname= datadriven.writer.get_sheets() datadriven.writer.set_sheets(sheetname[0]) datadriven.writer.write(1, 4, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) datadriven.writer.save_close() res = Res() result = res.get_res('./lib/results/result-HTTP接口用例.xls') logger.info(result) # logger.info(config.config) html = str(config.config['mailtxt']) # #替换html模板中的数据 html = html.replace('status', result['status']) if result['status'] == 'FAIL': html = html.replace('#00d800', 'red') else: pass print(result) html = html.replace('title', result['title']) html = html.replace('runtype', result['runtype']) html = html.replace('passrate', result['passrate']) html = html.replace('casecount', result['casecount'])
def run(casename="多线程APP1"): # 运行的相对路径 path = '.' # 用例路径 casepath = '' resultpath = '' logger.info("开始用例") try: casepath = sys.argv[1] except: casepath = '' casefile = casename # 为空,则使用默认的 if casepath == '': casepath = path + '/data/cases/' + casefile + '.xls' resultpath = path + '/outputs/resultcases/chint结果-' + casefile + '.xls' else: # 如果是绝对路径,就使用绝对路径 if casepath.find(':') >= 0: # 获取用例文件名 resultpath = path + '/outputs/resultcases/chint结果-' + casepath[ casepath.rfind('\\') + 1:] else: logger.error('非法用例路径') config.get_config(path + '/conf/conf.properties') # logger.info(config.config) # mysql = Mysql() # mysql.init_mysql(path + '/lib/userinfo.sql') logger.info("传递的参数是:%s,%s" % (casepath, resultpath)) datadriven.getparams(casepath, resultpath) # print(datadriven.alllist) ### 下面三行代码为BeautifulReport 报告 # suite = unittest.defaultTestLoader.discover("./utest/", pattern="WebTest.py", top_level_dir=None) # # 生成执行用例的对象 # runner = bf(suite) # runner.report(filename='./report/test'+ casefile + '.html', description=datadriven.title) ### 下面几行代码为HTMLTestRunner 报告 suite = unittest.defaultTestLoader.discover("./utest/", pattern="WebTest.py", top_level_dir=None) # 生成执行用例的对象 runner = bf(suite) runner.report(filename='./outputs/report/test' + casefile + '.html', description=datadriven.title) # 记录结束时间,写入结果文件 endtime = str( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # 用例结束时间 datadriven.writer.set_sheet((datadriven.writer.get_sheets())[0]) datadriven.writer.write(1, 4, endtime) # 对结果文件第二行第四列写入开始时间 # 保存结果文件 datadriven.writer.save_close() # # 得到报告数据 res = Res() r = res.get_res(resultpath) # 读取配置文件 config.get_config('./conf/conf.properties') # logger.info(config.config) # logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title', r['title']) # 替换模板中的数据 html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('status', r['status']) html = html.replace('casecount', r['casecount']) html = html.replace('starttime', r['starttime']) html = html.replace('endtime', r['endtime']) if r['status'] == 'Fail': html = html.replace('#00d800', 'red') # 根据结果决定是否发送邮件 if r['status'] == 'Fail': logger.info("开始发送邮件") mail = Mail() # mail.send(html) logger.info("结束用例")
# 设置当前读取的sheet页面 reader.set_sheet(sheet) # 设置写的sheet页,保持读和写是同一个sheet writer.set_sheet(sheet) for i in range(reader.rows): writer.row = i line = reader.readline() runcase(line,http) # 写入结束时间 endTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) writer.write(1,4,str(endTime)) writer.save_close() # 得到报告数据 res = Res() r = res.get_res('./lib/results/chintInter结果-'+ casename +'.xls') # 读取配置文件 config.get_config('./conf/conf.properties') logger.info(config.config) logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title',r['title']) # 替换模板中的数据 html = html.replace('runtype',r['runtype']) html = html.replace('passrate',r['passrate']) html = html.replace('status',r['status']) html = html.replace('casecount',r['casecount']) html = html.replace('starttime',r['starttime']) html = html.replace('endtime',r['endtime']) if r['status'] == 'Fail':
# 设置当前读取的sheet页面 reader.set_sheet(sheet) # 设置写的sheet页,保持读和写是同一个sheet writer.set_sheet(sheet) for i in range(reader.rows): writer.row = i line = reader.readline() runcase(line) # 写入结束时间 endTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) writer.write(1, 4, str(endTime)) writer.save_close() # 得到报告数据 res = Res() r = res.get_res(resultcase_path) # 读取配置文件 config.get_config('./conf/conf.properties') logger.info(config.config) logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title', r['title']) # 替换模板中的数据 html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('status', r['status']) html = html.replace('casecount', r['casecount']) html = html.replace('starttime', r['starttime']) html = html.replace('endtime', r['endtime']) if r['status'] == 'Fail':
def main(case_name="多线程APP1"): def runcase(line): # 分组信息不用执行 if len(line[0]) > 0 or len(line[1]) > 0: return # 反射获取关键字函数 func = getattr(http, line[3]) # 获取参数列表 args = inspect.getfullargspec(func).__str__() args = args[args.find('args=') + 5:args.rfind(', varargs')] args = eval(args) args.remove('self') # 根据参数调用 # print(len(args)) if len(args) == 0: func() return if len(args) == 1: func(line[4]) return if len(args) == 2: func(line[4], line[5]) return if len(args) == 3: func(line[4], line[5], line[6]) return print('warning:目前只支持三个关键字调用') case_name = case_name case_path = './data/cases/' + case_name + '.xls' resultcase_path = './outputs/resultcases/result-' + case_name + '.xls' reader = Reader() reader.open_excel(case_path) sheetname = reader.get_sheets() writer = Writer() writer.copy_open(case_path, resultcase_path) # 根据情况实例化app、web、接口对象 # http = HTTP(writer) http = APP(writer) # 测试开始时间 writer.set_sheet(sheetname[0]) startTime = str( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) writer.write(1, 3, str(startTime)) for sheet in sheetname: # 设置当前读取的sheet页面 reader.set_sheet(sheet) # 设置写的sheet页,保持读和写是同一个sheet writer.set_sheet(sheet) for i in range(reader.rows): writer.row = i line = reader.readline() runcase(line) # 写入结束时间 endTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) writer.write(1, 4, str(endTime)) writer.save_close() # 得到报告数据 res = Res() r = res.get_res(resultcase_path) # 读取配置文件 config.get_config('./conf/conf.properties') logger.info(config.config) logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title', r['title']) # 替换模板中的数据 html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('status', r['status']) html = html.replace('casecount', r['casecount']) html = html.replace('starttime', r['starttime']) html = html.replace('endtime', r['endtime']) if r['status'] == 'Fail': html = html.replace('#00d800', 'red')