我寫了一個python腳本爲我自動完成所有測試,並生成一個HTML報告。前幾天我發現了discover
for unittests,它讓我可以在給定的目錄下運行所有的unittests,而不用明確地命名它們,我真的很希望能夠以同樣的方式來完成我的doctests,而不是必須明確地導入每個模塊。如何將doctests與unittest的測試發現結合起來?
我在https://docs.python.org/2/library/doctest.html上發現了一些關於如何做到這一點的信息,但並沒有真正得到它。你能幫我用discover
與我的doctests?
Python test discovery with doctests, coverage and parallelism是相關的,但仍然不回答我的問題。
coverage_module
import coverage
import doctest
import unittest
import os
# import test_module
import my_module
cov = coverage.Coverage()
cov.start()
# running doctest by explicity naming the module
doctest.testmod(my_module)
# running unittests by just specifying the folder to look into
testLoad = unittest.TestLoader()
testSuite = testLoad.discover(start_dir=os.getcwd())
runner = unittest.TextTestRunner()
runner.run(testSuite)
cov.stop()
cov.save()
cov.html_report()
print "tests completed"
test_module
import unittest
import doctest
from my_module import My_Class
class My_Class_Tests(unittest.TestCase):
def setUp(self):
# setup variables
def test_1(self):
# test code
# The bit that should load up the doctests? What's loader, tests, and ignore though?
# Is this in the right place?
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(module_with_doctests))
return tests
if __name__ == '__main__':
unittest.main()
pytest'現在被認爲是Python測試的標準具,所以我建議檢查一下。看起來它也支持doctests:http://doc.pytest.org/en/latest/doctest.html – yedpodtrzitko