我同意上面提到的有關doctest不縮放的所有上述問題,我更願意堅持單元測試。
我可以貢獻的一點是從代碼處理__name__ == "__main__
調用單元測試,因此如果測試文件作爲腳本運行,它將運行測試。
如:
#!/usr/bin/env python
"""
Unit tests for the GetFiles.py utility
"""
import unittest
from FileUtilities import getTree
class TestFileUtilities(unittest.TestCase):
def testGetTree(self):
"""
Tests that a known tree is found and incidentally confirms
that we have the tree we expected to use for our current
sample extraction.
"""
found = getTree('./anzmeta-dtd', '.pen')
expected_path_tail = ['ISOdia.pen',
'ISOgrk1.pen',
'ISOtech.pen']
for i, full_path in enumerate(found):
assert full_path.endswith(expected_path_tail[i]), expected_path_tail[i]
# other tests elided
if __name__ == "__main__":
# When this module is executed from the command-line, run all its tests
unittest.main()
對不起,我不是故意的文檔測試的*覆蓋*,通過運行文檔測試行使代碼的更多報道。但是,在你的建議和亞歷克斯之間,我深信不要使用它們,而是去單元測試! – fmark 2010-07-14 02:27:48
當然,接受@ bstpiere有關coverage.py的建議! :) – 2010-07-14 02:30:01