简体   繁体   English

您如何从pybuilder运行doctest?

[英]How do you run doctests from pybuilder?

There are examples for unittests in the docs and much of this is already integrated into pybuilder. 在文档中有一些单元测试的例子,其中大部分已经集成到pybuilder中。

How do you run doctests in a target? 您如何在目标中运行doctest?

For now I am just running a unittest for every module I want to doctest. 现在,我只为我要文档测试的每个模块运行单元测试。 There are 2 similar ways to do it. 有2种类似的方法。 I have put this unittest into the pybuilder unittest directory in a file named gmprod_tests.py : 我已将此单元测试放入名为gmprod_tests.py的文件中的pybuilder unittest目录中:

1) Without exceptions, just asserting the number of doctest failures to be zero: 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()

The advantage is that the output of failed doctests appears in your console output when you run pyb . 这样做的好处是,运行pyb时,失败的doctests的输出将显示在控制台输出中。

2) There is another way using exceptions. 2)还有另一种使用异常的方法。 It is the same code, only the test_docstrings method now looks like this: 这是相同的代码,现在只有test_docstrings方法看起来像这样:

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

This way there is no detailed doctest error description on the console, but you write less code in the unittest :) 这样,控制台上没有详细的doctest错误描述,但是您在unittest中编写的代码更少:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM