Robot Framework 调用的测试如何将信息返回到控制台

问题描述

我有一个调用 python 方法的机器人框架测试套件.我希望该 python 方法在不通过测试的情况下向控制台返回一条消息.具体来说,我正在尝试为一个过程计时.

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.

我可以使用raise"向控制台返回一条消息,但同时测试失败.

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

我想要的是这个:

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


解决方案

由于您使用的是 Python,您有两种简单的可能性:

Since you are using Python you have two simple possibilities:

  1. 将您的消息写入 stderr.这些消息同时写入机器人的日志文件和控制台.一个限制是消息仅在您正在执行的关键字完成后才会到达控制台.一个好处是这种方法也适用于基于 Java 的库.

  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.

在 Python 中将消息写入 sys.__stdout__.机器人只拦截 sys.stdoutsys.stderr 并单独留下 sys.__stdout__(和 sys.__stderr__)(所有表现良好的 Python 程序都应该这样做).这些消息只会到达控制台,但您也可以将它们写入 sys.stdout 以将它们也写入日志文件.

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.

相关文章