Reputation: 2505
Is it possible to determine the elapsed time and the result of the current test in a variable?
I'd like some tests to log their results external systems, and so ideally in [Teardown] I'd like to know:
Is that possible?
Upvotes: 1
Views: 6240
Reputation: 386210
The listener interface provides an elapsed time in milliseconds, at the end of execution of each keyword, test, and suite. See the section titled Taking listeners into use in the user guide.
Starting with robot framework 2.8.5 you can implement listeners in a library, meaning you can build this functionality into a test suite, rather than depending on command line options to set up the listener. See the section titled Test libraries as listeners in the user guide.
The following example shows how you might implement the listener interface. The library prints the name, status and elapsed time, but you could just as easily call a web service, insert the data into a database, or write the data to a file.
class ReporterLibrary(object):
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
ROBOT_LISTENER_API_VERSION = 2
def __init__(self):
self.ROBOT_LIBRARY_LISTENER = self
def _end_test(self, name, attrs):
print "%s => status: %s, elapsed time: %s ms" % (name, attrs['status'], attrs['elapsedtime'])
*** Settings ***
| Library | ReporterLibrary.py
*** Test Cases ***
| Example of a passing test
| | sleep | 200 milliseconds
| | Pass execution | test passed
| Example of a failing test
| | sleep | 500 milliseconds
| | Fail | test failed
Upvotes: 3