Robot Framework測試框架中提供了多種用例執(zhí)行的方法,最常用的是Setup和Teardown户辞,今天嘗試該框架提供了Listener方法泌类,該方法提供了一種類似回調(diào)的機(jī)制,可以監(jiān)控測試執(zhí)行底燎,在執(zhí)行失敗時(shí)發(fā)送信息和調(diào)用其他系統(tǒng)刃榨。
Robot Framework框架提供了兩個(gè)版本的接口,2和3双仍,版本2支持JAVA和Python枢希,版本3只支持Python。同時(shí)版本3還支持修改測試數(shù)據(jù)朱沃,而版本2只能讀取苞轿。
接口版本由ROBOT_LISTENER_API_VERSION屬性定義茅诱。
本文主要介紹接口3的使用,同時(shí)會(huì)提供兩個(gè)使用示例搬卒,拋磚引玉之用瑟俭。
接口介紹
下表為第三版接口提供的方法,可以讀取和寫入測試數(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中的日志在不同接口中有一些限制尔当,下表給出詳細(xì)的介紹莲祸,重點(diǎn)關(guān)注是輸出到log文件還是控制臺(tái)蹂安。
方法 | 解釋 |
---|---|
start_keyword, end_keyword, log_message | 日志打印到日志log文件中的執(zhí)行關(guān)鍵字下面。 |
start_suite, end_suite, start_test, end_test | 日志打印到系統(tǒng)日志中锐帜,如果日志級(jí)別為Warnings田盈,日志將打印到日志log文件中的相應(yīng)位置中。 |
message | 日志打印到系統(tǒng)日志中缴阎,如果日志是在關(guān)鍵字中調(diào)用允瞧,日志將打印到日志log文件中的相應(yīng)位置中。 |
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í)行測試套
下邊的例子將在測試套中添加一個(gè)測試用例New Test并添加關(guān)鍵字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!'])
- 修改測試結(jié)果
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.'
上述接口實(shí)現(xiàn)了如果測試用例執(zhí)行時(shí)間大于10s后將用例結(jié)果設(shè)置為Fail畦韭。
使用Listener
- 不帶參數(shù)的調(diào)用
robot --listener lister.py test.robot
- 帶參數(shù)的調(diào)用
robot --listener lister.py:arg1:arg2 test.robot
應(yīng)用示例
- 保存執(zhí)行中的監(jiān)控日志
如果測試用例執(zhí)行失敗,我們可能需要保存用例執(zhí)行過程中的所有日志信息肛跌,如下代碼可以簡單實(shí)現(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
總結(jié)
Listener為我們提供了一個(gè)在用例執(zhí)行過程中和框架通訊的機(jī)制转唉,通過這個(gè)機(jī)制可以實(shí)現(xiàn)很多意想不到的功能。
Robot Framework自帶的編輯器在執(zhí)行器在執(zhí)行用例的時(shí)候就是通過Listener來實(shí)現(xiàn)輸出更多的信息的功能稳捆。