Get the reason for failure and fetch the details from a keyword in RobotFramework

Viewed 162

I'm using ExecutionResult and ResultVisitor from robot.api to fetch the Testcase and keyword which causes the failure. But I'm not able to:

  1. Print the reason for the failure. E.g., Suppose I've a failure something like this. I should be able to print the reason as Call not Found on Agent Desktop

    enter image description here

  2. Fetch the details from a keyword in the failed testcase. In my case, I should fetch the Arguments provided to a keyword and returned output from that keyword, which is part of the teardown.

*** Keywords ***

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

Here I should fetch the values of ${arg1}, ${arg2} and ${output}

This is my sample code for reference:

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)

1 Answers

Understand how the visitor pattern works and how to terminate visiting unwanted branches. From the API documentation, you can explicitly return False at any node level if you do not want to visit further down the result tree.

Use other visitor steps like start_keyword and end_keyword to visit each keyword and see which keyword failed.

Eg:

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) 
Related