1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005-2011 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
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
from .. import pyutils
23
25
# Mark this python module as being part of the implementation
24
26
# of unittest: this gives us better tracebacks where the last
37
41
def makeCollectingLogger():
38
42
"""I make a logger instance that collects its logs for programmatic analysis
39
43
-> (logger, collector)"""
40
logger=logging.Logger("collector")
41
handler=LogCollector()
44
logger = logging.Logger("collector")
45
handler = LogCollector()
42
46
handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
43
47
logger.addHandler(handler)
44
48
return logger, handler
47
51
def visitTests(suite, visitor):
48
52
"""A foreign method for visiting the tests in a test suite."""
49
53
for test in suite._tests:
50
#Abusing types to avoid monkey patching unittest.TestCase.
54
# Abusing types to avoid monkey patching unittest.TestCase.
51
55
# Maybe that would be better?
53
57
test.visit(visitor)
58
62
visitor.visitSuite(test)
59
63
visitTests(test, visitor)
61
print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
65
print("unvisitable non-unittest.TestCase element %r (%r)" % (
66
test, test.__class__))
69
class FailedCollectionCase(unittest.TestCase):
70
"""Pseudo-test to run and report failure if given case was uncollected"""
72
def __init__(self, case):
73
super(FailedCollectionCase, self).__init__("fail_uncollected")
74
# GZ 2011-09-16: Maybe catch errors from id() method as cases may be
75
# in a bit of a funny state by now.
76
self._problem_case_id = case.id()
79
if self._problem_case_id[-1:] == ")":
80
return self._problem_case_id[:-1] + ",uncollected)"
81
return self._problem_case_id + "(uncollected)"
83
def fail_uncollected(self):
84
self.fail("Uncollected test case: " + self._problem_case_id)
64
87
class TestSuite(unittest.TestSuite):
77
100
tests = list(self)
104
count_stored_tests = getattr(result, "_count_stored_tests", int)
105
from breezy.tests import selftest_debug_flags
106
notify = "uncollected_cases" in selftest_debug_flags
81
108
if result.shouldStop:
82
109
self._tests = reversed(tests)
84
tests.pop().run(result)
111
case = _run_and_collect_case(tests.pop(), result)()
112
new_stored_count = count_stored_tests()
113
if case is not None and isinstance(case, unittest.TestCase):
114
if stored_count == new_stored_count and notify:
115
# Testcase didn't fail, but somehow is still alive
116
FailedCollectionCase(case).run(result)
117
# Adding a new failure so need to reupdate the count
118
new_stored_count = count_stored_tests()
119
# GZ 2011-09-16: Previously zombied the case at this point by
120
# clearing the dict as fallback, skip for now.
121
stored_count = new_stored_count
125
def _run_and_collect_case(case, res):
126
"""Run test case against result and use weakref to drop the refcount"""
128
return weakref.ref(case)
88
131
class TestLoader(unittest.TestLoader):
89
132
"""Custom TestLoader to extend the stock python one."""
107
150
def loadTestsFromModuleName(self, name):
108
151
result = self.suiteClass()
109
module = _load_module_by_name(name)
152
module = pyutils.get_named_object(name)
111
154
result.addTests(self.loadTestsFromModule(module))
114
def loadTestsFromModule(self, module):
115
"""Load tests from a module object.
117
This extension of the python test loader looks for an attribute
118
load_tests in the module object, and if not found falls back to the
119
regular python loadTestsFromModule.
121
If a load_tests attribute is found, it is called and the result is
124
load_tests should be defined like so:
125
>>> def load_tests(standard_tests, module, loader):
128
standard_tests is the tests found by the stock TestLoader in the
129
module, module and loader are the module and loader instances.
131
For instance, to run every test twice, you might do:
132
>>> def load_tests(standard_tests, module, loader):
133
>>> result = loader.suiteClass()
134
>>> for test in iter_suite_tests(standard_tests):
135
>>> result.addTests([test, test])
138
basic_tests = super(TestLoader, self).loadTestsFromModule(module)
139
load_tests = getattr(module, "load_tests", None)
140
if load_tests is not None:
141
return load_tests(basic_tests, module, self)
145
157
def getTestCaseNames(self, test_case_class):
146
158
test_fn_names = self.test_func_names.get(test_case_class, None)
147
159
if test_fn_names is not None:
173
185
return self.suiteClass()
176
def _load_module_by_name(mod_name):
177
parts = mod_name.split('.')
178
module = __import__(mod_name)
180
# for historical reasons python returns the top-level module even though
181
# it loads the submodule; we need to walk down to get the one we want.
183
module = getattr(module, parts.pop(0))
187
188
class TestVisitor(object):
188
189
"""A visitor for Tests"""
189
191
def visitSuite(self, aTestSuite):
191
194
def visitCase(self, aTestCase):