Skip to content

colortest

ontopy.colortest

Print tests in colors.

Adapted from https://github.com/meshy/colour-runner by Charlie Denton License: MIT

ColourTextTestResult

Bases: TestResult

A test result class that prints colour formatted text results to a stream.

Based on https://github.com/python/cpython/blob/3.3/Lib/unittest/runner.py

Source code in ontopy/colortest.py
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class ColourTextTestResult(TestResult):
    """
    A test result class that prints colour formatted text results to a stream.

    Based on https://github.com/python/cpython/blob/3.3/Lib/unittest/runner.py
    """

    formatter = formatters.Terminal256Formatter()  # pylint: disable=no-member
    lexer = Lexer()
    separator1 = "=" * 70
    separator2 = "-" * 70
    indent = " " * 4
    # if `checkmode` is true, simplified output will be generated with
    # no traceback
    checkmode = False
    _terminal = Terminal()
    colours = {
        None: str,
        "error": _terminal.bold_red,
        "expected": _terminal.blue,
        # "fail": _terminal.bold_yellow,
        "fail": _terminal.bold_magenta,
        "skip": str,
        "success": _terminal.green,
        "title": _terminal.blue,
        "unexpected": _terminal.bold_red,
    }

    _test_class = None

    def __init__(self, stream, descriptions, verbosity):
        super().__init__(stream, descriptions, verbosity)
        self.stream = stream
        self.show_all = verbosity > 1
        self.dots = verbosity == 1
        self.descriptions = descriptions

    def getShortDescription(self, test):
        doc_first_line = test.shortDescription()
        if self.descriptions and doc_first_line:
            return self.indent + doc_first_line
        return self.indent + test._testMethodName

    def getLongDescription(self, test):
        doc_first_line = test.shortDescription()
        if self.descriptions and doc_first_line:
            return "\n".join((str(test), doc_first_line))
        return str(test)

    def getClassDescription(self, test):
        test_class = test.__class__
        doc = test_class.__doc__
        if self.descriptions and doc:
            return doc.split("\n")[0].strip()
        return strclass(test_class)

    def startTest(self, test):
        super().startTest(test)
        pos = 0
        if self.show_all:
            if self._test_class != test.__class__:
                self._test_class = test.__class__
                title = self.getClassDescription(test)
                self.stream.writeln(self.colours["title"](title))
            descr = self.getShortDescription(test)
            self.stream.write(descr)
            pos += len(descr)
            self.stream.write(" " * (70 - pos))
            # self.stream.write(' ' * (self._terminal.width - 10 - pos))
            # self.stream.write(' ... ')
            self.stream.flush()

    def printResult(self, short, extended, colour_key=None):
        colour = self.colours[colour_key]
        if self.show_all:
            self.stream.writeln(colour(extended))
        elif self.dots:
            self.stream.write(colour(short))
            self.stream.flush()

    def addSuccess(self, test):
        super().addSuccess(test)
        self.printResult(".", "ok", "success")

    def addError(self, test, err):
        super().addError(test, err)
        self.printResult("E", "ERROR", "error")

    def addFailure(self, test, err):
        super().addFailure(test, err)
        self.printResult("F", "FAIL", "fail")

    def addSkip(self, test, reason):
        super().addSkip(test, reason)
        if self.checkmode:
            self.printResult("s", "skipped", "skip")
        else:
            self.printResult("s", f"skipped {reason!r}", "skip")

    def addExpectedFailure(self, test, err):
        super().addExpectedFailure(test, err)
        self.printResult("x", "expected failure", "expected")

    def addUnexpectedSuccess(self, test):
        super().addUnexpectedSuccess(test)
        self.printResult("u", "unexpected success", "unexpected")

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

    def printErrorList(self, flavour, errors):
        colour = self.colours[flavour.lower()]

        for test, err in errors:
            if self.checkmode and flavour == "FAIL":
                self.stream.writeln(self.separator1)
                title = f"{flavour}: {test.shortDescription()}"
                self.stream.writeln(colour(title))
                self.stream.writeln(str(test))
                if self.show_all:
                    self.stream.writeln(self.separator2)
                    lines = str(err).split("\n")
                    i = 1
                    for line in lines[1:]:
                        if line.startswith(" "):
                            i += 1
                        else:
                            break
                    self.stream.writeln(
                        highlight(
                            "\n".join(lines[i:]), self.lexer, self.formatter
                        )
                    )
            else:
                self.stream.writeln(self.separator1)
                title = f"{flavour}: {self.getLongDescription(test)}"
                self.stream.writeln(colour(title))
                self.stream.writeln(self.separator2)
                self.stream.writeln(highlight(err, self.lexer, self.formatter))

ColourTextTestRunner

Bases: TextTestRunner

A test runner that uses colour in its output.

Source code in ontopy/colortest.py
164
165
166
167
168
169
class ColourTextTestRunner(
    TextTestRunner
):  # pylint: disable=too-few-public-methods
    """A test runner that uses colour in its output."""

    resultclass = ColourTextTestResult
Back to top