1

文档中有单元测试的示例,其中大部分已经集成到 pybuilder 中。

你如何在目标中运行文档测试?

4

1 回答 1

1

现在我只是为每个我想进行文档测试的模块运行一个单元测试。有两种类似的方法可以做到这一点。我已将此单元测试放入 pybuilder unittest 目录中的一个名为gmprod_tests.py

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()

优点是失败的 doctests 的输出会在您运行时出现在控制台输出中pyb

2)还有另一种使用异常的方法。它是相同的代码,只是test_docstrings方法现在看起来像这样:

def test_docstrings(self):
  import bin.lib.gmprod
  doctest.testmod(bin.lib.gmprod,raise_on_error=True)

这样在控制台上就没有详细的 doctest 错误描述了,但是你在 unittest 中写的代码更少了 :)

于 2014-04-03T18:21:25.150 回答