سؤال

There are examples for unittests in the docs and much of this is already integrated into pybuilder.

How do you run doctests in a target?

هل كانت مفيدة؟

المحلول

For now I am just running a unittest for every module I want to doctest. There are 2 similar ways to do it. I have put this unittest into the pybuilder unittest directory in a file named gmprod_tests.py:

1) Without exceptions, just asserting the number of doctest failures to be zero:

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.

2) There is another way using exceptions. It is the same code, only the test_docstrings method now looks like this:

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top