在Python库中注册Robot Framework侦听器 [英] Register Robot Framework listener within Python library

查看:187
本文介绍了在Python库中注册Robot Framework侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Robot Framework的 Listener 功能非常适合添加可选的pre /post-processing可以在命令行上调用,例如pybot --listener myListener.py mySuite.robot.但是,我正在为Robot Framework创建一个Python库,并且我想自动注册其侦听器而无需在命令行上调用,以便在导入我的库时始终使用那些侦听器(我希望使用关键字和侦听器一起工作).有没有办法使用Python代码注册侦听器?

Robot Framework's Listener feature is great for adding optional pre/post-processing that can be invoked on the command line, e.g. pybot --listener myListener.py mySuite.robot. However, I am creating a Python library for Robot Framework, and I would like to automatically register its listeners without needing to be invoked on the command line, so that those listeners are always used when my library is imported (I want the keywords and listeners to work together). Is there a way to register a listener using Python code?

推荐答案

从机器人框架2.8.5开始,您可以将库注册为侦听器.请参阅《机器人框架用户指南》中的测试库作为侦听器 .原始功能请求在问题811

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

以下是一个简单的示例.它是一个提供单个关键字"require test case"的库.此关键字将另一个测试用例的名称作为参数.该库还是一个监听器,用于跟踪已运行哪些测试用例.关键字运行时,它会查看已运行的测试列表,如果所需的测试用例尚未运行或失败,则会失败.

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

这篇关于在Python库中注册Robot Framework侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆