Elapsed time and result of a test in variables

不羁岁月 提交于 2019-12-25 16:25:33

问题


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:

  • Elapsed Time of current test
  • Result of the test

Is that possible?


回答1:


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.

Example

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.

ReporterLibrary.py

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'])

Example.robot

*** 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


来源:https://stackoverflow.com/questions/25754988/elapsed-time-and-result-of-a-test-in-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!