~bzr-pqm/bzr/bzr.dev

841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
1
# Copyright (C) 2005 by Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Enhanced layer on unittest.
19
20
This does several things:
21
22
* nicer reporting as tests run
23
24
* test code can log messages into a buffer that is recorded to disk
25
  and displayed if the test fails
26
27
* tests can be run in a separate directory, which is useful for code that
28
  wants to create files
29
30
* utilities to run external commands and check their return code
31
  and/or output
32
1102 by Martin Pool
- merge test refactoring from robertc
33
Test cases should normally subclass testsweet.TestCase.  The test runner should
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
34
call runsuite().
35
36
This is meant to become independent of bzr, though that's not quite
37
true yet.
38
"""  
39
1102 by Martin Pool
- merge test refactoring from robertc
40
import unittest
41
import sys
1092.1.20 by Robert Collins
import and use TestUtil to do regex based partial test runs
42
from bzrlib.selftest import TestUtil
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
43
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
44
# XXX: Don't need this anymore now we depend on python2.4
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
45
def _need_subprocess():
46
    sys.stderr.write("sorry, this test suite requires the subprocess module\n"
47
                     "this is shipped with python2.4 and available separately for 2.3\n")
48
    
49
50
class CommandFailed(Exception):
51
    pass
52
53
54
class TestSkipped(Exception):
55
    """Indicates that a test was intentionally skipped, rather than failing."""
56
    # XXX: Not used yet
57
58
1102 by Martin Pool
- merge test refactoring from robertc
59
1092.1.19 by Robert Collins
make tests stop at the first failure, preventing multi-page omgs
60
class EarlyStoppingTestResultAdapter(object):
61
    """An adapter for TestResult to stop at the first first failure or error"""
62
63
    def __init__(self, result):
64
        self._result = result
65
66
    def addError(self, test, err):
67
        self._result.addError(test, err)
68
        self._result.stop()
69
70
    def addFailure(self, test, err):
71
        self._result.addFailure(test, err)
72
        self._result.stop()
73
74
    def __getattr__(self, name):
75
        return getattr(self._result, name)
76
77
    def __setattr__(self, name, value):
78
        if name == '_result':
79
            object.__setattr__(self, name, value)
80
        return setattr(self._result, name, value)
81
82
1102 by Martin Pool
- merge test refactoring from robertc
83
class _MyResult(unittest._TextTestResult):
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
84
    """
85
    Custom TestResult.
86
87
    No special behaviour for now.
88
    """
965 by Martin Pool
- selftest is less verbose by default, and takes a -v option if you want it
89
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
90
    def startTest(self, test):
1102 by Martin Pool
- merge test refactoring from robertc
91
        unittest.TestResult.startTest(self, test)
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
92
        # TODO: Maybe show test.shortDescription somewhere?
842 by Martin Pool
- don't say runit when running tests under python2.3 dammit
93
        what = test.id()
94
        # python2.3 has the bad habit of just "runit" for doctests
95
        if what == 'runit':
96
            what = test.shortDescription()
1102 by Martin Pool
- merge test refactoring from robertc
97
        if self.showAll:
98
            self.stream.write('%-60.60s' % what)
99
        self.stream.flush()
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
100
101
    def addError(self, test, err):
1102 by Martin Pool
- merge test refactoring from robertc
102
        super(_MyResult, self).addError(test, err)
103
        self.stream.flush()
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
104
105
    def addFailure(self, test, err):
1102 by Martin Pool
- merge test refactoring from robertc
106
        super(_MyResult, self).addFailure(test, err)
107
        self.stream.flush()
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
108
109
    def addSuccess(self, test):
1102 by Martin Pool
- merge test refactoring from robertc
110
        if self.showAll:
111
            self.stream.writeln('OK')
112
        elif self.dots:
113
            self.stream.write('~')
114
        self.stream.flush()
115
        unittest.TestResult.addSuccess(self, test)
116
117
    def printErrorList(self, flavour, errors):
118
        for test, err in errors:
119
            self.stream.writeln(self.separator1)
120
            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
121
            self.stream.writeln(self.separator2)
122
            self.stream.writeln("%s" % err)
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
123
            if hasattr(test, '_get_log'):
1102 by Martin Pool
- merge test refactoring from robertc
124
                self.stream.writeln()
125
                self.stream.writeln('log from this test:')
126
                print >>self.stream, test._get_log()
127
128
129
class TextTestRunner(unittest.TextTestRunner):
130
131
    def _makeResult(self):
1092.1.19 by Robert Collins
make tests stop at the first failure, preventing multi-page omgs
132
        result = _MyResult(self.stream, self.descriptions, self.verbosity)
133
        return EarlyStoppingTestResultAdapter(result)
1092.1.4 by Robert Collins
move error printing into the TestResult and use the TextTestRunner from unittest
134
135
1092.1.20 by Robert Collins
import and use TestUtil to do regex based partial test runs
136
class filteringVisitor(TestUtil.TestVisitor):
137
    """I accruse all the testCases I visit that pass a regexp filter on id
138
    into my suite
139
    """
140
141
    def __init__(self, filter):
142
        import re
143
        TestUtil.TestVisitor.__init__(self)
144
        self._suite=None
145
        self.filter=re.compile(filter)
146
147
    def suite(self):
148
        """answer the suite we are building"""
149
        if self._suite is None:
150
            self._suite=TestUtil.TestSuite()
151
        return self._suite
152
153
    def visitCase(self, aCase):
154
        if self.filter.match(aCase.id()):
155
            self.suite().addTest(aCase)
156
157
158
def run_suite(suite, name='test', verbose=False, pattern=".*"):
1092.1.9 by Robert Collins
move working dir creation into InTempDir and nuke the TestSuite subclass as unneeded
159
    import shutil
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
160
    from bzrlib.selftest import TestCaseInTempDir
161
    TestCaseInTempDir._TEST_NAME = name
1102 by Martin Pool
- merge test refactoring from robertc
162
    if verbose:
163
        verbosity = 2
164
    else:
165
        verbosity = 1
166
    runner = TextTestRunner(stream=sys.stdout,
167
                            descriptions=0,
168
                            verbosity=verbosity)
1092.1.20 by Robert Collins
import and use TestUtil to do regex based partial test runs
169
    visitor = filteringVisitor(pattern)
170
    suite.visit(visitor)
171
    result = runner.run(visitor.suite())
1102 by Martin Pool
- merge test refactoring from robertc
172
    # This is still a little bogus, 
173
    # but only a little. Folk not using our testrunner will
174
    # have to delete their temp directories themselves.
175
    if result.wasSuccessful():
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
176
        if TestCaseInTempDir.TEST_ROOT:
177
            shutil.rmtree(TestCaseInTempDir.TEST_ROOT) 
1102 by Martin Pool
- merge test refactoring from robertc
178
    else:
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
179
        print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
841 by Martin Pool
- Start splitting bzr-independent parts of the test framework into
180
    return result.wasSuccessful()