Robot框架调用的测试如何将信息返回给cons

2024-10-02 16:23:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个robot框架测试套件,它调用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
==============================================================================

Tags: andtestselftimepassprocessdoraise
3条回答

让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. 新的Log To Console命令实时输出到控制台(即在测试执行期间,而Set Test Message只在测试用例结束时输出)

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

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

因为您使用的是Python,所以有两种简单的可能性:

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

  2. 用Python将消息写到sys.__stdout__。Robot只截取sys.stdoutsys.stderr,只留下sys.__stdout__(和sys.__stderr__)一个(所有性能良好的Python程序都应该这样)。这些消息最终只能发送到控制台,但您也可以将它们写入sys.stdout,以便将它们也发送到日志文件。

相关问题 更多 >