def testTermcolorTrue(self): """ termcolor=True results in terminal output """ c = Colors(termcolor=True) self.assertTrue(c.termcolor) self.assertTrue(len(c.bold("")) > 0)
def testTermcolorFalse(self): """ termcolor=False results in no terminal output """ c = Colors(termcolor=False) self.assertFalse(c.termcolor) self.assertFalse(len(c.bold("")) > 0)
def testUp(self): """ calling up gives us a non-blank string """ c = Colors() up = c.up() self.assertEqual(type(up), str) self.assertNotEqual(up, '')
def testTermstyleColorsDoNotCrash(self): "termstyle-based colors don't crash and output something" c = Colors(termcolor=True) for func in [c.bold, c.blue, c.green, c.red, c.yellow, c.passing, c.failing, c.error, c.skipped, c.unexpectedSuccess, c.expectedFailure, c.moduleName]: self.assertTrue(len(func("")) > 0) # c.className is a special case c.className("")
def testDisableWindowsTrue(self): """ disable_windows=True: ANSI color codes are present in the stream """ c = Colors(termcolor=True) s = StringIO() gs = GreenStream(s, disable_windows=True) msg = c.red("some colored string") gs.write(msg) self.assertEqual(len(gs.stream.getvalue()), len(msg))
def testHTMLColorsDoNotCrash(self): "termstyle-based colors don't crash and output something" c = Colors(html=True) for func in [c.bold, c.blue, c.green, c.red, c.yellow, c.passing, c.failing, c.error, c.skipped, c.unexpectedSuccess, c.expectedFailure, c.moduleName]: self.assertTrue(len(func("")) > 0, "%r is not producing output" % func) self.assertTrue('span' in func("")) # c.className is a special case c.className("")
def testTermstyleColorsDoNotCrash(self): """ termstyle-based colors don't crash and output something """ c = Colors(termcolor=True) for func in [c.bold, c.blue, c.green, c.red, c.yellow, c.passing, c.failing, c.error, c.skipped, c.unexpectedSuccess, c.expectedFailure, c.moduleName]: self.assertTrue(len(func("")) > 0) # c.className is a special case c.className("")
def testDisableWindowsFalse(self): """ disable_windows=False: Colorama strips ANSI color codes from the stream """ c = Colors(termcolor=True) s = StringIO() gs = GreenStream(s, override_appveyor=True, disable_windows=False) colored_msg = c.red("a") gs.write(colored_msg) import colorama self.assertTrue( issubclass(type(gs.stream), colorama.ansitowin32.StreamWrapper))
def testDisableWindowsFalse(self): """ disable_windows=False: Colorama strips ANSI color codes from the stream """ c = Colors(termcolor=True) s = StringIO() gs = GreenStream(s, override_appveyor=True, disable_windows=False) colored_msg = c.red("a") gs.write(colored_msg) import colorama self.assertTrue(issubclass(type(gs.stream), colorama.ansitowin32.StreamWrapper))
def test_displayStderr(self): """ displayStderr displays captured stderr """ stream = StringIO() noise = "blah blah blah" btr = BaseTestResult(stream, Colors(False)) pt = ProtoTest() btr.stderr_errput[pt] = noise btr.displayStderr(pt) self.assertIn(noise, stream.getvalue())
def __init__(self, args, stream): super(GreenTestResult, self).__init__(stream, Colors(args.termcolor)) self.args = args self.showAll = args.verbose > 1 self.dots = args.verbose == 1 self.verbose = args.verbose self.last_module = '' self.last_class = '' self.failfast = args.failfast self.shouldStop = False self.testsRun = 0 # Individual lists self.errors = [] self.expectedFailures = [] self.failures = [] self.passing = [] self.skipped = [] self.unexpectedSuccesses = [] # Combination of all errors and failures self.all_errors = []
def __init__(self, args, stream): super(GreenTestResult, self).__init__(stream, Colors(args.termcolor)) self.args = args self.showAll = args.verbose > 1 self.dots = args.verbose == 1 self.verbose = args.verbose self.last_module = "" self.last_class = "" self.first_text_output = "" self.failfast = args.failfast self.shouldStop = False self.testsRun = 0 # Individual lists self.errors = [] self.expectedFailures = [] self.failures = [] self.passing = [] self.skipped = [] self.unexpectedSuccesses = [] # Combination of all errors and failures self.all_errors = [] # For exiting non-zero if we don't reach a certain level of coverage self.coverage_percent = None
def __init__(self, stream, descriptions, verbosity, html=False, termcolor=None): """stream, descriptions, and verbosity are as in unittest.runner.TextTestRunner. """ self.stream = stream self.showAll = verbosity > 1 self.dots = verbosity == 1 self.verbosity = verbosity self.descriptions = descriptions self.colors = Colors(termcolor, html) self.last_module = '' self.last_class = '' self.shouldStop = False self.testsRun = 0 # Individual lists self.errors = [] self.expectedFailures = [] self.failures = [] self.passing = [] self.skipped = [] self.unexpectedSuccesses = [] # Combination of all errors and failures self.all_errors = []
def testEnableHTML(self): "html=True causes HTML output" c = Colors(html=True) self.assertEqual(c.bold(''), '<span style="color: rgb(255,255,255);"></span>')
class GreenTestResult(): "Aggregates test results and outputs them to a stream." def __init__(self, stream, descriptions, verbosity, html=False, termcolor=None): """stream, descriptions, and verbosity are as in unittest.runner.TextTestRunner. """ self.stream = stream self.showAll = verbosity > 1 self.dots = verbosity == 1 self.verbosity = verbosity self.descriptions = descriptions self.colors = Colors(termcolor, html) self.last_module = '' self.last_class = '' self.shouldStop = False self.testsRun = 0 # Individual lists self.errors = [] self.expectedFailures = [] self.failures = [] self.passing = [] self.skipped = [] self.unexpectedSuccesses = [] # Combination of all errors and failures self.all_errors = [] def addProtoTestResult(self, protoTestResult): for test, err in protoTestResult.errors: self.addError(test, err) for test, err in protoTestResult.expectedFailures: self.addExpectedFailure(test, err) for test, err in protoTestResult.failures: self.addFailure(test, err) for test in protoTestResult.passing: self.addSuccess(test) for test, reason in protoTestResult.skipped: self.addSkip(test, reason) for test in protoTestResult.unexpectedSuccesses: self.addUnexpectedSuccess(test) def startTestRun(self): "Called once before any tests run" self.startTime = time.time() # Really verbose information if self.colors.html: self.stream.write( '<div style="font-family: Monaco, \'Courier New\', monospace; color: rgb(170,170,170); background: rgb(0,0,0); padding: 14px;">') if self.verbosity > 2: self.stream.writeln(self.colors.bold(pretty_version() + "\n")) def stopTestRun(self): "Called once after all tests have run" self.stopTime = time.time() self.timeTaken = self.stopTime - self.startTime self.printErrors() if self.testsRun: self.stream.writeln() self.stream.writeln("Ran %s test%s in %ss" % (self.colors.bold(str(self.testsRun)), self.testsRun != 1 and "s" or "", self.colors.bold("%.3f" % self.timeTaken))) self.stream.writeln() results = [ (self.errors, 'errors', self.colors.error), (self.expectedFailures, 'expected_failures', self.colors.expectedFailure), (self.failures, 'failures', self.colors.failing), (self.passing, 'passes', self.colors.passing), (self.skipped, 'skips', self.colors.skipped), (self.unexpectedSuccesses, 'unexpected_successes', self.colors.unexpectedSuccess), ] stats = [] for obj_list, name, color_func in results: if obj_list: stats.append("{}={}".format(name, color_func(str(len(obj_list))))) if not stats: self.stream.writeln(self.colors.passing("No Tests Found")) else: grade = self.colors.passing('OK') if self.errors or self.failures: grade = self.colors.failing('FAILED') self.stream.writeln("{} ({})".format(grade, ', '.join(stats))) if self.colors.html: self.stream.writeln('</div>') def startTest(self, test): "Called before the start of each test" self.testsRun += 1 # Get our bearings test = proto_test(test) current_module = test.module current_class = test.class_name # Output if self.showAll: # Module...if it changed. if current_module != self.last_module: self.stream.writeln(self.colors.moduleName(current_module)) # Class...if it changed. if current_class != self.last_class: self.stream.writeln(self.colors.className( self.stream.formatText(current_class, indent=1))) # Test name or description if not self.colors.html: # In the terminal, we will write a placeholder, and then # rewrite it in color after the test has run. self.stream.write( self.colors.bold( self.stream.formatLine( test.getDescription(self.verbosity), indent=2))) self.stream.flush() # Set state for next time if current_module != self.last_module: self.last_module = current_module if current_class != self.last_class: self.last_class = current_class def stopTest(self, test): "Called after the end of each test" def _reportOutcome(self, test, outcome_char, color_func, err=None, reason=''): test = proto_test(test) if self.showAll: # Move the cursor back to the start of the line in terminal mode if not self.colors.html: self.stream.write('\r') # Escape the HTML that may be in the docstring test_description = test.getDescription(self.verbosity) if self.colors.html: test_description = escape(test_description) self.stream.write( color_func( self.stream.formatLine( test_description, indent=2, outcome_char=outcome_char) ) ) if reason: self.stream.write(color_func(' -- ' + reason)) self.stream.writeln() self.stream.flush() elif self.dots: self.stream.write(color_func(outcome_char)) self.stream.flush() def addSuccess(self, test): "Called when a test passed" test = proto_test(test) self.passing.append(test) self._reportOutcome(test, '.', self.colors.passing) def addError(self, test, err): "Called when a test raises an exception" test = proto_test(test) err = proto_error(err) self.errors.append((test, err)) self.all_errors.append((test, self.colors.error, 'Error', err)) self._reportOutcome(test, 'E', self.colors.error, err) def addFailure(self, test, err): "Called when a test fails a unittest assertion" test = proto_test(test) err = proto_error(err) self.failures.append((test, err)) self.all_errors.append((test, self.colors.error, 'Failure', err)) self._reportOutcome(test, 'F', self.colors.failing, err) def addSkip(self, test, reason): test = proto_test(test) "Called when a test is skipped" self.skipped.append((test, reason)) self._reportOutcome( test, 's', self.colors.skipped, reason=reason) def addExpectedFailure(self, test, err): "Called when a test fails, and we expeced the failure" test = proto_test(test) err = proto_error(err) self.expectedFailures.append((test, err)) self._reportOutcome(test, 'x', self.colors.expectedFailure, err) def addUnexpectedSuccess(self, test): "Called when a test passed, but we expected a failure" test = proto_test(test) self.unexpectedSuccesses.append(test) self._reportOutcome(test, 'u', self.colors.unexpectedSuccess) def printErrors(self): "Print a list of all tracebacks from errors and failures" if not self.all_errors: return if self.dots: self.stream.writeln() for (test, color_func, outcome, err) in self.all_errors: # Header Line self.stream.writeln( '\n' + color_func(outcome) + ' in ' + self.colors.bold(test.dotted_name)) # Frame Line relevant_frames = [] for i, frame in enumerate(err.traceback_lines): debug('\n' + '*' * 30 + "Frame {}:".format(i) + '*' * 30 + "\n{}".format(self.colors.yellow(frame)), level = 3) # Ignore useless frames if self.verbosity < 4: if frame.strip() == "Traceback (most recent call last):": continue reindented_lines = [] # If we're in html, space-based indenting needs to be converted. if self.colors.html: for line in frame.split('\n'): frame_indent = 0 while line[:2] == ' ': line = line[2:] frame_indent += 1 line = self.stream.formatLine(line, indent=frame_indent) reindented_lines.append(line) frame = "\n".join(reindented_lines) # Done with this frame, capture it. relevant_frames.append(frame) self.stream.write(''.join(relevant_frames)) def wasSuccessful(self): "Tells whether or not the overall run was successful" return len(self.all_errors) == 0
def testTermcolorAuto(self): """ termcolor=None causes termcolor autodetected and set to True or False """ c = Colors() self.assertTrue(c.termcolor in [True, False])