def test_wasSuccessful(self): """ wasSuccessful returns what we expect. """ self.args.verbose = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) self.assertEqual(gtr.wasSuccessful(), False) gtr.passing.append("anything") self.assertEqual(gtr.wasSuccessful(), True) gtr.all_errors.append("anything") self.assertEqual(gtr.wasSuccessful(), False)
def _outputFromTest(self, args): class FakeCase(unittest.TestCase): def runTest(self): pass gtr = GreenTestResult(args, GreenStream(self.stream)) gtr.startTestRun() gtr.startTest(FakeCase()) gtr.stopTestRun() output = self.stream.getvalue() return output.split("\n")
def test_printErrorsSkipreport(self): """ printErrors() prints the skip report """ self.args.verbose = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) pt = MyProtoTest() reason = "dog ate homework" gtr.addSkip(pt, reason) gtr.printErrors() self.assertIn(reason, self.stream.getvalue())
def setUp(self): self._destination = StringIO() self._test_results = GreenTestResult(default_args, GreenStream(StringIO())) self._test_results.timeTaken = 4.06 self._adapter = JUnitXML() self._test = ProtoTest() self._test.module = "my_module" self._test.class_name = "MyClass" self._test.method_name = "my_method" self._test.test_time = "0.005"
def test_printErrorsStdout(self): """ printErrors() prints out the captured stdout. """ self.args.verbose = 1 self.args.termcolor = False gtr = GreenTestResult(self.args, GreenStream(self.stream)) pt = MyProtoTest() output = 'this is what the test spit out to stdout' gtr.recordStdout(pt, output) gtr.addSuccess(pt) gtr.printErrors() self.assertIn(output, self.stream.getvalue())
def _outputFromVerboseTest(self): """ Start a test with verbose = 2 and get its output. """ class FakeCase(unittest.TestCase): def runTest(self): pass self.args.verbose = 2 gtr = GreenTestResult(self.args, GreenStream(self.stream)) tc = FakeCase() gtr.startTest(tc) output = self.stream.getvalue() return output.split('\n')
def test_stopTestRun(self, mock_printErrors): """ We ignore coverage's error about not having anything to cover. """ self.args.cov = MagicMock() self.args.cov.stop = MagicMock( side_effect=CoverageException('Different Exception')) self.args.run_coverage = True gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.startTestRun() self.assertRaises(CoverageException, gtr.stopTestRun) self.args.cov.stop = MagicMock( side_effect=CoverageException('No data to report'))
def test_printErrorsStderrQuietStdoutOnSuccess(self): """ printErrors() prints out the captured stdout except when quiet_stdout is set to True for successful tests. """ self.args.quiet_stdout = True gtr = GreenTestResult(self.args, GreenStream(self.stream)) pt = MyProtoTest() output = 'this is what the test should not spit out to stdout' gtr.recordStderr(pt, output) gtr.addSuccess(pt) gtr.printErrors() self.assertNotIn(output, self.stream.getvalue())
def test_failfastAddFailure(self): """ addFailure triggers failfast when it is set """ self.args.failfast = True gtr = GreenTestResult(self.args, GreenStream(self.stream)) self.assertEqual(gtr.failfast, True) try: raise Exception except: err = sys.exc_info() self.assertEqual(gtr.shouldStop, False) gtr.addFailure(MyProtoTest(), proto_error(err)) self.assertEqual(gtr.shouldStop, True)
def test_reportOutcomeVerbose(self): """ _reportOutcome contains output we expect in verbose mode """ self.args.verbose = 2 def isatty(): return True gs = GreenStream(self.stream) gs.isatty = isatty gtr = GreenTestResult(self.args, gs) r = 'a fake reason' t = MagicMock() t.__str__.return_value = 'junk' gtr._reportOutcome(t, '.', lambda x: x, None, r) self.assertIn(r, self.stream.getvalue())
def test_printErrorsNoTracebacks(self): """ printErrors() omits tracebacks for failures and errors when no_tracebacks is True """ self.args.no_tracebacks = True try: raise Exception except: err = sys.exc_info() gtr = GreenTestResult(self.args, GreenStream(self.stream)) pt = MyProtoTest() gtr.addError(pt, proto_error(err)) gtr.printErrors() self.assertNotIn("Exception", self.stream.getvalue())
def test_reportOutcomeCursorUp(self): """ _reportOutcome moves the cursor up when it needs to """ self.args.verbose = 2 def isatty(): return True gs = GreenStream(self.stream) gs.isatty = isatty gtr = GreenTestResult(self.args, gs) r = 'a fake reason' t = MagicMock() t.__str__.return_value = 'x' * 1000 gtr._reportOutcome(t, '.', lambda x: x, None, r) self.assertIn(r, self.stream.getvalue()) self.assertLess(len(self.stream.getvalue()), 2000)
def test_addProtoTestResult(self): """ addProtoTestResult adds the correct things to the correct places. """ ptr = ProtoTestResult() err_t = proto_test(MagicMock()) try: raise Exception except: err_e = proto_error(sys.exc_info()) ptr.addError(err_t, err_e) ef_t = proto_test(MagicMock()) try: raise Exception except: ef_e = proto_error(sys.exc_info()) ptr.addExpectedFailure(ef_t, ef_e) fail_t = proto_test(MagicMock()) try: raise Exception except: fail_e = proto_error(sys.exc_info()) ptr.addFailure(fail_t, fail_e) pass_t = proto_test(MagicMock()) ptr.addSuccess(pass_t) skip_t = proto_test(MagicMock()) skip_r = proto_test(MagicMock()) ptr.addSkip(skip_t, skip_r) us_t = proto_test(MagicMock()) ptr.addUnexpectedSuccess(us_t) self.args.verbose = 0 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.addProtoTestResult(ptr) self.assertEqual(gtr.errors, [(err_t, err_e)]) self.assertEqual(gtr.expectedFailures, [(ef_t, ef_e)]) self.assertEqual(gtr.failures, [(fail_t, fail_e)]) self.assertEqual(gtr.passing, [pass_t]) self.assertEqual(gtr.skipped, [(skip_t, skip_r)]) self.assertEqual(gtr.unexpectedSuccesses, [us_t])
def test_printErrorsVerbose3(self): """ printErrors() looks correct in verbose=3 mode. """ try: raise Exception except: err = sys.exc_info() self.args.verbose = 3 self.args.termcolor = False gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.addError(MyProtoTest(), proto_error(err)) gtr.printErrors() self.assertIn('\n\n', self.stream.getvalue()) self.assertIn('my_module.MyClass.myMethod', self.stream.getvalue()) self.assertIn('test_printErrorsVerbose3', self.stream.getvalue()) self.assertIn('raise Exception', self.stream.getvalue()) self.assertIn('Error', self.stream.getvalue())
def test_printErrors_Py2Unicode(self): """ printErrors() doesn't crash in Python 2 when tracebacks contain unicode """ try: raise Exception(u'Das Böse ist immer und überall') except: err = sys.exc_info() self.args.verbose = 1 self.args.termcolor = False gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.addError(MyProtoTest(), proto_error(err)) gtr.printErrors() # We shouldn't hit an exception here self.assertIn('\n\n', self.stream.getvalue()) self.assertIn('my_module.MyClass.myMethod', self.stream.getvalue()) self.assertIn('raise Exception', self.stream.getvalue()) self.assertIn('Error', self.stream.getvalue()) self.assertIn('Böse', self.stream.getvalue())
def test_printErrorsDots(self): """ printErrors() looks correct in verbose=1 (dots) mode. """ try: raise Exception except: err = sys.exc_info() self.args.verbose = 1 self.args.termcolor = False gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.addError(MyProtoTest(), proto_error(err)) gtr.printErrors() self.assertIn("\n\n", self.stream.getvalue()) self.assertIn("my_module.MyClass.myMethod", self.stream.getvalue()) self.assertIn("test_printErrorsDots", self.stream.getvalue()) self.assertIn("raise Exception", self.stream.getvalue()) self.assertIn("Error", self.stream.getvalue())
def test_stopTestRun(self, mock_printErrors): """ We ignore coverage's error about not having anything to cover. """ try: from coverage.misc import CoverageException except: self.skipTest("Coverage needs to be installed for this test.") self.args.cov = MagicMock() self.args.cov.stop = MagicMock( side_effect=CoverageException('Different Exception')) self.args.run_coverage = True gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.startTestRun() self.assertRaises(CoverageException, gtr.stopTestRun) self.args.cov.stop = MagicMock( side_effect=CoverageException('No data to report'))
def test_printErrorsStdoutQuietStdoutOnError(self): """ printErrors() prints out the captured stdout except when quiet_stdout is set to True for successful tests, but here we are on a failing test. """ self.args.quiet_stdout = True try: raise Exception except: err = sys.exc_info() gtr = GreenTestResult(self.args, GreenStream(self.stream)) pt = MyProtoTest() output = 'this is what the test should spit out to stdout' gtr.recordStdout(pt, output) gtr.addError(pt, proto_error(err)) gtr.printErrors() self.assertIn(output, self.stream.getvalue())
def test_reportOutcomeVerbose(self, mock_proto_test): """ _reportOutcome contains output we expect in verbose mode. """ mockProtoTest = MagicMock() mockProtoTest.getDescription.return_value = "a description" mock_proto_test.return_value = mockProtoTest self.args.verbose = 2 def isatty(): return True gs = GreenStream(self.stream) gs.isatty = isatty gtr = GreenTestResult(self.args, gs) r = "a fake reason" t = MagicMock() t.__str__.return_value = "junk" gtr._reportOutcome(t, ".", lambda x: x, None, r) self.assertIn(r, self.stream.getvalue())
def test_reportOutcomeCursorUp(self, mock_proto_test): """ _reportOutcome moves the cursor up when it needs to. """ mockProtoTest = MagicMock() mockProtoTest.getDescription.return_value = "a description" mock_proto_test.return_value = mockProtoTest self.args.verbose = 2 def isatty(): return True gs = GreenStream(self.stream) gs.isatty = isatty gtr = GreenTestResult(self.args, gs) r = "a fake reason" t = MagicMock() t.__str__.return_value = "x" * 1000 gtr._reportOutcome(t, ".", lambda x: x, None, r) self.assertIn(r, self.stream.getvalue()) self.assertLess(len(self.stream.getvalue()), 2000)
def test_tryRecordingStdoutStderr(self): """ Recording stdout and stderr works correctly. """ gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.recordStdout = MagicMock() gtr.recordStderr = MagicMock() output = 'apple' test1 = MagicMock() ptr1 = MagicMock() ptr1.stdout_output = {test1: output} ptr1.stderr_errput = {} errput = 'banana' test2 = MagicMock() ptr2 = MagicMock() ptr2.stdout_output = {} ptr2.stderr_errput = {test2: errput} gtr.tryRecordingStdoutStderr(test1, ptr1) gtr.recordStdout.assert_called_with(test1, output) gtr.tryRecordingStdoutStderr(test2, ptr2) gtr.recordStderr.assert_called_with(test2, errput)
def run(suite, stream, args, testing=False): """ Run the given test case or test suite with the specified arguments. Any args.stream passed in will be wrapped in a GreenStream """ if not issubclass(GreenStream, type(stream)): stream = GreenStream(stream, disable_windows=args.disable_windows) result = GreenTestResult(args, stream) # Note: Catching SIGINT isn't supported by Python on windows (python # "WONTFIX" issue 18040) installHandler() registerResult(result) with warnings.catch_warnings(): if args.warnings: # pragma: no cover # if args.warnings is set, use it to filter all the warnings warnings.simplefilter(args.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 args.warnings is None. if args.warnings in ['default', 'always']: warnings.filterwarnings( 'module', category=DeprecationWarning, message='Please use assert\w+ instead.') result.startTestRun() pool = LoggingDaemonlessPool( processes=args.processes or None, initializer=InitializerOrFinalizer(args.initializer), finalizer=InitializerOrFinalizer(args.finalizer)) manager = multiprocessing.Manager() targets = [(target, manager.Queue()) for target in toParallelTargets(suite, args.targets)] if targets: for index, (target, queue) in enumerate(targets): if args.run_coverage: coverage_number = index + 1 else: coverage_number = None debug("Sending {} to runner {}".format(target, poolRunner)) pool.apply_async( poolRunner, (target, queue, coverage_number, args.omit_patterns)) pool.close() for target, queue in targets: abort = False while True: msg = queue.get() # Sentinel value, we're done if not msg: break else: # Result guaranteed after this message, we're # currently waiting on this test, so print out # the white 'processing...' version of the output result.startTest(msg) proto_test_result = queue.get() result.addProtoTestResult(proto_test_result) if result.shouldStop: abort = True break if abort: break pool.close() pool.join() result.stopTestRun() removeResult(result) return result
def run(suite, stream, args, testing=False): """ Run the given test case or test suite with the specified arguments. Any args.stream passed in will be wrapped in a GreenStream """ # check if the kubefwd is running, then stop the run if check_kubefwd_running(): return GreenTestResult(args, stream) if not issubclass(GreenStream, type(stream)): stream = GreenStream( stream, disable_windows=args.disable_windows, disable_unidecode=args.disable_unidecode, ) result = GreenTestResult(args, stream) # Note: Catching SIGINT isn't supported by Python on windows (python # "WONTFIX" issue 18040) installHandler() registerResult(result) with warnings.catch_warnings(): if args.warnings: # pragma: no cover # if args.warnings is set, use it to filter all the warnings warnings.simplefilter(args.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 args.warnings is None. if args.warnings in ["default", "always"]: warnings.filterwarnings( "module", category=DeprecationWarning, message="Please use assert\w+ instead.", ) result.startTestRun() # The call to toParallelTargets needs to happen before pool stuff so we can crash if there # are, for example, syntax errors in the code to be loaded. parallel_targets = toParallelTargets(suite, args.targets) pool = LoggingDaemonlessPool( processes=args.processes or None, initializer=InitializerOrFinalizer(args.initializer), finalizer=InitializerOrFinalizer(args.finalizer), ) manager = multiprocessing.Manager() targets = [(target, manager.Queue()) for target in parallel_targets] if targets: for index, (target, queue) in enumerate(targets): if args.run_coverage: coverage_number = index + 1 else: coverage_number = None debug("Sending {} to poolRunner {}".format(target, poolRunner)) pool.apply_async( poolRunner, ( target, queue, coverage_number, args.omit_patterns, args.cov_config_file, ), ) pool.close() for target, queue in targets: abort = False while True: msg = queue.get() # Sentinel value, we're done if not msg: debug("runner.run(): received sentinal, breaking.", 3) break else: debug("runner.run(): start test: {}".format(msg)) # Result guaranteed after this message, we're # currently waiting on this test, so print out # the white 'processing...' version of the output result.startTest(msg) proto_test_result = queue.get() debug( "runner.run(): received proto test result: {}". format(str(proto_test_result)), 3, ) result.addProtoTestResult(proto_test_result) if result.shouldStop: debug("runner.run(): shouldStop encountered, breaking", 3) abort = True break if abort: break pool.close() pool.join() result.stopTestRun() removeResult(result) return result
def setUp(self): self.stream = StringIO() self.args = copy.deepcopy(default_args) self.args.verbose = 0 self.gtr = GreenTestResult(self.args, GreenStream(self.stream)) self.gtr._reportOutcome = MagicMock()