1
# Copyright (C) 2005 by Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
18
"""Enhanced layer on unittest.
 
 
20
This does several things:
 
 
22
* nicer reporting as tests run
 
 
24
* test code can log messages into a buffer that is recorded to disk
 
 
25
  and displayed if the test fails
 
 
27
* tests can be run in a separate directory, which is useful for code that
 
 
30
* utilities to run external commands and check their return code
 
 
33
Test cases should normally subclass testsweet.TestCase.  The test runner should
 
 
36
This is meant to become independent of bzr, though that's not quite
 
 
43
# XXX: Don't need this anymore now we depend on python2.4
 
 
44
def _need_subprocess():
 
 
45
    sys.stderr.write("sorry, this test suite requires the subprocess module\n"
 
 
46
                     "this is shipped with python2.4 and available separately for 2.3\n")
 
 
49
class CommandFailed(Exception):
 
 
53
class TestSkipped(Exception):
 
 
54
    """Indicates that a test was intentionally skipped, rather than failing."""
 
 
58
class _MyResult(unittest._TextTestResult):
 
 
62
    No special behaviour for now.
 
 
65
    def startTest(self, test):
 
 
66
        unittest.TestResult.startTest(self, test)
 
 
67
        # TODO: Maybe show test.shortDescription somewhere?
 
 
69
        # python2.3 has the bad habit of just "runit" for doctests
 
 
71
            what = test.shortDescription()
 
 
73
            self.stream.write('%-60.60s' % what)
 
 
76
    def addError(self, test, err):
 
 
77
        super(_MyResult, self).addError(test, err)
 
 
80
    def addFailure(self, test, err):
 
 
81
        super(_MyResult, self).addFailure(test, err)
 
 
84
    def addSuccess(self, test):
 
 
86
            self.stream.writeln('OK')
 
 
88
            self.stream.write('~')
 
 
90
        unittest.TestResult.addSuccess(self, test)
 
 
92
    def printErrorList(self, flavour, errors):
 
 
93
        for test, err in errors:
 
 
94
            self.stream.writeln(self.separator1)
 
 
95
            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
 
 
96
            self.stream.writeln(self.separator2)
 
 
97
            self.stream.writeln("%s" % err)
 
 
98
            if hasattr(test, '_get_log'):
 
 
100
                self.stream.writeln('log from this test:')
 
 
101
                print >>self.stream, test._get_log()
 
 
104
class TextTestRunner(unittest.TextTestRunner):
 
 
106
    def _makeResult(self):
 
 
107
        return _MyResult(self.stream, self.descriptions, self.verbosity)
 
 
110
def run_suite(suite, name='test', verbose=False):
 
 
112
    from bzrlib.selftest import FunctionalTestCase
 
 
113
    FunctionalTestCase._TEST_NAME = name
 
 
118
    runner = TextTestRunner(stream=sys.stdout,
 
 
121
    result = runner.run(suite)
 
 
122
    # This is still a little bogus, 
 
 
123
    # but only a little. Folk not using our testrunner will
 
 
124
    # have to delete their temp directories themselves.
 
 
125
    if result.wasSuccessful():
 
 
126
        if FunctionalTestCase.TEST_ROOT:
 
 
127
            shutil.rmtree(FunctionalTestCase.TEST_ROOT) 
 
 
129
        print "Failed tests working directories are in '%s'\n" % FunctionalTestCase.TEST_ROOT
 
 
130
    return result.wasSuccessful()