Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robotframework - get failing keyword / stack trace of failure

I have a keyword called "debug teardown" which prints the test status and then runs the debuglibrary Debug keyword, if the test has failed.

I would like to be able to log to console which keyword has caused the failure, so I can more effectively debug my test.

Is it possible to get the stack trace or most recent test keyword, and log it to the console?

Here is my Debug Teardown keyword:

Debug Teardown
    Run Keyword If Test Failed                  Log    ${TEST STATUS}: ${TEST MESSAGE}    ERROR
    Run Keyword If Test Failed                  Debug
like image 672
Daniel Paczuski Bak Avatar asked Sep 03 '25 04:09

Daniel Paczuski Bak


1 Answers

You can get a bit more information if you create a listener and also set the log level to DEBUG. Inside the listener you can save the results of log commands, and then when a keyword fails you can print it out or do whatever else you want.

For example, here's a listener that will print to stdout the last log message when a keyword fails:

from __future__ import print_function
class my_listener():
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self
        self.last_log = None

    def _log_message(self, message):
        self.last_log = message

    def _end_keyword(self, name, attrs):
        if attrs['status'] == 'FAIL':
            print("\n******\n", self.last_log['message'])

You would use it by importing the listener like a normal library, and also setting the log level to DEBUG (otherwise you'll get the error but no stack trace).

Example:

*** Settings ***
Library      my_listener
Suite Setup  set log level  DEBUG

*** Test cases ***
Example
    some keyword
like image 70
Bryan Oakley Avatar answered Sep 04 '25 23:09

Bryan Oakley