I have a robot framework test suite that calls a python method. I would like that python method to return a message to the console without failing the test. Specifically I am trying to time a process.
I can use "raise" to return a message to the console, but that simultaneously fails the test.
def doSomething(self, testCFG={}):
'''
Do a process and time it.
'''
testCFG['operation'] = 'doSomething'
startTime = time.time()
response=self.Engine(testCFG)
endTime = time.time()
duration = int(round(endTime-startTime))
raise "doSomething took", duration//60 , "minutes and", duration%60, "seconds."
errmsg = 'doSomething failed'
if testCFG['code']: raise Exception(errmsg)
Or I can use "print" to return a message to the log file and report without failing the test, but that information is only available in the report, not the console.
def doSomething(self, testCFG={}):
'''
Do a process and time it.
'''
testCFG['operation'] = 'doSomething'
startTime = time.time()
response=self.Engine(testCFG)
endTime = time.time()
duration = int(round(endTime-startTime))
print "doSomething took", duration//60 , "minutes and", duration%60, "seconds."
errmsg = 'doSomething failed'
if testCFG['code']: raise Exception(errmsg)
If I use the "print" option I get this:
==============================================================================
Do Something :: Do a process to a thing(Slow Process). | PASS |
------------------------------------------------------------------------------
doSomething :: Overall Results | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
What I want is this:
==============================================================================
Do Something :: Do a process to a thing(Slow Process). | PASS |
doSomething took 3 minutes and 14 seconds.
------------------------------------------------------------------------------
doSomething :: Overall Results | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
You can use the robot.api library. This is the document for the library
https://robot-framework.readthedocs.org/en/latest/_modules/robot/api/logger.html
Have your lib return a string, then use
Set Test Message
to display it.ref: http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html#Set%20Test%20Message
Updates:
Log To Console
command outputs to console in real time (i.e. during test execution, as opposed toSet Test Message
which outputs only at the end of the test case.)Since you are using Python you have two simple possibilities:
Write your messages to the
stderr
. These messages are written both to Robot's log file and to the console. A limitation is that the messages end up to the console only after the keyword you are executing finishes. A bonus is that this approach works also with Java based libraries.Write your messages to
sys.__stdout__
in Python. Robot only interceptssys.stdout
andsys.stderr
and leavessys.__stdout__
(andsys.__stderr__
) alone (as all well behaving Python programs should). These messages only end up to the console, but you can write them also tosys.stdout
to get them also to the log file.