[英]How can a test called by Robot Framework return information to the console
我有一個調用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)
或者我可以使用“打印”將消息返回到日志文件並報告而不會使測試失敗,但該信息僅在報告中可用,而不是在控制台中。
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
==============================================================================
由於您使用的是Python,因此您有兩種簡單的可能性:
將您的消息寫入stderr
。 這些消息都寫入Robot的日志文件和控制台。 限制是只有在您執行的關鍵字結束后,消息才會結束到控制台。 值得一提的是,這種方法也適用於基於Java的庫。
在Python sys.__stdout__
您的消息寫入sys.__stdout__
。 Robot只攔截sys.stdout
和sys.stderr
並單獨留下sys.__stdout__
(和sys.__stderr__
)(因為所有表現良好的Python程序都應該這樣)。 這些消息最終只能到控制台,但您也可以將它們寫入sys.stdout
以將它們也傳遞給日志文件。
您可以使用robot.api庫。 這是圖書館的文件
https://robot-framework.readthedocs.org/en/latest/_modules/robot/api/logger.html
讓您的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
更新:
Log To Console
命令實時輸出到控制台(即在測試執行期間,而不是僅在測試用例結束時輸出的Set Test Message
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.