Robot Framework调用的测试如何将信息返回到控制台
我有一个调用 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
==============================================================================
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
==============================================================================
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您使用的是 Python,因此您有两种简单的可能性:
将消息写入
stderr
。这些消息会写入机器人的日志文件和控制台。一个限制是,只有在您正在执行的关键字完成后,消息才会最终到达控制台。一个好处是,这种方法也适用于基于 Java 的库。使用 Python 将消息写入
sys.__stdout__
。 Robot 仅拦截sys.stdout
和sys.stderr
并单独保留sys.__stdout__
(和sys.__stderr__
) (正如所有表现良好的 Python 程序应该的那样)。这些消息仅最终到达控制台,但您也可以将它们写入 sys.stdout,以将它们也写入日志文件。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.您可以使用 robots.api 库。这是库的文档
https://robot- Framework.readthedocs.org/en/latest/_modules/robot/api/logger.html
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
让您的库返回一个字符串,然后使用
Set Test Message
来显示它。参考:http://robotframework.googlecode.com/hg /doc/libraries/BuiltIn.html#Set%20Test%20Message
更新:
Log To Console
命令输出到控制台实时(即在测试执行期间,而不是仅在测试用例结束时输出的Set Test Message
。)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.)