Auty自动化测试框架第四篇——生成测试结果报告
[本文出自天外归云的博客园]
本次为Auty框架添加生成测试结果报告功能,文件结构更新:
在Auty的lib文件夹中添加generate_result.py文件,用来生成测试结果报告:
# -*- coding: utf-8 -*- import os import time import csv def generate_result(resultFileName,result): filePath = os.path.abspath(os.path.dirname(__file__)) resultFilePath = os.path.join(os.path.dirname(filePath),'results',resultFileName) print resultFilePath csvFile = file(resultFilePath,'a+') writer = csv.writer(csvFile) data = [result] writer.writerows(data) csvFile.close()
将生成测试结果报告功能整合进Auty框架,修改execute_selection.py文件,添加收集测试结果功能:
# -*- coding: utf-8 -*- from .read_selection import read_selection import os import time from .exe_deco import exe_deco from .write_log import write_log from utils.utils import str_2_tuple from utils.utils import get_local_time from utils.utils import get_specific_time from generate_result import generate_result def execute_selection(): selection = read_selection() genTime = get_local_time() resultFileName = genTime+' test_result.csv' autyPath = os.getcwd() resultFilePath = os.path.join(autyPath,'results',resultFileName) generate_result(resultFilePath,('scriptPath','detail','startTime','endTime','duration')) for scriptPath in selection: result = str_2_tuple(scriptPath) startTime = get_specific_time() ret,result2 = execute_script(scriptPath) endTime = get_specific_time() duration = (endTime-startTime).microseconds*0.000001 result = result+result2+str_2_tuple(startTime)+str_2_tuple(endTime)+str_2_tuple(duration) generate_result(resultFilePath,result) @exe_deco def execute_script(scriptPath): write_log('execute_script: '+scriptPath) os.system('python '+scriptPath)
这里引入了工具类utils,在Auty的utils文件夹下添加utils.py文件,内含一些常用方法以便调用:
# -*- coding: utf-8 -*- import time import datetime import os def str_2_tuple(*str): return str def get_local_time(): return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) def get_specific_time(): return datetime.datetime.now() def tylan_assert(actual,expected): if actual != expected: result = 'failed' print result else: result = 'passed' print result return result def tylan_assert_include(actual,expected): if expected not in actual: result = 'failed' print result else: result = 'passed' print result return result
接下来需要修改我们脚本运行时所用到的装饰器,修改exe_deco.py文件,添加收集测试结果功能:
# -*- coding: utf-8 -*- import traceback from .write_log import write_log from utils.utils import str_2_tuple def exe_deco(func): def _deco(*args, **kwargs): result = () try: ret = func(*args, **kwargs) except Exception, e: log = 'Exception in '+func.__name__+' method: '+str(e) write_log(log) result = result+str_2_tuple(log) else: log = 'No exception in '+func.__name__+' method.' write_log(log) result = result+str_2_tuple(log) finally: return ret,result return _deco
至此我们的框架就已经具备了生成测试结果报告的功能,在Auty根目录下运行start.py文件,对应在results文件夹中可以看到我们生成的结果报告:
结果报告格式如下:
在此基础上,进一步优化生成的显示结果,在lib下添加“generate_html.py”文件,添加内容:
# -*- coding: utf-8 -*- import csv,os def write_csv_to_html(csv_path,save_path): print os.getcwd() csvfile = file(csv_path, 'rb') reader = csv.reader(csvfile) html = open(save_path, 'w') html.write(""" <html> <head> <title>Auty test result</title> <h1>Auty test result</h1> <h3>Result file path:"""+csv_path+"""</h3> <style>img{float:left;margin:5px;}</style> </head> <body"> <table border="1"> """) for line in reader: sig = True for each in line: if(('failed' in each) or ('Exception in' in each)): sig = False if sig == False: html.write('<tr bgcolor="#8B2323">') else: html.write('<tr bgcolor="#00FF7F">') for each in line: html.write('<td>'+each+'</td>') html.write('</tr>') html.write(""" </table> </body> """)
以上添加了一个write_to_csv方法,将生成的csv文件转换为html的形式进行显示,如果行里包含了“failed”字样或者“Exception in”字样,则将该行显示为红色,否则显示为绿色。对应的修改“generate_result.py”文件,在生成csv文件后调用生成html文件的方法:
# -*- coding: utf-8 -*- import os import time import csv from .generate_html import write_csv_to_html def generate_result(resultFileName,result): filePath = os.path.abspath(os.path.dirname(__file__)) resultFilePath = os.path.join(os.path.dirname(filePath),'results',resultFileName+'.csv') #print resultFilePath csvFile = file(resultFilePath,'a+') writer = csv.writer(csvFile) data = [result] writer.writerows(data) csvFile.close() html_result_path = os.path.join(os.path.dirname(filePath),'results',resultFileName+'.html') write_csv_to_html(resultFilePath,html_result_path)
至此对生成的测试结果做了进一步完善。接下来我们要为Auty添加支持类库,所需要的支持库根据工作范畴决定,如做web接口测试的话需要引入requests库,和oracle打交道要引入cx_Oracle库等等。
本文来自博客园,作者:天外归云,转载请注明原文链接:https://www.cnblogs.com/LanTianYou/p/5920699.html