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, 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 casetype == 'WEB': 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'])
obj.row = i 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])
try: runCases(http, line) 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'])
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'])
p.remove('self') if len(p) == 0: func() elif len(p) == 1: func(line[4]) elif len(p) == 2: func(line[4], line[5]) elif len(p) == 3: func(line[4], line[5], line[6]) else: print("waring:只支持三个参数!") if __name__ == "__main__": res = Res() reader = Reader() writer = Writer() key = 'APP' if key == 'HTTP': runType = HTTP(writer) name = 'HTTP接口' elif key == 'SOAP': runType = SOAP(writer) name = 'SOAP接口' elif key == 'REST': runType = HTTP(writer) name = 'REST接口' elif key == 'WEB': runType = WEB(writer) name = 'WEB'
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('./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'])
datadriven.getparams(casepath,resultpath) # 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'])
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("结束用例")
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,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'])
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'])
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')