1 from __future__
import print_function
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
17 class DocTestsModuleA(_TestDocTests):
18 def __init__(self, method_name="test_doctests"):
19 super(DocTestsModuleA, self).__init__(pkg_name="ModuleA", method_name=method_name)
21 if __name__ == '__main__':
22 suite = unittest.TestSuite()
23 suite.addTest(DocTestsModuleA())
24 unittest.TextTestRunner(verbosity=2).run(suite)
28 def __init__(self, pkg_name: str, module_name: str =
None, method_name: str =
"test_doctests"):
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
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
47 Iterates over all Python files in module_name/src/module_name and runs doctest.testmod
50 path = rospkg.RosPack().get_path(self.
pkg_name)
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)
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)
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))