定义一个关键字仅在机器人框架失败时调用

Define a keyword to call only on failure in robot framework

我们有什么方法可以在 setting 或机器人代码的任何其他部分定义当我们遇到特定类型的故障时立即调用关键字?

我们有 Test setupTest Teardown,这将 运行 在开始时作为测试设置或在测试用例结束时作为测试拆解,类似的有什么办法, 我们可以根据 failure

来定义和调用关键字

使用Teardown的问题是当我们有5行机器人代码(关键字)时,如果失败发生在第二行,它会跳过第三、四、五行,直接运行s拆解这是我在使用拆解 时遇到的问题。如果我们在第二行失败,它将调用定义的关键字,该关键字需要是 运行 然后返回 运行 第三,第四和第五行(不跳过)。

具有listener library you can implement such functionality. Create a keyword that will set the desired keyword to be executed on failure. Implement the end_keyword listener功能来检测何时发生关键字错误。

最后使用BuiltInrun_keyword函数执行配置的关键字。

示例:

from robot.api import logger
from robot.libraries.BuiltIn import BuiltIn

class RunOnFailureLibrary(object):
    ROBOT_LISTENER_API_VERSION = 2
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LIBRARY_VERSION = 0.1

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self
        self.keyword_to_run_on_faiure = None
        self.keyword_args = None

    def set_keyword_to_run_on_failure(self, keyword, *args):
        self.keyword_to_run_on_faiure = keyword
        if len(args) > 0:
            self.keyword_args = args
        
    def _end_keyword(self, name, attributes):
        if attributes['status'] == 'FAIL':
            logger.info(f"Running keyword:'{self.keyword_to_run_on_faiure}' on failure!")
            if self.keyword_args is None:
                BuiltIn().run_keyword(self.keyword_to_run_on_faiure)
            else:
                BuiltIn().run_keyword(self.keyword_to_run_on_faiure, *self.keyword_args)
        

globals()[__name__] = RunOnFailureLibrary

用法:

*** Settings ***
Library    RunOnFailureLibrary
Suite Setup    Set Keyword To Run On Failure    Log Many    1    2   3

*** Test Cases ***
Test
    Log    1
    Log    2
    Fail   0
    Log    3
    Log    4

结果(运行 和 robot --pythonpath . test.robot):

正如我在评论中所说,测试用例的其余部分仍然不会执行。为此,您必须像其他人建议的那样忽略失败。