在Python库中注册Robot框架侦听器

在Python库中注册Robot框架侦听器,python,listener,robotframework,Python,Listener,Robotframework,Robot框架的功能非常适合添加可在命令行上调用的可选预处理/后处理,例如pybot-listener myListener.py mySuite.Robot。但是,我正在为Robot Framework创建一个Python库,我希望在不需要在命令行上调用的情况下自动注册它的侦听器,以便在导入库时始终使用这些侦听器。我希望关键字和侦听器一起工作。有没有办法用Python代码注册一个监听器?看来我最近没有好好看一下这些文档。在Robot Framework 2.8.5中,这个确切的功能是新的:看起

Robot框架的功能非常适合添加可在命令行上调用的可选预处理/后处理,例如pybot-listener myListener.py mySuite.Robot。但是,我正在为Robot Framework创建一个Python库,我希望在不需要在命令行上调用的情况下自动注册它的侦听器,以便在导入库时始终使用这些侦听器。我希望关键字和侦听器一起工作。有没有办法用Python代码注册一个监听器?

看来我最近没有好好看一下这些文档。在Robot Framework 2.8.5中,这个确切的功能是新的:

看起来我最近没有仔细查看文档。Robot Framework 2.8.5中新增了这一功能:

从Robot Framework 2.8.5开始,您可以将库注册为侦听器。请参见《机器人框架用户指南》中的。中讨论了原始功能请求

下面是一个简单的例子。它是一个库,提供一个关键字require测试用例。此关键字将另一个测试用例的名称作为参数。库也是一个侦听器,它跟踪哪些测试用例已经运行。当关键字运行时,它查看已运行测试的列表,如果所需的测试用例尚未运行或已失败,它将失败

from robot.libraries.BuiltIn import BuiltIn

class DependencyLibrary(object):
    ROBOT_LISTENER_API_VERSION = 2
    ROBOT_LIBRARY_SCOPE = "GLOBAL"

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self
        self.test_status = {}

    def require_test_case(self, name):
        key = name.lower()
        if (key not in self.test_status):
            BuiltIn().fail("required test case can't be found: '%s'" % name)

        if (self.test_status[key] != "PASS"):
            BuiltIn().fail("required test case failed: '%s'" % name)

        return True

    def _end_test(self, name, attrs):
        self.test_status[name.lower()] = attrs["status"]
在测试用例中使用此选项的示例:

*** Settings ***
| Library | /path/to/DependencyLibrary.py

*** Test Cases ***
| Example of a failing test
| | fail | this test has failed

| Example of a dependent test
| | [Setup] | Require test case | Example of a failing test
| | log | hello, world

从robot framework 2.8.5开始,您可以将库注册为侦听器。请参见《机器人框架用户指南》中的。中讨论了原始功能请求

下面是一个简单的例子。它是一个库,提供一个关键字require测试用例。此关键字将另一个测试用例的名称作为参数。库也是一个侦听器,它跟踪哪些测试用例已经运行。当关键字运行时,它查看已运行测试的列表,如果所需的测试用例尚未运行或已失败,它将失败

from robot.libraries.BuiltIn import BuiltIn

class DependencyLibrary(object):
    ROBOT_LISTENER_API_VERSION = 2
    ROBOT_LIBRARY_SCOPE = "GLOBAL"

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self
        self.test_status = {}

    def require_test_case(self, name):
        key = name.lower()
        if (key not in self.test_status):
            BuiltIn().fail("required test case can't be found: '%s'" % name)

        if (self.test_status[key] != "PASS"):
            BuiltIn().fail("required test case failed: '%s'" % name)

        return True

    def _end_test(self, name, attrs):
        self.test_status[name.lower()] = attrs["status"]
在测试用例中使用此选项的示例:

*** Settings ***
| Library | /path/to/DependencyLibrary.py

*** Test Cases ***
| Example of a failing test
| | fail | this test has failed

| Example of a dependent test
| | [Setup] | Require test case | Example of a failing test
| | log | hello, world

嘿,如果我想把参数放到库中,如何在类中访问库中的参数呢。我无法使用sys.argv[2]…嘿,如果我想将参数放入库中,如何在类中访问库中的参数呢。我无法使用sys.argv[2]。。