[英]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.