runner.py 7.57 KB
Newer Older
1 2 3 4
"""Running tests"""

import sys
import time
5
import warnings
6 7

from . import result
8
from .signals import registerResult
9

10 11
__unittest = True

12 13 14 15 16 17 18

class _WritelnDecorator(object):
    """Used to decorate file-like objects with a handy 'writeln' method"""
    def __init__(self,stream):
        self.stream = stream

    def __getattr__(self, attr):
19 20
        if attr in ('stream', '__getstate__'):
            raise AttributeError(attr)
21 22 23 24 25 26 27 28
        return getattr(self.stream,attr)

    def writeln(self, arg=None):
        if arg:
            self.write(arg)
        self.write('\n') # text-mode streams translate to \r\n if needed


29
class TextTestResult(result.TestResult):
30 31 32 33 34 35 36 37
    """A test result class that can print formatted text results to a stream.

    Used by TextTestRunner.
    """
    separator1 = '=' * 70
    separator2 = '-' * 70

    def __init__(self, stream, descriptions, verbosity):
38
        super(TextTestResult, self).__init__(stream, descriptions, verbosity)
39 40 41 42 43 44
        self.stream = stream
        self.showAll = verbosity > 1
        self.dots = verbosity == 1
        self.descriptions = descriptions

    def getDescription(self, test):
45 46 47
        doc_first_line = test.shortDescription()
        if self.descriptions and doc_first_line:
            return '\n'.join((str(test), doc_first_line))
48 49 50 51
        else:
            return str(test)

    def startTest(self, test):
52
        super(TextTestResult, self).startTest(test)
53 54 55 56 57 58
        if self.showAll:
            self.stream.write(self.getDescription(test))
            self.stream.write(" ... ")
            self.stream.flush()

    def addSuccess(self, test):
59
        super(TextTestResult, self).addSuccess(test)
60 61 62 63 64 65 66
        if self.showAll:
            self.stream.writeln("ok")
        elif self.dots:
            self.stream.write('.')
            self.stream.flush()

    def addError(self, test, err):
67
        super(TextTestResult, self).addError(test, err)
68 69 70 71 72 73 74
        if self.showAll:
            self.stream.writeln("ERROR")
        elif self.dots:
            self.stream.write('E')
            self.stream.flush()

    def addFailure(self, test, err):
75
        super(TextTestResult, self).addFailure(test, err)
76 77 78 79 80 81 82
        if self.showAll:
            self.stream.writeln("FAIL")
        elif self.dots:
            self.stream.write('F')
            self.stream.flush()

    def addSkip(self, test, reason):
83
        super(TextTestResult, self).addSkip(test, reason)
84 85 86 87 88 89 90
        if self.showAll:
            self.stream.writeln("skipped {0!r}".format(reason))
        elif self.dots:
            self.stream.write("s")
            self.stream.flush()

    def addExpectedFailure(self, test, err):
91
        super(TextTestResult, self).addExpectedFailure(test, err)
92 93 94 95 96 97 98
        if self.showAll:
            self.stream.writeln("expected failure")
        elif self.dots:
            self.stream.write("x")
            self.stream.flush()

    def addUnexpectedSuccess(self, test):
99
        super(TextTestResult, self).addUnexpectedSuccess(test)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        if self.showAll:
            self.stream.writeln("unexpected success")
        elif self.dots:
            self.stream.write("u")
            self.stream.flush()

    def printErrors(self):
        if self.dots or self.showAll:
            self.stream.writeln()
        self.printErrorList('ERROR', self.errors)
        self.printErrorList('FAIL', self.failures)

    def printErrorList(self, flavour, errors):
        for test, err in errors:
            self.stream.writeln(self.separator1)
            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
            self.stream.writeln(self.separator2)
            self.stream.writeln("%s" % err)


class TextTestRunner(object):
    """A test runner class that displays results in textual form.

    It prints out the names of tests as they are run, errors as they
    occur, and a summary of the results at the end of the test run.
    """
126 127
    resultclass = TextTestResult

128
    def __init__(self, stream=None, descriptions=True, verbosity=1,
129 130 131 132 133 134 135
                 failfast=False, buffer=False, resultclass=None, warnings=None,
                 *, tb_locals=False):
        """Construct a TextTestRunner.

        Subclasses should accept **kwargs to ensure compatibility as the
        interface changes.
        """
136 137
        if stream is None:
            stream = sys.stderr
138 139 140
        self.stream = _WritelnDecorator(stream)
        self.descriptions = descriptions
        self.verbosity = verbosity
141
        self.failfast = failfast
Benjamin Peterson's avatar
Benjamin Peterson committed
142
        self.buffer = buffer
143
        self.tb_locals = tb_locals
144
        self.warnings = warnings
145 146
        if resultclass is not None:
            self.resultclass = resultclass
147 148

    def _makeResult(self):
149
        return self.resultclass(self.stream, self.descriptions, self.verbosity)
150 151 152 153

    def run(self, test):
        "Run the given test case or test suite."
        result = self._makeResult()
154
        registerResult(result)
155
        result.failfast = self.failfast
Benjamin Peterson's avatar
Benjamin Peterson committed
156
        result.buffer = self.buffer
157
        result.tb_locals = self.tb_locals
158 159 160 161 162 163 164 165 166 167 168 169
        with warnings.catch_warnings():
            if self.warnings:
                # if self.warnings is set, use it to filter all the warnings
                warnings.simplefilter(self.warnings)
                # if the filter is 'default' or 'always', special-case the
                # warnings from the deprecated unittest methods to show them
                # no more than once per module, because they can be fairly
                # noisy.  The -Wd and -Wa flags can be used to bypass this
                # only when self.warnings is None.
                if self.warnings in ['default', 'always']:
                    warnings.filterwarnings('module',
                            category=DeprecationWarning,
170
                            message=r'Please use assert\w+ instead.')
171 172 173 174 175 176 177 178 179 180 181
            startTime = time.time()
            startTestRun = getattr(result, 'startTestRun', None)
            if startTestRun is not None:
                startTestRun()
            try:
                test(result)
            finally:
                stopTestRun = getattr(result, 'stopTestRun', None)
                if stopTestRun is not None:
                    stopTestRun()
            stopTime = time.time()
182 183
        timeTaken = stopTime - startTime
        result.printErrors()
184 185
        if hasattr(result, 'separator2'):
            self.stream.writeln(result.separator2)
186 187 188 189
        run = result.testsRun
        self.stream.writeln("Ran %d test%s in %.3fs" %
                            (run, run != 1 and "s" or "", timeTaken))
        self.stream.writeln()
190 191 192 193 194 195 196 197

        expectedFails = unexpectedSuccesses = skipped = 0
        try:
            results = map(len, (result.expectedFailures,
                                result.unexpectedSuccesses,
                                result.skipped))
        except AttributeError:
            pass
198 199
        else:
            expectedFails, unexpectedSuccesses, skipped = results
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        infos = []
        if not result.wasSuccessful():
            self.stream.write("FAILED")
            failed, errored = len(result.failures), len(result.errors)
            if failed:
                infos.append("failures=%d" % failed)
            if errored:
                infos.append("errors=%d" % errored)
        else:
            self.stream.write("OK")
        if skipped:
            infos.append("skipped=%d" % skipped)
        if expectedFails:
            infos.append("expected failures=%d" % expectedFails)
        if unexpectedSuccesses:
            infos.append("unexpected successes=%d" % unexpectedSuccesses)
        if infos:
            self.stream.writeln(" (%s)" % (", ".join(infos),))
        else:
            self.stream.write("\n")
        return result