~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to testsweet.py

  • Committer: Robert Collins
  • Date: 2005-10-06 12:14:01 UTC
  • mfrom: (1393.1.67)
  • Revision ID: robertc@robertcollins.net-20051006121401-ce87bcb93909bbdf
merge martins latest

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
33
 
Test cases should normally subclass testsweet.TestCase.  The test runner should
34
 
call runsuite().
35
 
 
36
 
This is meant to become independent of bzr, though that's not quite
37
 
true yet.
38
 
"""  
39
 
 
40
 
import unittest
41
 
import sys
42
 
from bzrlib.selftest import TestUtil
43
 
 
44
 
# XXX: Don't need this anymore now we depend on python2.4
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
 
 
59
 
class EarlyStoppingTestResultAdapter(object):
60
 
    """An adapter for TestResult to stop at the first first failure or error"""
61
 
 
62
 
    def __init__(self, result):
63
 
        self._result = result
64
 
 
65
 
    def addError(self, test, err):
66
 
        self._result.addError(test, err)
67
 
        self._result.stop()
68
 
 
69
 
    def addFailure(self, test, err):
70
 
        self._result.addFailure(test, err)
71
 
        self._result.stop()
72
 
 
73
 
    def __getattr__(self, name):
74
 
        return getattr(self._result, name)
75
 
 
76
 
    def __setattr__(self, name, value):
77
 
        if name == '_result':
78
 
            object.__setattr__(self, name, value)
79
 
        return setattr(self._result, name, value)
80
 
 
81
 
 
82
 
class _MyResult(unittest._TextTestResult):
83
 
    """
84
 
    Custom TestResult.
85
 
 
86
 
    No special behaviour for now.
87
 
    """
88
 
 
89
 
    def startTest(self, test):
90
 
        unittest.TestResult.startTest(self, test)
91
 
        # TODO: Maybe show test.shortDescription somewhere?
92
 
        what = test.id()
93
 
        # python2.3 has the bad habit of just "runit" for doctests
94
 
        if what == 'runit':
95
 
            what = test.shortDescription()
96
 
        if self.showAll:
97
 
            self.stream.write('%-60.60s' % what)
98
 
        self.stream.flush()
99
 
 
100
 
    def addError(self, test, err):
101
 
        super(_MyResult, self).addError(test, err)
102
 
        self.stream.flush()
103
 
 
104
 
    def addFailure(self, test, err):
105
 
        super(_MyResult, self).addFailure(test, err)
106
 
        self.stream.flush()
107
 
 
108
 
    def addSuccess(self, test):
109
 
        if self.showAll:
110
 
            self.stream.writeln('OK')
111
 
        elif self.dots:
112
 
            self.stream.write('~')
113
 
        self.stream.flush()
114
 
        unittest.TestResult.addSuccess(self, test)
115
 
 
116
 
    def printErrorList(self, flavour, errors):
117
 
        for test, err in errors:
118
 
            self.stream.writeln(self.separator1)
119
 
            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
120
 
            self.stream.writeln(self.separator2)
121
 
            self.stream.writeln("%s" % err)
122
 
            if hasattr(test, '_get_log'):
123
 
                self.stream.writeln()
124
 
                self.stream.writeln('log from this test:')
125
 
                print >>self.stream, test._get_log()
126
 
 
127
 
 
128
 
class TextTestRunner(unittest.TextTestRunner):
129
 
 
130
 
    def _makeResult(self):
131
 
        result = _MyResult(self.stream, self.descriptions, self.verbosity)
132
 
        return EarlyStoppingTestResultAdapter(result)
133
 
 
134
 
 
135
 
class filteringVisitor(TestUtil.TestVisitor):
136
 
    """I accruse all the testCases I visit that pass a regexp filter on id
137
 
    into my suite
138
 
    """
139
 
 
140
 
    def __init__(self, filter):
141
 
        import re
142
 
        TestUtil.TestVisitor.__init__(self)
143
 
        self._suite=None
144
 
        self.filter=re.compile(filter)
145
 
 
146
 
    def suite(self):
147
 
        """answer the suite we are building"""
148
 
        if self._suite is None:
149
 
            self._suite=TestUtil.TestSuite()
150
 
        return self._suite
151
 
 
152
 
    def visitCase(self, aCase):
153
 
        if self.filter.match(aCase.id()):
154
 
            self.suite().addTest(aCase)
155
 
 
156
 
 
157
 
def run_suite(suite, name='test', verbose=False, pattern=".*"):
158
 
    import shutil
159
 
    from bzrlib.selftest import TestCaseInTempDir
160
 
    TestCaseInTempDir._TEST_NAME = name
161
 
    if verbose:
162
 
        verbosity = 2
163
 
    else:
164
 
        verbosity = 1
165
 
    runner = TextTestRunner(stream=sys.stdout,
166
 
                            descriptions=0,
167
 
                            verbosity=verbosity)
168
 
    visitor = filteringVisitor(pattern)
169
 
    suite.visit(visitor)
170
 
    result = runner.run(visitor.suite())
171
 
    # This is still a little bogus, 
172
 
    # but only a little. Folk not using our testrunner will
173
 
    # have to delete their temp directories themselves.
174
 
    if result.wasSuccessful():
175
 
        if TestCaseInTempDir.TEST_ROOT is not None:
176
 
            shutil.rmtree(TestCaseInTempDir.TEST_ROOT) 
177
 
    else:
178
 
        print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
179
 
    return result.wasSuccessful()