简体   繁体   English

使用Robot Framework中的内置函数在自定义关键字中预期出现错误

[英]Expect Error in a custom keyword using a built-in function in Robot Framework

I have a following example keyword in my custom Robot Framework library that uses Robot Framework's BuiltIn library to call another keyword inside the test sequence using parameters: 我的自定义Robot Framework库中有一个示例关键字,该关键字使用Robot Framework的BuiltIn库使用参数在测试序列中调用另一个关键字:

# MyLibrary.py
from robot.libraries.BuiltIn import BuiltIn

class MyLibrary(object):
    def run_a_keyword(self, keywordName):
        builtinLib = BuiltIn().get_library_instance("BuiltIn")
        parameters = ['hi', 'second param']
        # Runs the keyword with the given parameters
        builtinLib.run_keyword(keywordName, *parameters)

I would be running for example a following simplified test to check that the keyword works property by testing the errors as well: 例如,我将运行以下简化测试,以通过测试错误来检查关键字是否有效:

*** Settings ***
Library MyLibrary.py

*** Test Cases ***
Test Case
    Run A Keyword   My Keyword
    Run Keyword And Expect Error    Keyword 'Another Keyword' expected 3 arguments, got 2.  Run A Keyword   Another Keyword

*** Keywords ***
My Keyword
    [Arguments] ${arg1} ${arg2}
    Log To Console  Keyword Executed 

Another Keyword
    [Arguments] ${arg1} ${arg2} ${arg3}
    Log To Console  Keyword Executed

I would expect that this test case would pass, but the test case fails in the second step with Run Keyword and Expect Error despite I had stated that the error was expected? 我希望该测试用例能够通过,但是尽管我已经指出了Run Keyword and Expect Error ,但是该测试用例在第二步中却由于Run Keyword and Expect Error失败了?

My workaround for this was to catch the exception thrown by builtInLib's call inside my keyword and re-throw it, after this the test sequence with Run Keyword And Expect Error works properly: 我的解决方法是在关键字中捕获buildInLib调用引发的异常,然后将其重新抛出,此后,带有Run Keyword And Expect Error的测试序列可以正常工作:

def run_a_keyword(self, keywordName):
        builtinLib = BuiltIn().get_library_instance("BuiltIn")
        parameters = ['hi', 'second param']
        try:
            builtinLib.run_keyword(keywordName, *parameters)
        except Exception as err:
            raise Exception(err.message)

However, I need a listener to pass the errors to a service: 但是,我需要一个侦听器来将错误传递给服务:

class MyListener(object):
    ROBOT_LISTENER_API_VERSION = 2
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self

    def log_message(self, message):
        level = message['level']
        if level == 'FAIL':
            send_the_error_to_somewhere(message['message'])

The log_message is called twice (when the built-in function is called and when I raise the new exception). log_message调用了两次(调用内置函数时,以及引发新异常时)。 This causes that the same error would be handled and logged twice which is not what I want. 这将导致相同的错误将被处理并记录两次,这不是我想要的。

So: How can I Run Keyword And Expect Error with a keyword that calls a built in function and still handle the error only once? 因此:如何使用调用内置函数的关键字Run Keyword And Expect Error ,并且仍然仅处理一次错误?

First and foremost, the Run Keyword And Expect Error does not handle syntax errors - see its documentation, it clearly states it at the end, and also its implementation - this error is in the dont_continue category. 首先,“ Run Keyword And Expect Error ”不处理语法错误-请参阅其文档,在末尾清楚地说明该错误以及其实现 -此错误在dont_continue类别中。
You are passing 2 arguments to a keyword that has 3 required parameters, which is clearly a syntax error. 您正在将2个参数传递给具有3个必需参数的关键字,这显然是语法错误。


Why is log_message called/storing the error twice? 为什么log_message调用/存储错误两次? Because two exceptions are raised - one in the RF's builtin keyword, and then when you reraise it in your custom one. 因为引发了两个异常-一个在RF的内置关键字中,然后在您自定义中重新引发时。 The framework logs on fatal level in every exception, thus you get the 2. 框架在每个异常中都记录致命级别,因此您得到了2。

The first solution that comes to my mind is to communicate with your log handler through the message content itself. 我想到的第一个解决方案是通过消息内容本身与您的日志处理程序进行通信。
The most optimal solutuon world have been modifying the exception, say adding an attribute 'producer' with a set value to it, and checking in the handler hasattr(err, 'producer') , but it does not have access to the exception anymore, just to the message text. 最佳的解决方案领域是修改异常,比如说给它添加一个具有设置值的属性“ producer”,并签入处理程序hasattr(err, 'producer') ,但是它不再可以访问该异常,仅用于消息文本。 Prefix the error message with a special identifier: 在错误消息前添加特殊标识符:

except Exception as err:
            raise Exception("SPECIAL_IDENTIFIER:{}".format(err.message))

, and then send_the_error_to_somewhere only if it has it: ,然后仅在具有send_the_error_to_somewhere的情况下:

if level == 'FAIL' and message['message'].startswith('SPECIAL_IDENTIFIER:'):
            # get rid of the special identifier prefix, and then
            send_the_error_to_somewhere(message['message'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 机器人框架运行关键字和期望错误 - Robot Framework Run Keyword and Expect Error Robot Framework内置`Convert To Bytes`关键字只支持Big Endian - Robot Framework Built-In `Convert To Bytes` keyword only supports Big Endian Robot Framework-从自定义关键字返回值 - Robot Framework - return a value from custom keyword Robot Framework自定义关键字仅在测试设置中 - Robot Framework custom keyword only in Test Setup 使用动态库的robot框架自定义关键字中run_keyword方法的实现问题 - implementation issue in the run_keyword method in robot framework custom keyword using dynamic library 在机器人框架中找不到名称为“foo”的关键字错误 - No keyword with name 'foo' found error in robot framework 内置关键字类型是指python中的函数还是类? - The built-in keyword type means a function or a class in python? 如何使用内置 function 的 `type()` 传递 class 关键字 arguments? - How to pass class keyword arguments with the `type()` built-in function? 应用 | 机器人框架 | 无法使用自定义定位器策略运行关键字来查找元素 - Appium | Robot Framework | Unable to run a keyword to find an element using a custom locator strategy 使用 Robot Framework 自定义关键字从 Python 脚本返回列表值 - Return list values from Python Script using Robot Framework custom Keyword
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM