~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

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()
107
119
        regular python loadTestsFromModule.
108
120
 
109
121
        If a load_tests attribute is found, it is called and the result is
110
 
        returned. 
 
122
        returned.
111
123
 
112
124
        load_tests should be defined like so:
113
125
        >>> def load_tests(standard_tests, module, loader):
123
135
        >>>         result.addTests([test, test])
124
136
        >>>     return result
125
137
        """
126
 
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
138
        if sys.version_info < (2, 7):
 
139
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
140
        else:
 
141
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
 
142
            #                a different and incompatible signature
 
143
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
 
144
                use_load_tests=False)
127
145
        load_tests = getattr(module, "load_tests", None)
128
146
        if load_tests is not None:
129
147
            return load_tests(basic_tests, module, self)