2013-05-29 106 views
0

的Jython V2.5 RobotFramework v2.7.5 的Log4j v1.2.16如何漂亮打印字典由robot.libraries.BuiltIn.BuiltIn.get_variables

我有一個Jython腳本作爲listener用來記錄variables字典返回但pprint模塊不像attributes字典那麼漂亮。

我使用BuiltIn().get_variables()函數來獲取變量詞典:

def start_suite(self, name, attributes): 
    self.LOGGER.info('<<<start_suite>>>')   
    self.LOGGER.debug('variables @ start suite: %s' % pprint.pformat(BuiltIn().get_variables())) 
     self.LOGGER.debug('attributes @ start suite: %s' % pprint.pformat(attributes)) 

但這是在日誌輸出:

2013-05-29 08:03:11,493 [51a618afdf08ff6296260098] DEBUG [NativeMethodAccessorImpl] - variables scoped at start suite: {'${outputdir}': u'/mypath/', '${outputfile}': 'NONE', '${reportfile}': 'NONE', '${none}': None, '${prevtestmessage}': '', '${suitemetadata}': {}, '${suitedocumentation}': '', '${\\n}': '\n', '${/}': '/', '${true}': True, '${:}': ':', '${suitesource}': u'/mypath/source', '${space}': ' ', u'${environment}': u'sit', '${suitename}': u'MySuite', '${debugfile}': 'NONE', '${null}': None, '${logfile}': 'NONE', '${prevteststatus}': '', '${tempdir}': u'/tmp', '${execdir}': u'mypath3', '${prevtestname}': '', '${false}': False, '@{empty}':(), '${empty}': ''} 
2013-05-29 08:03:11,499 [51a618afdf08ff6296260098] DEBUG [NativeMethodAccessorImpl] - attributes @ start suite: {'doc': '', 
'longname': u'BcvDocumentImageRequestTest', 
'metadata': {}, 
'source': u'/robotRoot/MyTest', 
'starttime': '20130529 08:03:11.224', 
'suites': [u'MyTest', u'MyTest'], 
'tests': [], 
'totaltests': 2} 

回答

2

當你觀察,並stated in the docs(雖然含糊) ,BuiltIn().get_variables()不會返回字典,但類似字典的對象 - 要漂亮打印你將不得不從它得到一本字典:

dict(BuiltIn().get_variables().items()) 
+0

這將工作在Python/Jython 2.5.x上? – Ghasfarost

+0

只要你有機器人框架在運行 – theheadofabroom

+0

@Ghasfarost哦實際上 - 我不確定字典理解是否可以運行到2.6,但是經過一些調試進入我的測試用例後,我會看看我已經去了什麼方式太麻煩了 - 我修改了答案 – theheadofabroom