1 # Copyright (C) 2005-2012 Aaron Bentley <abentley@panoramicfeedback.com>
2 # Chris Ball <cjb@laptop.org>
3 # Marien Zwart <marien.zwart@gmail.com>
4 # W. Trevor King <wking@tremily.us>
6 # This file is part of Bugs Everywhere.
8 # Bugs Everywhere is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by the Free
10 # Software Foundation, either version 2 of the License, or (at your option) any
13 # Bugs Everywhere is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 # You should have received a copy of the GNU General Public License along with
19 # Bugs Everywhere. If not, see <http://www.gnu.org/licenses/>.
29 from libbe.util.tree import Tree
30 from libbe.util.plugin import import_by_name
31 from libbe.version import version
33 def python_tree(root_path='libbe', root_modname='libbe'):
40 if f.path.endswith('.py'):
41 f.name = os.path.basename(f.path)[:-len('.py')]
42 elif os.path.isdir(f.path) \
43 and os.path.exists(os.path.join(f.path, '__init__.py')):
44 f.name = os.path.basename(f.path)
46 for child in os.listdir(f.path):
47 if child == '__init__.py':
50 c.path = os.path.join(f.path, child)
56 f.modname = root_modname
58 f.modname = f.parent.modname + '.' + f.name
62 def add_module_tests(suite, modname):
64 mod = import_by_name(modname)
65 except ValueError as e:
66 sys.stderr.write('Failed to import "{}"\n'.format(modname))
68 if hasattr(mod, 'suite'):
71 s = unittest.TestLoader().loadTestsFromModule(mod)
73 sdoc = doctest.DocTestSuite(mod)
79 if __name__ == '__main__':
81 parser = optparse.OptionParser(usage='%prog [options] [modules ...]',
83 """When called without optional module names, run the test suites for
84 *all* modules. This may raise lots of errors if you haven't installed
85 one of the versioning control systems.
87 When called with module name arguments, only run the test suites from
88 those modules and their submodules. For example::
90 $ python test.py libbe.bugdir libbe.storage
92 parser.add_option('-q', '--quiet', action='store_true', default=False,
93 help='Run unittests in quiet mode (verbosity 1).')
94 options,args = parser.parse_args()
95 sys.stderr.write('Testing BE\n{}\n'.format(version(verbose=True)))
98 if options.quiet == True:
101 suite = unittest.TestSuite()
104 for node in tree.traverse():
105 add_module_tests(suite, node.modname)
109 for node in tree.traverse():
110 if node.modname == modname:
111 for n in node.traverse():
112 if n.modname not in added:
113 add_module_tests(suite, n.modname)
114 added.append(n.modname)
117 result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
119 numErrors = len(result.errors)
120 numFailures = len(result.failures)
121 numBad = numErrors + numFailures