1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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
|
# This module mimics unittest's default main with --verbose, but reduces
# the output by not showing successful tests.
#
# stdout/stderr are redirected into the same stream (stdout). The name of the
# test and its description (if any) are printed only if the test fails or the
# tests causes some printout. For this, the module defines a stream with
# 'preambule' that is, a string that is printed before the next print.
# The preambule is set at the beginning at each test function.
import os
import sys
from io import IOBase
from unittest import TestResult, TextTestRunner
from unittest.main import main
__unittest = True
if os.getenv("CI"):
# Remove the current path to prevent tox from importing sources instead of
# installed files (with compiled binaries)
sys.path.remove(os.path.split(__file__)[0])
class PreambuleStream(IOBase):
def __init__(self):
super().__init__()
self.preambule = ""
self.line_before_msg = False
def set_preambule(self, preambule):
self.preambule = preambule
def writable(self):
return True
def writelines(self, lines):
return self.write("".join(lines))
def write(self, s):
if self.preambule:
_stdout.write("\n" + self.preambule + "\n")
self.preambule = ""
self.line_before_msg = True
return _stdout.write(s)
def write_msg(self, s):
if self.line_before_msg:
_stdout.write("\n")
_stdout.write(self.preambule + " ... " + s)
self.line_before_msg = False
self.preambule = ""
def flush(self):
_stdout.flush()
class QuietTestResult(TestResult):
separator1 = '=' * 70
separator2 = '-' * 70
def startTest(self, test):
super().startTest(test)
sys.stdout.set_preambule(self.getDescription(test))
def stopTest(self, test):
super().stopTest(test)
sys.stdout.set_preambule("")
@staticmethod
def getDescription(test):
doc_first_line = test.shortDescription()
if doc_first_line:
return '\n'.join((str(test), doc_first_line))
else:
return str(test)
def addError(self, test, err):
super().addError(test, err)
sys.stdout.write_msg("ERROR\n")
def addFailure(self, test, err):
super().addError(test, err)
sys.stdout.write_msg("FAIL\n")
def printErrors(self):
sys.stdout.set_preambule("")
print()
self.printErrorList('ERROR', self.errors)
self.printErrorList('FAIL', self.failures)
def printErrorList(self, flavour, errors):
for test, err in errors:
print(self.separator1)
print("%s: %s" % (flavour,self.getDescription(test)))
print(self.separator2)
print("%s" % err)
_stdout = sys.stdout
sys.stderr = sys.stdout = PreambuleStream()
testRunner = TextTestRunner(
resultclass=QuietTestResult,
stream=sys.stdout,
verbosity=2)
main(module=None, testRunner=testRunner)
|