Register Robot Framework listener within Python library

戏子无情 提交于 2019-12-06 15:10:29

Starting with robot framework 2.8.5, you can register a library as a listener. See Test Libraries as Listeners in the robot framework user's guide. The original feature request is discussed in issue 811

The following is a simple example. It is a library that provides a single keyword, "require test case". This keyword takes as an argument the name of another test case. The library is also a listener which keeps track of which test cases have run. When the keyword runs, it looks at the list of already-run tests and will fail if the required test case has not run yet or has failed.

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"]

Example of using this in a test case:

*** 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

Looks like I haven't taken a good look at the docs recently enough. This exact feature is new in Robot Framework 2.8.5: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#test-libraries-as-listeners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!