1 # Copyright (C) 2005-2010 Aaron Bentley and Panometrics, Inc.
2 # Marien Zwart <marienz@gentoo.org>
3 # W. Trevor King <wking@drexel.edu>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 from libbe.util.tree import Tree
28 from libbe.util.plugin import import_by_name
29 from libbe.version import version
31 def python_tree(root_path='libbe', root_modname='libbe'):
38 if f.path.endswith('.py'):
39 f.name = os.path.basename(f.path)[:-len('.py')]
40 elif os.path.isdir(f.path) \
41 and os.path.exists(os.path.join(f.path, '__init__.py')):
42 f.name = os.path.basename(f.path)
44 for child in os.listdir(f.path):
45 if child == '__init__.py':
48 c.path = os.path.join(f.path, child)
54 f.modname = root_modname
56 f.modname = f.parent.modname + '.' + f.name
60 def add_module_tests(suite, modname):
62 mod = import_by_name(modname)
64 print >> sys.stderr, 'Failed to import "%s"' % (modname)
66 if hasattr(mod, 'suite'):
69 s = unittest.TestLoader().loadTestsFromModule(mod)
71 sdoc = doctest.DocTestSuite(mod)
77 if __name__ == '__main__':
79 parser = optparse.OptionParser(usage='%prog [options] [modules ...]',
81 """When called without optional module names, run the test suites for
82 *all* modules. This may raise lots of errors if you haven't installed
83 one of the versioning control systems.
85 When called with module name arguments, only run the test suites from
86 those modules and their submodules. For example::
88 $ python test.py libbe.bugdir libbe.storage
90 parser.add_option('-q', '--quiet', action='store_true', default=False,
91 help='Run unittests in quiet mode (verbosity 1).')
92 options,args = parser.parse_args()
93 print >> sys.stderr, 'Testing BE\n%s' % version(verbose=True)
96 if options.quiet == True:
99 suite = unittest.TestSuite()
102 for node in tree.traverse():
103 add_module_tests(suite, node.modname)
107 for node in tree.traverse():
108 if node.modname == modname:
109 for n in node.traverse():
110 if n.modname not in added:
111 add_module_tests(suite, n.modname)
112 added.append(n.modname)
115 result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
117 numErrors = len(result.errors)
118 numFailures = len(result.failures)
119 numBad = numErrors + numFailures