【问题标题】:Get the reason for failure and fetch the details from a keyword in RobotFramework获取失败原因并从 RobotFramework 中的关键字中获取详细信息
【发布时间】:2021-10-23 04:19:31
【问题描述】:

我正在使用 robot.api 中的 ExecutionResultResultVisitor 来获取导致失败的测试用例和关键字。但我不能:

  1. 打印失败的原因。例如,假设我遇到了这样的失败。我应该能够将原因打印为 在 Agent Desktop 上找不到呼叫

  2. 从失败的测试用例中的关键字获取详细信息。就我而言,我应该获取提供给关键字的参数并从该关键字返回输出,这是拆解的一部分。

*** Keywords ***

Teardown Keyword
    [Arguments]   ${arg1}  ${arg2}
    ${output}=  <Some execution here>
    [return]    ${output}

这里我应该获取${arg1}、${arg2}和${output}的值

这是我的示例代码供参考:

from robot.api import ExecutionResult, ResultVisitor
class Visitor(ResultVisitor):

    def __init__(self):
        self.failed = []

    def end_test(self, test):
        if test.status == "FAIL":
            self.failed.append(test)

result = ExecutionResult('output.xml')
result.visit(visitor)

【问题讨论】:

    标签: python robotframework


    【解决方案1】:

    了解visitor pattern 的工作原理以及如何终止访问不需要的分支。从API documentation,如果您不想进一步访问结果树,可以在任何节点级别显式返回False

    使用start_keywordend_keyword 等其他访问者步骤来访问每个关键字并查看哪个关键字失败。

    例如:

    def start_test(self, test):
        if test.passed:
            # Not interested in passed test cases. 
            # So we can stop further visiting by returning False.
            return False
        # start_keyword for keywords in this test case 
        # would be called only if this test case has failed
    
    
    def start_keyword(self, kw):
        # We are in a keyword of a failed test case
        if kw.passed:
             # Even though the test case might have failed, 
             # the keyword might have still passed. 
             # Since we are not interested in passed keyword, 
             # return False to stop visiting further.
             return False 
        # end_keyword for this keyword would only be called if this keyword has failed.
    
    def end_keyword(self, kw):
        # We are in a failed keyword
        # Add your required logic here.
        if kw.type == robot.model.keyword.Keyword.TEARDOWN_TYPE and kw.name == 'your keyword':
            print("keyword args:", kw.args) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-27
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多