文档中有单元测试的例子,其中大部分都已集成到pybuilder中。
如何在目标中运行doctests?
答案 0 :(得分:1)
现在我只是为每个想要doctest的模块运行一个单元测试。有两种类似的方法可以做到这一点。我已将此unittest放入名为gmprod_tests.py
的文件中的pybuilder unittest目录中:
1)没有例外,只是断言doctest失败的数量为零:
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
时,失败的doctests的输出会出现在您的控制台输出中。
2)还有另一种使用异常的方法。它是相同的代码,现在只有test_docstrings
方法如下所示:
def test_docstrings(self):
import bin.lib.gmprod
doctest.testmod(bin.lib.gmprod,raise_on_error=True)
这样在控制台上没有详细的doctest错误描述,但你在unittest中编写了更少的代码:)