文档中有单元测试的示例,其中大部分已经集成到pybuilder中。
如何在目标中运行doctest?
发布于 2014-04-03 18:21:25
现在,我只是对我想要修改的每个模块运行一个单元测试。有两种类似的方法。我已经将这个unittest放到了一个名为gmprod_tests.py的文件中的pybuilder目录中。
1)毫无例外地,只断言文档测试失败的次数为零:
import unittest
import doctest2 as doctest #pip install doctest2
class GmProdTest (unittest.TestCase):
def test_docstrings(self):
import bin.lib.gmprod
(num_failures, num_attempts) = doctest.testmod(bin.lib.gmprod)
self.assertEquals(num_failures,0)
if __name__ == '__main__':
unittest.main()优点是在运行pyb时,失败的doctest的输出会出现在控制台输出中。
2)还有另一种使用例外的方法。它是相同的代码,现在只有test_docstrings方法如下所示:
def test_docstrings(self):
import bin.lib.gmprod
doctest.testmod(bin.lib.gmprod,raise_on_error=True)通过这种方式,控制台上没有详细的doctest错误描述,但是在unittest中编写的代码更少:)
https://stackoverflow.com/questions/22845327
复制相似问题