~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Vincent Ladeuil
  • Date: 2010-10-26 08:08:23 UTC
  • mfrom: (5514.1.1 665100-content-type)
  • mto: This revision was merged to the branch mainline in revision 5516.
  • Revision ID: v.ladeuil+lp@free.fr-20101026080823-3wggo03b7cpn9908
Correctly set the Content-Type header when POSTing http requests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2004 Canonical Limited
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#       Author: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
#
18
18
 
19
19
import sys
20
20
import logging
21
21
import unittest
22
22
 
 
23
from bzrlib import pyutils
 
24
 
 
25
# Mark this python module as being part of the implementation
 
26
# of unittest: this gives us better tracebacks where the last
 
27
# shown frame is the test code, not our assertXYZ.
 
28
__unittest = 1
 
29
 
23
30
 
24
31
class LogCollector(logging.Handler):
25
32
    def __init__(self):
42
49
def visitTests(suite, visitor):
43
50
    """A foreign method for visiting the tests in a test suite."""
44
51
    for test in suite._tests:
45
 
        #Abusing types to avoid monkey patching unittest.TestCase. 
 
52
        #Abusing types to avoid monkey patching unittest.TestCase.
46
53
        # Maybe that would be better?
47
54
        try:
48
55
            test.visit(visitor)
54
61
                visitTests(test, visitor)
55
62
            else:
56
63
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
57
 
    
 
64
 
58
65
 
59
66
class TestSuite(unittest.TestSuite):
60
67
    """I am an extended TestSuite with a visitor interface.
67
74
        visitor.visitSuite(self)
68
75
        visitTests(self, visitor)
69
76
 
 
77
    def run(self, result):
 
78
        """Run the tests in the suite, discarding references after running."""
 
79
        tests = list(self)
 
80
        tests.reverse()
 
81
        self._tests = []
 
82
        while tests:
 
83
            if result.shouldStop:
 
84
                self._tests = reversed(tests)
 
85
                break
 
86
            tests.pop().run(result)
 
87
        return result
 
88
 
70
89
 
71
90
class TestLoader(unittest.TestLoader):
72
 
    """Custom  TestLoader to address some quirks in the stock python one."""
 
91
    """Custom TestLoader to extend the stock python one."""
 
92
 
73
93
    suiteClass = TestSuite
 
94
    # Memoize test names by test class dict
 
95
    test_func_names = {}
74
96
 
75
97
    def loadTestsFromModuleNames(self, names):
76
98
        """use a custom means to load tests from modules.
77
99
 
78
 
        There is an undesirable glitch in the python TestLoader where a 
79
 
        import error is ignore. We think this can be solved by ensuring the 
 
100
        There is an undesirable glitch in the python TestLoader where a
 
101
        import error is ignore. We think this can be solved by ensuring the
80
102
        requested name is resolvable, if its not raising the original error.
81
103
        """
82
104
        result = self.suiteClass()
83
105
        for name in names:
84
 
            _load_module_by_name(name)
85
 
            result.addTests(self.loadTestsFromName(name))
86
 
        return result
87
 
 
88
 
 
89
 
def _load_module_by_name(mod_name):
90
 
    parts = mod_name.split('.')
91
 
    module = __import__(mod_name)
92
 
    del parts[0]
93
 
    # for historical reasons python returns the top-level module even though
94
 
    # it loads the submodule; we need to walk down to get the one we want.
95
 
    while parts:
96
 
        module = getattr(module, parts.pop(0))
97
 
    return module
 
106
            result.addTests(self.loadTestsFromModuleName(name))
 
107
        return result
 
108
 
 
109
    def loadTestsFromModuleName(self, name):
 
110
        result = self.suiteClass()
 
111
        module = pyutils.get_named_object(name)
 
112
 
 
113
        result.addTests(self.loadTestsFromModule(module))
 
114
        return result
 
115
 
 
116
    def loadTestsFromModule(self, module):
 
117
        """Load tests from a module object.
 
118
 
 
119
        This extension of the python test loader looks for an attribute
 
120
        load_tests in the module object, and if not found falls back to the
 
121
        regular python loadTestsFromModule.
 
122
 
 
123
        If a load_tests attribute is found, it is called and the result is
 
124
        returned.
 
125
 
 
126
        load_tests should be defined like so:
 
127
        >>> def load_tests(standard_tests, module, loader):
 
128
        >>>    pass
 
129
 
 
130
        standard_tests is the tests found by the stock TestLoader in the
 
131
        module, module and loader are the module and loader instances.
 
132
 
 
133
        For instance, to run every test twice, you might do:
 
134
        >>> def load_tests(standard_tests, module, loader):
 
135
        >>>     result = loader.suiteClass()
 
136
        >>>     for test in iter_suite_tests(standard_tests):
 
137
        >>>         result.addTests([test, test])
 
138
        >>>     return result
 
139
        """
 
140
        if sys.version_info < (2, 7):
 
141
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
142
        else:
 
143
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
 
144
            #                a different and incompatible signature
 
145
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
 
146
                use_load_tests=False)
 
147
        load_tests = getattr(module, "load_tests", None)
 
148
        if load_tests is not None:
 
149
            return load_tests(basic_tests, module, self)
 
150
        else:
 
151
            return basic_tests
 
152
 
 
153
    def getTestCaseNames(self, test_case_class):
 
154
        test_fn_names = self.test_func_names.get(test_case_class, None)
 
155
        if test_fn_names is not None:
 
156
            # We already know them
 
157
            return test_fn_names
 
158
 
 
159
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
 
160
                                                             test_case_class)
 
161
        self.test_func_names[test_case_class] = test_fn_names
 
162
        return test_fn_names
 
163
 
 
164
 
 
165
class FilteredByModuleTestLoader(TestLoader):
 
166
    """A test loader that import only the needed modules."""
 
167
 
 
168
    def __init__(self, needs_module):
 
169
        """Constructor.
 
170
 
 
171
        :param needs_module: a callable taking a module name as a
 
172
            parameter returing True if the module should be loaded.
 
173
        """
 
174
        TestLoader.__init__(self)
 
175
        self.needs_module = needs_module
 
176
 
 
177
    def loadTestsFromModuleName(self, name):
 
178
        if self.needs_module(name):
 
179
            return TestLoader.loadTestsFromModuleName(self, name)
 
180
        else:
 
181
            return self.suiteClass()
98
182
 
99
183
 
100
184
class TestVisitor(object):