test_tools
doctest_unittest.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import doctest
4 import importlib
5 import os
6 import rospkg
7 import unittest
8 
9 
10 class _TestDocTests(unittest.TestCase):
11  """
12  Parent class, which runs all doctests in a python module. As rospkg is used to locate the module, this test only
13  can be applied to catkin packages.
14  To create your own test:
15  from test_tools.doctest_unittest import _TestDocTests
16 
17  class DocTestsModuleA(_TestDocTests):
18  def __init__(self, method_name="test_doctests"):
19  super(DocTestsModuleA, self).__init__(pkg_name="ModuleA", method_name=method_name)
20 
21  if __name__ == '__main__':
22  suite = unittest.TestSuite()
23  suite.addTest(DocTestsModuleA())
24  unittest.TextTestRunner(verbosity=2).run(suite)
25 
26  """
27 
28  def __init__(self, pkg_name: str, module_name: str = None, method_name: str = "test_doctests"):
29  """
30  Constructor
31 
32  :param pkg_name: Name of catkin package
33  :param module_name: Name of the python module
34  :param method_name: Name of the member variable to run, this should be "test_doctests" and shouldn't
35  be changed.
36  """
37  assert method_name == "test_doctests", "The method_name should be 'test_doctests'. This is the function which" \
38  "implements the functionality of this TestCase."
39  super(_TestDocTests, self).__init__(method_name)
40  if module_name is None:
41  module_name = pkg_name
42  self.pkg_name = pkg_name
43  self.module_name = module_name
44 
45  def test_doctests(self):
46  """
47  Iterates over all Python files in module_name/src/module_name and runs doctest.testmod
48  """
49 
50  path = rospkg.RosPack().get_path(self.pkg_name)
51  path = os.path.join(path, "src", self.module_name)
52 
53  for root, dirs, files in os.walk(path):
54  for filename in files:
55  if filename.endswith(".py") and "__init__" not in filename:
56  filepath = os.path.join(root, filename)
57  module_name = self.module_name + filepath.split(self.module_name)[-1]
58  module_name = module_name[:-3]
59  module_name = module_name.replace("/", ".")
60  mod = importlib.import_module(module_name)
61  doctest.testmod(mod, report=1)
62 
63  failed_count, attempted_count = doctest.master.summarize(True)
64  print("Attempted count: {}".format(attempted_count))
65  self.assertEqual(failed_count, 0, "{} out of {} tests failed".format(failed_count, attempted_count))
test_tools.doctest_unittest._TestDocTests.module_name
module_name
Definition: doctest_unittest.py:43
test_tools.doctest_unittest._TestDocTests.pkg_name
pkg_name
Definition: doctest_unittest.py:42
test_tools.doctest_unittest._TestDocTests.__init__
def __init__(self, str pkg_name, str module_name=None, str method_name="test_doctests")
Definition: doctest_unittest.py:28
test_tools.doctest_unittest._TestDocTests.test_doctests
def test_doctests(self)
Definition: doctest_unittest.py:45
test_tools.doctest_unittest._TestDocTests
Definition: doctest_unittest.py:10