码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-29 20:41:39      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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

本次为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()

接下来需要修改我们脚本运行时所用到的装饰器,修改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文件夹中可以看到我们生成的结果报告:

技术分享

结果报告格式如下:

技术分享

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

 
 

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

标签:

原文地址:http://www.cnblogs.com/LanTianYou/p/5920699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!