介紹Robot Framework中的Listener

Robot Framework測試框架中提供了多種用例執(zhí)行的方法,最常用的是Setup和Teardown,今天嘗試該框架提供了Listener方法,該方法提供了一種類似回調(diào)的機制,可以監(jiān)控測試執(zhí)行,在執(zhí)行失敗時發(fā)送信息和調(diào)用其他系統(tǒng)。

Robot Framework框架提供了兩個版本的接口,2和3,版本2支持JAVA和Python,版本3只支持Python。同時版本3還支持修改測試數(shù)據(jù),而版本2只能讀取。
接口版本由ROBOT_LISTENER_API_VERSION屬性定義。

本文主要介紹接口3的使用,同時會提供兩個使用示例,拋磚引玉之用。

接口介紹

下表為第三版接口提供的方法,可以讀取和寫入測試數(shù)據(jù)。

方法 參數(shù) 描述
start_suite data, result Called when a test suite starts. data and result are model objects representing the executed test suite and its execution results, respectively.
end_suite data, result Called when a test suite ends.
start_test data, result Called when a test case starts.data and result are model objects representing the executed test case and its execution results, respectively.
end_test data, result Called when a test case ends.Same arguments as with start_test.
log_message message Called when an executed keyword writes a log message. message is a model object representing the logged message.This method is not called if the message has level below the current threshold level.
message message Called when the framework itself writes a syslog message. message is same object as with log_message.
output_file path Called when writing to an output file is ready.path is an absolute path to the file.
log_file path Called when writing to a log file is ready.path is an absolute path to the file.
report_file path Called when writing to a report file is ready.path is an absolute path to the file.
xunit_file path Called when writing to an xunit file is ready.path is an absolute path to the file.
debug_file path Called when writing to a debug file is ready.path is an absolute path to the file.
close Called when the whole test execution ends.With library listeners called when the library goes out of scope.

接口中的日志

Listener中的日志在不同接口中有一些限制,下表給出詳細的介紹,重點關注是輸出到log文件還是控制臺。

方法 解釋
start_keyword, end_keyword, log_message 日志打印到日志log文件中的執(zhí)行關鍵字下面。
start_suite, end_suite, start_test, end_test 日志打印到系統(tǒng)日志中,如果日志級別為Warnings,日志將打印到日志log文件中的相應位置中。
message 日志打印到系統(tǒng)日志中,如果日志是在關鍵字中調(diào)用,日志將打印到日志log文件中的相應位置中。
Other methods 日志打印到系統(tǒng)日志中。

Listener的例子

獲取執(zhí)行信息

  • 使用函數(shù)
"""Listener that stops execution if a test fails."""

ROBOT_LISTENER_API_VERSION = 3

def end_test(data, result):
    if not result.passed:
        print('Test "{0}" failed: {1}'.format(result.name, result.message))
  • 使用類
import os.path
import tempfile


class PythonListener:
    ROBOT_LISTENER_API_VERSION = 3

    def end_test(self, name, attrs):
        if not result.passed:
            print('Test "{0}" failed: {1}'.format(result.name, result.message))

使用類要注意類名要和文件名一樣,否則不能調(diào)用成功。

修改執(zhí)行信息

  • 修改執(zhí)行測試套
    下邊的例子將在測試套中添加一個測試用例New Test并添加關鍵字Log。
ROBOT_LISTENER_API_VERSION = 3

def start_suite(suite, result):
    suite.tests.create(name='New test')

def start_test(test, result):
    test.keywords.create(name='Log', args=['Keyword added by listener!'])
  • 修改測試結果
class ResultModifier(object):
    ROBOT_LISTENER_API_VERSION = 3

    def __init__(self, max_seconds=10):
        self.max_milliseconds = float(max_seconds) * 1000

    def end_test(self, data, test):
        if test.status == 'PASS' and test.elapsedtime > self.max_milliseconds:
            test.status = 'FAIL'
            test.message = 'Test execution took too long.'

上述接口實現(xiàn)了如果測試用例執(zhí)行時間大于10s后將用例結果設置為Fail。

使用Listener

  • 不帶參數(shù)的調(diào)用
robot --listener lister.py test.robot 
  • 帶參數(shù)的調(diào)用
robot --listener lister.py:arg1:arg2 test.robot 

應用示例

  • 保存執(zhí)行中的監(jiān)控日志
    如果測試用例執(zhí)行失敗,我們可能需要保存用例執(zhí)行過程中的所有日志信息,如下代碼可以簡單實現(xiàn)此功能。
import os.path
import tempfile


class PythonListener:
    ROBOT_LISTENER_API_VERSION = 3

    def start_test(self, name, attrs):
        start_sys_log()

    def end_test(self, name, attrs):
        if attrs['status'] != 'PASS':
            save_sys_log()
  • 修改日志格式
    如果用例中打印的信息為圖片文件,可以使用Listener修改輸出格式。
class ResultModifier(object):
    ROBOT_LISTENER_API_VERSION = 3

    def log_message(self, msg):
        if '.png' in msg.message[:-5] and not msg.html:
            msg.message = '<a href = "{0}"><img src="{0}" width="800px"></a>'.format(msg.message)
            msg.html = True

總結

Listener為我們提供了一個在用例執(zhí)行過程中和框架通訊的機制,通過這個機制可以實現(xiàn)很多意想不到的功能。
Robot Framework自帶的編輯器在執(zhí)行器在執(zhí)行用例的時候就是通過Listener來實現(xiàn)輸出更多的信息的功能。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容