Robot Framework提供了一个监听器(listener)接口可以用来接收测试执行过程中的通知.
监听器通过在命令行中设置选项 --listener
来启用,和导入测试库类似, 你也可以指定监听器文件的绝对或者相对路径. 该选项可以多次使用来启用多个监听器
从命令行还可以为监听器的类传递参数. 参数跟在监听器的名字(或路径)后面, 使用冒号(:
)隔开. 如果监听器使用的是Windowns格式的绝对路径, 磁盘驱动器后面的冒号不会被当作分隔.
可以使用分号(;
)作为分隔符. 这在监听器的参数本身含有冒号时非常有用, 不过在类UNIX操作系统中, 这需要使用双引号把整个包起来:
存在两种监听器接口版本,监听器实现时必须设置属性 ROBOT_LISTENER_API_VERSION
为2或3,3只支持Python
监听器版本2
所有测试执行进度相关的方法的签名都是 method(name, attributes)
, 其中 attributes
是一个字典, 其中包含了事件的详细信息,不能直接修改这两个参数
运行案例
*** Settings *** Library DateTime Documentation 监听器测试 Metadata tester zhangsan Resource ${EXECDIR}/resource/functest.resource Variables ${EXECDIR}/variables/test.py Suite Setup start Suite Teardown stop *** Test Cases *** case1 [Setup] startcase Should Be Equal 1 1 log 通过 case2 Should Be Equal 1 2 log 不通过 *** Keywords *** start log 开始 stop log 结束 startcase log 用例开始
运行命令robot --listener ./listener/listener2.py ./funcsuite,查看每个api的参数是什么
监听器api:library_import BuiltIn args:[] originalname:BuiltIn importer:None source:/Library/Python/3.8/site-packages/robot/libraries/BuiltIn.py ============================================================================== Funcsuite :: 套件A ============================================================================== 监听器api:start_suite Funcsuite id:s1 longname:Funcsuite doc:套件A metadata:{'tester': 'zhangsan'} starttime:20221021 17:12:42.435 tests:[] suites:['Functest'] totaltests:2 source:/Users/mac/PycharmProjects/rfs/funcsuite
监听器api:library_import BuiltIn args:[] originalname:BuiltIn importer:None source:/Library/Python/3.8/site-packages/robot/libraries/BuiltIn.py
监听器api:library_import DateTime args:[] originalname:DateTime importer:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot source:/Library/Python/3.8/site-packages/robot/libraries/DateTime.py
监听器api:resource_import functest importer:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot source:/Users/mac/PycharmProjects/rfs/resource/functest.resource
监听器api:variables_import test.py args:[] importer:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot source:/Users/mac/PycharmProjects/rfs/variables/test.py
Funcsuite.Functest :: 监听器测试 ==============================================================================
监听器api:start_suite Functest id:s1-s1 longname:Funcsuite.Functest doc:监听器测试 metadata:{'tester': 'zhangsan'} starttime:20221021 17:12:42.460 tests:['case1', 'case2'] suites:[] totaltests:2 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot
监听器api:start_keyword start doc: assign:[] tags:[] lineno:7 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:SETUP status:NOT SET starttime:20221021 17:12:42.468 kwname:start libname: args:[]
监听器api:start_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:22 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT SET starttime:20221021 17:12:42.469 kwname:Log libname:BuiltIn args:['开始']
监听器api:log_message timestamp:20221021 17:12:42.470 message:监听器api:log_message timestamp:20221021 17:12:42.470 message:开始 level:INFO html:no level:INFO html:no
监听器api:end_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:22 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:PASS starttime:20221021 17:12:42.469 endtime:20221021 17:12:42.470 elapsedtime:1 kwname:Log libname:BuiltIn args:['开始']
监听器api:end_keyword start doc: assign:[] tags:[] lineno:7 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:SETUP status:PASS starttime:20221021 17:12:42.468 endtime:20221021 17:12:42.470 elapsedtime:2 kwname:start libname: args:[]
case1
监听器api:start_test case1 id:s1-s1-t1 longname:Funcsuite.Functest.case1 doc: tags:[] lineno:11 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot starttime:20221021 17:12:42.471 template: originalname:case1
监听器api:start_keyword startcase doc: assign:[] tags:[] lineno:12 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:SETUP status:NOT SET starttime:20221021 17:12:42.471 kwname:startcase libname: args:[]
监听器api:start_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:28 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT SET starttime:20221021 17:12:42.472 kwname:Log libname:BuiltIn args:['用例开始']
监听器api:log_message timestamp:20221021 17:12:42.472 message:监听器api:log_message timestamp:20221021 17:12:42.472 message:用例开始 level:INFO html:no level:INFO html:no
监听器api:end_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:28 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:PASS starttime:20221021 17:12:42.472 endtime:20221021 17:12:42.472 elapsedtime:0 kwname:Log libname:BuiltIn args:['用例开始']
监听器api:end_keyword startcase doc: assign:[] tags:[] lineno:12 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:SETUP status:PASS starttime:20221021 17:12:42.471 endtime:20221021 17:12:42.472 elapsedtime:1 kwname:startcase libname: args:[] .
监听器api:start_keyword BuiltIn.Should Be Equal doc:Fails if the given objects are unequal. assign:[] tags:[] lineno:13 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT SET starttime:20221021 17:12:42.472 kwname:Should Be Equal libname:BuiltIn args:['1', '1']
监听器api:end_keyword BuiltIn.Should Be Equal doc:Fails if the given objects are unequal. assign:[] tags:[] lineno:13 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:PASS starttime:20221021 17:12:42.472 endtime:20221021 17:12:42.473 elapsedtime:1 kwname:Should Be Equal libname:BuiltIn args:['1', '1'] .
监听器api:start_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:14 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT SET starttime:20221021 17:12:42.473 kwname:Log libname:BuiltIn args:['通过']
监听器api:log_message timestamp:20221021 17:12:42.473 message:监听器api:log_message timestamp:20221021 17:12:42.473 message:通过 level:INFO html:no level:INFO html:no
监听器api:end_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:14 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:PASS starttime:20221021 17:12:42.473 endtime:20221021 17:12:42.473 elapsedtime:0 kwname:Log libname:BuiltIn args:['通过'] .
监听器api:end_test case1 id:s1-s1-t1 longname:Funcsuite.Functest.case1 doc: tags:[] lineno:11 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot starttime:20221021 17:12:42.471 endtime:20221021 17:12:42.473 elapsedtime:2 status:PASS message: template: originalname:case1
case1 | PASS | ------------------------------------------------------------------------------
case2
监听器api:start_test case2 id:s1-s1-t2 longname:Funcsuite.Functest.case2 doc: tags:[] lineno:16 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot starttime:20221021 17:12:42.474 template: originalname:case2
监听器api:start_keyword BuiltIn.Should Be Equal doc:Fails if the given objects are unequal. assign:[] tags:[] lineno:17 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT SET starttime:20221021 17:12:42.475 kwname:Should Be Equal libname:BuiltIn args:['1', '2']
监听器api:log_message timestamp:20221021 17:12:42.475 message:1 != 2 level:FAIL html:no
监听器api:end_keyword BuiltIn.Should Be Equal doc:Fails if the given objects are unequal. assign:[] tags:[] lineno:17 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:FAIL starttime:20221021 17:12:42.475 endtime:20221021 17:12:42.475 elapsedtime:0 kwname:Should Be Equal libname:BuiltIn args:['1', '2']
F
监听器api:start_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:18 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT RUN starttime:20221021 17:12:42.476 kwname:Log libname:BuiltIn args:['不通过']
监听器api:end_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:18 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT RUN starttime:20221021 17:12:42.476 endtime:20221021 17:12:42.476 elapsedtime:0 kwname:Log libname:BuiltIn args:['不通过'] .
监听器api:end_test case2 id:s1-s1-t2 longname:Funcsuite.Functest.case2 doc: tags:[] lineno:16 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot starttime:20221021 17:12:42.474 endtime:20221021 17:12:42.477 elapsedtime:3 status:FAIL message:1 != 2 template: originalname:case2
case2 | FAIL | 1 != 2 ------------------------------------------------------------------------------
监听器api:start_keyword stop doc: assign:[] tags:[] lineno:8 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:TEARDOWN status:NOT SET starttime:20221021 17:12:42.478 kwname:stop libname: args:[]
监听器api:start_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:25 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:NOT SET starttime:20221021 17:12:42.478 kwname:Log libname:BuiltIn args:['结束']
监听器api:log_message timestamp:20221021 17:12:42.478 message:监听器api:log_message timestamp:20221021 17:12:42.478 message:结束 level:INFO html:no level:INFO html:no
监听器api:end_keyword BuiltIn.Log doc:Logs the given message with the given level. assign:[] tags:[] lineno:25 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:KEYWORD status:PASS starttime:20221021 17:12:42.478 endtime:20221021 17:12:42.479 elapsedtime:1 kwname:Log libname:BuiltIn args:['结束']
监听器api:end_keyword stop doc: assign:[] tags:[] lineno:8 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot type:TEARDOWN status:PASS starttime:20221021 17:12:42.478 endtime:20221021 17:12:42.479 elapsedtime:1 kwname:stop libname: args:[]
监听器api:end_suite Functest id:s1-s1 longname:Funcsuite.Functest doc:监听器测试 metadata:{'tester': 'zhangsan'} starttime:20221021 17:12:42.460 endtime:20221021 17:12:42.479 elapsedtime:19 status:FAIL message: tests:['case1', 'case2'] suites:[] totaltests:2 source:/Users/mac/PycharmProjects/rfs/funcsuite/functest.robot statistics:2 tests, 1 passed, 1 failed
Funcsuite.Functest :: 监听器测试 | FAIL | 2 tests, 1 passed, 1 failed ==============================================================================
监听器api:end_suite Funcsuite id:s1 longname:Funcsuite doc:套件A metadata:{'tester': 'zhangsan'} starttime:20221021 17:12:42.435 endtime:20221021 17:12:42.480 elapsedtime:45 status:FAIL message: tests:[] suites:['Functest'] totaltests:2 source:/Users/mac/PycharmProjects/rfs/funcsuite statistics:2 tests, 1 passed, 1 failed
Funcsuite :: 套件A | FAIL | 2 tests, 1 passed, 1 failed ==============================================================================
监听器版本3大多数方法和 监听器版本2 一样, 但是既可以从这些对象中查询所需信息, 也可以直接做出修改.
改变正在执行的内容需要修改作为第一参数data,下面的例子中的suite即代表data,展示了在每个执行的测试套件中新加入一个测试, 以及在这个测试中新加入一个关键字.
============================================================================== Funcsuite :: 套件A ============================================================================== Funcsuite.Functest :: 监听器测试 ============================================================================== case1 | PASS | ------------------------------------------------------------------------------ case2 | FAIL | 1 != 2 ------------------------------------------------------------------------------ New test 监听器加的关键字 New test | PASS | ------------------------------------------------------------------------------ Funcsuite.Functest :: 监听器测试 | FAIL | 3 tests, 2 passed, 1 failed ============================================================================== New test 监听器加的关键字 New test | PASS | ------------------------------------------------------------------------------ Funcsuite :: 套件A | FAIL | 4 tests, 3 passed, 1 failed ==============================================================================
试图在 end_suite
或 end_test
方法中修改执行是无效的, 原因很简单, 因为测试套件或用例已经执行完成了
============================================================================== Funcsuite :: 套件A ============================================================================== Funcsuite.Functest :: 监听器测试 ============================================================================== case1 | PASS | ------------------------------------------------------------------------------ case2 | FAIL | 1 != 2 ------------------------------------------------------------------------------ Funcsuite.Functest :: 监听器测试 | FAIL | 2 tests, 1 passed, 1 failed ============================================================================== Funcsuite :: 套件A | FAIL | 2 tests, 1 passed, 1 failed ==============================================================================
试图在 start_suite
或 start_test
方法中修改测试的名称, 文档或者其它类似的元数据也是无效的, 因为这些信息对应的对象已经创建过了. 只有变更测试套件的用例或子测试集或者关键字才有效.
测试发现名字没有变化
改变测试执行的结果需要修改作为第二个参数result(下面用test代替)
*** Test Cases *** smokecase1 [Tags] smoke Log 冒烟1通过 smokecase2 [Tags] smoke Log 冒烟2通过 case1 [Setup] startcase Log 用例通过 case2 Log message WARN *** Keywords *** startcase FOR ${i} IN RANGE 5 Log 第${i}秒 Sleep 1s END
end监听器无法修改当前测试套件或用例的名称, 因为它们在监听器被调用时已经被写入到 输出文件(Output.xml) 中了,下面的设置是没有效果的
def end_test(self, data, test): test.name = "修改测试名" def end_suite(self, data, test): test.name = "修改套件名"
可以使用 ROBOT_LIBRARY_LISTENER
属性来注册一个监听器(不需要命令行使用--listner,只需要setting中引入库),假设listener3.py是一个测试库,可以像上面一样写监听器,但要,如果是测试库同时充当监听器, 为了避免监听器的方法被暴露为关键字, 可以使用下划线作前缀
Library ${EXECDIR}/listener/listener3.py
监听器2可以使用Robot Framework提供的Logger,start_keyword, end_keyword, log_message这三个api的logger会在测试报告中打印,其它在syslog中不在测试日志中打印
监听器3所有api都不会打印
标签:12,17,robotframework,listener,api,20221021,监听器,message From: https://www.cnblogs.com/zerotest/p/16814237.html