From: Zac Medico Date: Thu, 10 Dec 2009 00:56:06 +0000 (-0000) Subject: Copy the unittest._TextTestResult class from python-2.6 since it's been X-Git-Tag: v2.1.7.12~16 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=780f07a62b0d873757085593f0a40225507aa0b7;p=portage.git Copy the unittest._TextTestResult class from python-2.6 since it's been removed in python-2.7. (trunk r14963) svn path=/main/branches/2.1.7/; revision=14998 --- diff --git a/pym/portage/tests/__init__.py b/pym/portage/tests/__init__.py index 9a81d22ff..0382a2fc1 100644 --- a/pym/portage/tests/__init__.py +++ b/pym/portage/tests/__init__.py @@ -6,6 +6,7 @@ import sys import time import unittest +from unittest import TestResult from portage import os from portage import _encodings @@ -69,17 +70,26 @@ def getTests(path, base_path): result.append(unittest.TestLoader().loadTestsFromModule(mod)) return result -class TextTestResult(unittest._TextTestResult): +class TextTestResult(TestResult): """ - We need a subclass of unittest._TextTestResult to handle tests with TODO + We need a subclass of unittest.TestResult to handle tests with TODO + Most of this class is copied from the unittest._TextTestResult that's + included with python-2.6 (but not included with python-2.7). This just adds an addTodo method that can be used to add tests that are marked TODO; these can be displayed later by the test runner. """ - + + separator1 = '=' * 70 + separator2 = '-' * 70 + def __init__(self, stream, descriptions, verbosity): - unittest._TextTestResult.__init__( self, stream, descriptions, verbosity ) + unittest.TestResult.__init__(self) + self.stream = stream + self.showAll = verbosity > 1 + self.dots = verbosity == 1 + self.descriptions = descriptions self.todoed = [] def addTodo(self, test, info): @@ -88,14 +98,58 @@ class TextTestResult(unittest._TextTestResult): self.stream.writeln("TODO") elif self.dots: self.stream.write(".") - + def printErrors(self): if self.dots or self.showAll: self.stream.writeln() self.printErrorList('ERROR', self.errors) self.printErrorList('FAIL', self.failures) self.printErrorList('TODO', self.todoed) - + + def getDescription(self, test): + if self.descriptions: + return test.shortDescription() or str(test) + else: + return str(test) + + def startTest(self, test): + TestResult.startTest(self, test) + if self.showAll: + self.stream.write(self.getDescription(test)) + self.stream.write(" ... ") + self.stream.flush() + + def addSuccess(self, test): + TestResult.addSuccess(self, test) + if self.showAll: + self.stream.writeln("ok") + elif self.dots: + self.stream.write('.') + self.stream.flush() + + def addError(self, test, err): + TestResult.addError(self, test, err) + if self.showAll: + self.stream.writeln("ERROR") + elif self.dots: + self.stream.write('E') + self.stream.flush() + + def addFailure(self, test, err): + TestResult.addFailure(self, test, err) + if self.showAll: + self.stream.writeln("FAIL") + elif self.dots: + self.stream.write('F') + self.stream.flush() + + 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 TestCase(unittest.TestCase): """ We need a way to mark a unit test as "ok to fail"