【问题标题】:How do you run doctests from pybuilder?你如何从 pybuilder 运行 doctest?
【发布时间】:2014-04-03 17:53:55
【问题描述】:

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

如何在目标中运行 doctest?

【问题讨论】:

    标签: python doctest pybuilder


    【解决方案1】:

    现在我只是为每个我想进行 doctest 的模块运行一个单元测试。有两种类似的方法可以做到这一点。我已将此单元测试放入 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()
    

    优点是当您运行pyb 时,失败的文档测试的输出会出现在您的控制台输出中。

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-10-03
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多