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

时间:2011-04-04 15:30:39

标签: python frameworks automated-tests robotframework

我有一个调用python方法的机器人框架测试套件。我希望该python方法在不失败测试的情况下将消息返回到控制台。具体来说,我正在尝试计划一个过程。

我可以使用“raise”将消息返回到控制台,但同时也无法通过测试。

 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)

或者我可以使用“print”将消息返回到日志文件并报告而不会使测试失败,但该信息仅在报告中提供,而不是在控制台中提供。

 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)

如果我使用“打印”选项,我会得到:

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

3 个答案:

答案 0 :(得分:13)

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

  1. 将您的消息写入stderr。这些消息都写入Robot的日志文件和控制台。限制是只有在您执行的关键字结束后,消息才会结束到控制台。值得一提的是,这种方法也适用于基于Java的库。

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

答案 1 :(得分:2)

您可以使用robot.api库。这是图书馆的文件

https://robot-framework.readthedocs.org/en/latest/_modules/robot/api/logger.html

答案 2 :(得分:0)

让你的lib返回一个字符串,然后用Set Test Message显示它。

My Test Case  [Documentation]  display data returned from lib call
  ${r} =  mylib.libfunc  arg=param
  Set Test Message  libfunc returned ${r}

参考:http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html#Set%20Test%20Message

更新

  1. 新链接:http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Test%20Message
  2. new Log To Console命令实时输出到控制台(即在测试执行期间,而不是仅在测试用例结束时输出的Set Test Message。)