/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
 
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
47
47
def visitTests(suite, visitor):
48
48
    """A foreign method for visiting the tests in a test suite."""
49
49
    for test in suite._tests:
50
 
        #Abusing types to avoid monkey patching unittest.TestCase. 
 
50
        #Abusing types to avoid monkey patching unittest.TestCase.
51
51
        # Maybe that would be better?
52
52
        try:
53
53
            test.visit(visitor)
59
59
                visitTests(test, visitor)
60
60
            else:
61
61
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
62
 
    
 
62
 
63
63
 
64
64
class TestSuite(unittest.TestSuite):
65
65
    """I am an extended TestSuite with a visitor interface.
72
72
        visitor.visitSuite(self)
73
73
        visitTests(self, visitor)
74
74
 
 
75
    def run(self, result):
 
76
        """Run the tests in the suite, discarding references after running."""
 
77
        tests = list(self)
 
78
        tests.reverse()
 
79
        self._tests = []
 
80
        while tests:
 
81
            if result.shouldStop:
 
82
                self._tests = reversed(tests)
 
83
                break
 
84
            tests.pop().run(result)
 
85
        return result
 
86
 
75
87
 
76
88
class TestLoader(unittest.TestLoader):
77
89
    """Custom TestLoader to extend the stock python one."""
83
95
    def loadTestsFromModuleNames(self, names):
84
96
        """use a custom means to load tests from modules.
85
97
 
86
 
        There is an undesirable glitch in the python TestLoader where a 
87
 
        import error is ignore. We think this can be solved by ensuring the 
 
98
        There is an undesirable glitch in the python TestLoader where a
 
99
        import error is ignore. We think this can be solved by ensuring the
88
100
        requested name is resolvable, if its not raising the original error.
89
101
        """
90
102
        result = self.suiteClass()
91
103
        for name in names:
92
 
            module = _load_module_by_name(name)
93
 
            result.addTests(self.loadTestsFromModule(module))
 
104
            result.addTests(self.loadTestsFromModuleName(name))
 
105
        return result
 
106
 
 
107
    def loadTestsFromModuleName(self, name):
 
108
        result = self.suiteClass()
 
109
        module = _load_module_by_name(name)
 
110
 
 
111
        result.addTests(self.loadTestsFromModule(module))
94
112
        return result
95
113
 
96
114
    def loadTestsFromModule(self, module):
101
119
        regular python loadTestsFromModule.
102
120
 
103
121
        If a load_tests attribute is found, it is called and the result is
104
 
        returned. 
 
122
        returned.
105
123
 
106
124
        load_tests should be defined like so:
107
125
        >>> def load_tests(standard_tests, module, loader):
127
145
    def getTestCaseNames(self, test_case_class):
128
146
        test_fn_names = self.test_func_names.get(test_case_class, None)
129
147
        if test_fn_names is not None:
130
 
            # We already calculate that
 
148
            # We already know them
131
149
            return test_fn_names
132
150
 
133
151
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
135
153
        self.test_func_names[test_case_class] = test_fn_names
136
154
        return test_fn_names
137
155
 
 
156
 
 
157
class FilteredByModuleTestLoader(TestLoader):
 
158
    """A test loader that import only the needed modules."""
 
159
 
 
160
    def __init__(self, needs_module):
 
161
        """Constructor.
 
162
 
 
163
        :param needs_module: a callable taking a module name as a
 
164
            parameter returing True if the module should be loaded.
 
165
        """
 
166
        TestLoader.__init__(self)
 
167
        self.needs_module = needs_module
 
168
 
 
169
    def loadTestsFromModuleName(self, name):
 
170
        if self.needs_module(name):
 
171
            return TestLoader.loadTestsFromModuleName(self, name)
 
172
        else:
 
173
            return self.suiteClass()
 
174
 
 
175
 
138
176
def _load_module_by_name(mod_name):
139
177
    parts = mod_name.split('.')
140
178
    module = __import__(mod_name)