參考了https://blog.csdn.net/wyb199026/article/details/78730097,寫了個這樣的listener
# listen.py
class RobotListener:
ROBOT_LISTENER_API_VERSION = 2
def myprint(self, data):
if type(data)==unicode:
print data.encode("utf-8")
else:
print data
# def __init__(self):
# import easygui
# easygui.msgbox("hi")
def start_suite(self, name, args):
self.myprint("Starting Suite : " + name + " " + args['source'])
def start_test(self, name, args):
self.myprint("Starting test: " + name)
if args['template']:
self.myprint('Template is: ' + args['template'])
def end_test(self, name, args):
self.myprint("Ending test: " + args['longname'])
self.myprint("Test Result is : " + args['status'])
self.myprint("Test Time is: " + str(args['elapsedtime']))
def log_message(self, message):
self.myprint(message['timestamp'] + "\t" + message['level'] + "\t" + message['message'])
卻遇到了如下錯誤
[ ERROR ] Taking listener 'E:\gits\cross\autotest\listen.py' into use failed: Listener 'E:\gits\cross\autotest\listen.py' does not have mandatory 'ROBOT_LISTENER_API_VERSION' attribute.
調試起來非常噁心,先看到官網上用的是PythonListener,就改爲PythonListener,依然報錯
然後我學官網http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#listener-version-3,把ROBOT_LISTENER_API_VERSION = 2放在class外面,不報錯了,但是沒有日誌輸出
以爲是我延遲了讀取管道的緣故(延遲至進程結束),就把日誌輸出重定向到文件,並實時讀取文件,結果發現文件沒有創建
再次做調試,在監聽類的__init__裏寫了這樣的代碼(就是註釋掉的那些,easygui模塊需要pip安裝)
import easygui
easygui.msgbox(“hi”)
發現窗體並沒彈出,自此嚴重懷疑監聽類根本沒被初始化
後來折騰中突然想起,RF裏面自己開發測試庫,要求測試庫的類的名字和文件名一樣.
而我的文件名取的是listener.py,就把類名從RobotListener改成了listener
然後msgbox就彈出來了,於是問題解決
網上無此資料,特此記錄,希望以後遇到這個問題的同學能得到解決