2012-03-05 39 views
1

我有一個doctest,當文件沒有找到時,它需要IOError。如何處理python的doctest中的變量錯誤?

>>> configParser('conffig.ini') # should not exist 
Traceback (most recent call last): 
    ... 
IOError: No such file: /homes/ndeklein/workspace/MS/PyMS/conffig.ini 

但是,如果我想從不同的PC測試,或者其他人想要測試它,路徑不會是/家庭/ ndeklein /工作區/ MS/PyMS /。我願意這樣做

>>> configParser('conffig.ini') # should not exist 
Traceback (most recent call last): 
    ... 
IOError: No such file: os.path.abspath(conffig.ini) 

,而是因爲它是在文檔字符串它認爲os.path.abspath將(作爲結果的一部分。

我怎樣才能讓文檔字符串測試變量的結果?

+0

爲什麼您要測試磁盤上文件的非阻力?有沒有理由懷疑文件系統的正確性? – SingleNegationElimination 2012-03-05 14:48:27

回答

2

你真的需要來匹配的路徑名如果沒有,那麼只需要用省略號跳過輸出的一部分:

>>> configParser('conffig.ini') # should not exist 
Traceback (most recent call last): 
    ... 
IOError: No such file: ... 

如果你這樣做,那麼你就需要捕獲錯誤並手動測試數值。例如:

>>> try: 
... configParser('conffig.ini') # should not exist 
... except IOError as e: 
... print('ok' if str(e).endswith(os.path.abspath('conffig.ini')) else 'fail') 
ok