Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a test called by Robot Framework return information to the console

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
==============================================================================
like image 724
Skip Huffman Avatar asked Apr 04 '11 15:04

Skip Huffman


People also ask

How do you call a function in Robot Framework?

To import the Python script inside Robot, we use the keyword Library in the Robot file under ***settings*** . To call the function, we use <file_name> <dot> <function name> . To add keywords inside the function, we use the keyword decorator. Here, BuildIn().

How do I read a text file in Robot Framework?

You can use the keyword Get File from the OperatingSystem library to read the file, and you can use the Split to Lines keyword from the String library to convert the file contents to a list of lines. Then it's just a matter of looping over the lines using a for loop.


1 Answers

Since you are using Python you have two simple possibilities:

  1. 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.

  2. Write your messages to sys.__stdout__ in Python. Robot only intercepts sys.stdout and sys.stderr and leaves sys.__stdout__ (and sys.__stderr__) alone (as all well behaving Python programs should). These messages only end up to the console, but you can write them also to sys.stdout to get them also to the log file.

like image 148
Pekka Klärck Avatar answered Sep 17 '22 21:09

Pekka Klärck