首页 > 代码库 > Auty自动化测试框架第四篇——生成测试结果报告

Auty自动化测试框架第四篇——生成测试结果报告

[本文出自天外归云的博客园]

本次为Auty框架添加生成测试结果报告功能,文件结构更新:

技术分享

在Auty的lib文件夹中添加generate_result.py文件,用来生成测试结果报告:

# -*- coding: utf-8 -*-import osimport timeimport csvdef 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_selectionimport osimport timefrom .exe_deco import exe_decofrom .write_log import write_logfrom utils.utils import str_2_tuplefrom utils.utils import get_local_timefrom utils.utils import get_specific_timefrom generate_result import generate_resultdef 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_decodef execute_script(scriptPath):    write_log(execute_script: +scriptPath)    os.system(python +scriptPath)

这里引入了工具类utils,在Auty的utils文件夹下添加utils.py文件,内含一些常用方法以便调用:

# -*- coding: utf-8 -*-import timeimport datetimeimport osdef str_2_tuple(*str):    return strdef get_local_time():    return time.strftime(%Y-%m-%d %H:%M:%S,time.localtime(time.time()))def get_specific_time():    return datetime.datetime.now()

接下来需要修改我们脚本运行时所用到的装饰器,修改exe_deco.py文件,添加收集测试结果功能:

# -*- coding: utf-8 -*-import tracebackfrom .write_log import write_logfrom utils.utils import str_2_tupledef 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文件夹中可以看到我们生成的结果报告:

技术分享

结果报告格式如下:

技术分享

接下来我们要完善的就是框架的兼容性,添加自动化安装框架的支持类库功能,所需要的支持库根据工作范畴决定,如做web接口测试的话需要引入requests库,和oracle打交道要引入cx_Oracle库等等。

 
 

Auty自动化测试框架第四篇——生成测试结果报告