Auty自動化測試框架第三篇——添加異常處理與日志收集


[本文出自天外歸雲的博客園]

本次對框架進行完善,增加了日志收集功能和修飾運行功能,完善后的lib目錄如下:

在Auty的log文件夾中會存放一些腳本運行時生成的日志。在運行腳本時,對腳本的異常要有捕捉,並把捕捉到的信息打到日志中去。在lib文件夾中添加write_log.py文件,內容如下:

# -*- coding: utf-8 -*-
import os
import time
 
def write_log(log):
    filePath = os.path.abspath(os.path.dirname(__file__))
    logFilePath = os.path.join(os.path.dirname(filePath),'log','log.txt')
    print logFilePath
    execTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    open(logFilePath,'a').write(execTime+' '+log+'\n')

對於執行的腳本中的方法,添加異常處理與日志收集功能。這里用到裝飾器,在lib文件夾中添加exe_deco.py文件:

# -*- coding: utf-8 -*-
import traceback
from .write_log import write_log

def exe_deco(func):
    def _deco(*args, **kwargs):
        try:
            ret = func(*args, **kwargs)
        except Exception, e:
            log = 'Exception in '+func.__name__+' method: '+str(e)
            write_log(log)
        else:
            write_log('No exception in %s method.' % func.__name__)
        finally:
            return ret
    return _deco

對應的要修改之前寫的execute_selection.py文件,以便對執行的腳本應用新添加的功能(異常處理與日志收集):

# -*- coding: utf-8 -*-
from .read_selection import read_selection
import os
from .exe_deco import exe_deco
from .write_log import write_log

def execute_selection():
    selection = read_selection()
    for scriptPath in selection:
        execute_script(scriptPath)

@exe_deco
def execute_script(scriptPath):
    write_log('execute_script: '+scriptPath)
    os.system('python '+scriptPath)

至此,Auty框架的異常處理與日志收集功能也已經初具模型。運行框架根目錄下的start.py文件就可以看到log文件夾中生成了日志,記錄了對應腳本運行時的狀態。在Auty的logs文件中可以查看生產的日志文件,內容格式如下:

 

 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM