[本文出自天外歸雲的博客園]
本次為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庫等等。