1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
2
2
# Author: Robert Collins <robert.collins@canonical.com>
4
4
# This program is free software; you can redistribute it and/or modify
63
58
visitor.visitSuite(test)
64
59
visitTests(test, visitor)
66
print "unvisitable non-unittest.TestCase element %r (%r)" % (
70
class FailedCollectionCase(unittest.TestCase):
71
"""Pseudo-test to run and report failure if given case was uncollected"""
73
def __init__(self, case):
74
super(FailedCollectionCase, self).__init__("fail_uncollected")
75
# GZ 2011-09-16: Maybe catch errors from id() method as cases may be
76
# in a bit of a funny state by now.
77
self._problem_case_id = case.id()
80
if self._problem_case_id[-1:] == ")":
81
return self._problem_case_id[:-1] + ",uncollected)"
82
return self._problem_case_id + "(uncollected)"
84
def fail_uncollected(self):
85
self.fail("Uncollected test case: " + self._problem_case_id)
61
print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
88
64
class TestSuite(unittest.TestSuite):
96
72
visitor.visitSuite(self)
97
73
visitTests(self, visitor)
99
def run(self, result):
100
"""Run the tests in the suite, discarding references after running."""
105
count_stored_tests = getattr(result, "_count_stored_tests", int)
106
from bzrlib.tests import selftest_debug_flags
107
notify = "uncollected_cases" in selftest_debug_flags
109
if result.shouldStop:
110
self._tests = reversed(tests)
112
case = _run_and_collect_case(tests.pop(), result)()
113
new_stored_count = count_stored_tests()
114
if case is not None and isinstance(case, unittest.TestCase):
115
if stored_count == new_stored_count and notify:
116
# Testcase didn't fail, but somehow is still alive
117
FailedCollectionCase(case).run(result)
118
# Adding a new failure so need to reupdate the count
119
new_stored_count = count_stored_tests()
120
# GZ 2011-09-16: Previously zombied the case at this point by
121
# clearing the dict as fallback, skip for now.
122
stored_count = new_stored_count
126
def _run_and_collect_case(case, res):
127
"""Run test case against result and use weakref to drop the refcount"""
129
return weakref.ref(case)
132
76
class TestLoader(unittest.TestLoader):
133
77
"""Custom TestLoader to extend the stock python one."""
151
95
def loadTestsFromModuleName(self, name):
152
96
result = self.suiteClass()
153
module = pyutils.get_named_object(name)
97
module = _load_module_by_name(name)
155
99
result.addTests(self.loadTestsFromModule(module))
179
123
>>> result.addTests([test, test])
180
124
>>> return result
182
if sys.version_info < (2, 7):
183
basic_tests = super(TestLoader, self).loadTestsFromModule(module)
185
# GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
186
# a different and incompatible signature
187
basic_tests = super(TestLoader, self).loadTestsFromModule(module,
188
use_load_tests=False)
126
basic_tests = super(TestLoader, self).loadTestsFromModule(module)
189
127
load_tests = getattr(module, "load_tests", None)
190
128
if load_tests is not None:
191
129
return load_tests(basic_tests, module, self)
223
161
return self.suiteClass()
164
def _load_module_by_name(mod_name):
165
parts = mod_name.split('.')
166
module = __import__(mod_name)
168
# for historical reasons python returns the top-level module even though
169
# it loads the submodule; we need to walk down to get the one we want.
171
module = getattr(module, parts.pop(0))
226
175
class TestVisitor(object):
227
176
"""A visitor for Tests"""
229
177
def visitSuite(self, aTestSuite):
232
179
def visitCase(self, aTestCase):