def test_wasSuccessful(self): "wasSuccessful returns what we expect" self.args.verbose = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) self.assertEqual(gtr.wasSuccessful(), True) gtr.all_errors.append('anything') self.assertEqual(gtr.wasSuccessful(), False)
def test_reportOutcome(self): """ _reportOutcome contains output we expect """ gtr = GreenTestResult(GreenStream(self.stream), None, 1) gtr._reportOutcome(None, '.', lambda x: x) self.assertIn('.', self.stream.getvalue())
def test_wasSuccessful_coverageFails(self): """ wasSuccessful fails if minimum coverage is not met """ self.args.minimum_coverage = 50 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.coverage_percent = 49 self.assertEqual(gtr.wasSuccessful(), False)
def test_wasSuccessful_unexpectedSuccesses(self): """ wasSuccessful returns what we expect when we only have unexpectedSuccesses """ self.args.verbose = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.unexpectedSuccesses.append('anything') self.assertEqual(gtr.wasSuccessful(), True)
def test_wasSuccessful_skipped(self): """ wasSuccessful returns what we expect when we only have skipped tests """ self.args.verbose = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.skipped.append("anything") self.assertEqual(gtr.wasSuccessful(), True)
def test_reportOutcome(self): """ _reportOutcome contains output we expect. """ self.args.verbose = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr._reportOutcome(None, '.', lambda x: x) self.assertIn('.', self.stream.getvalue())
def test_reportOutcomeVerbose(self): "_reportOutcome contains output we expect in verbose mode" gtr = GreenTestResult(GreenStream(self.stream), None, 2) r = 'a fake reason' t = MagicMock() t.__str__.return_value = 'junk' gtr._reportOutcome(t, '.', lambda x: x, None, r) self.assertTrue(r in self.stream.getvalue())
def test_wasSuccessful_coverageSucceeds(self): """ wasSuccessful succeds if minimum coverage is met """ self.args.minimum_coverage = 50 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.passing.append("anything") gtr.coverage_percent = 60 self.assertEqual(gtr.wasSuccessful(), True)
def test_wasSuccessful_coverageSucceeds(self): """ wasSuccessful succeds if minimum coverage is met """ self.args.minimum_coverage = 50 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.passing.append('anything') gtr.coverage_percent = 60 self.assertEqual(gtr.wasSuccessful(), True)
def test_wasSuccessful(self): """ wasSuccessful returns what we expect """ self.args.verbose = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) self.assertEqual(gtr.wasSuccessful(), True) gtr.all_errors.append('anything') self.assertEqual(gtr.wasSuccessful(), False)
def test_stopTestRun_singular_process_message(self): """ StopTestRun adds correct summary when one process is used """ self.args.processes = 1 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.startTestRun() gtr.stopTestRun() self.assertIn("using 1 process\n", self.stream.getvalue())
def test_stopTestRun_processes_message(self): """ StopTestRun adds number of processes used to summary """ self.args.processes = 4 gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.startTestRun() gtr.stopTestRun() self.assertIn("using 4 processes\n", self.stream.getvalue())
def setUp(self): self._destination = StringIO() self._test_results = GreenTestResult(default_args, GreenStream(StringIO())) self._adapter = JUnitXML() self._test = ProtoTest() self._test.module = "my_module" self._test.class_name = "MyClass" self._test.method_name = "my_method"
def test_failfastAddUnexpectedSuccess(self): """ addUnexpectedSuccess no longer triggers failfast when it is set """ self.args.failfast = True gtr = GreenTestResult(self.args, GreenStream(self.stream)) self.assertEqual(gtr.failfast, True) self.assertEqual(gtr.shouldStop, False) gtr.addUnexpectedSuccess(MyProtoTest()) self.assertEqual(gtr.shouldStop, False)
def test_failfastAddUnexpectedSuccess(self): """ addUnexpectedSuccess triggers failfast when it is set """ self.args.failfast = True gtr = GreenTestResult(self.args, GreenStream(self.stream)) self.assertEqual(gtr.failfast, True) self.assertEqual(gtr.shouldStop, False) gtr.addUnexpectedSuccess(MyProtoTest()) self.assertEqual(gtr.shouldStop, True)
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 _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_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_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 test_tryRecordingStdoutStderr_SubTest(self): """ Recording stdout and stderr works correctly for failed/errored SubTests. """ gtr = GreenTestResult(self.args, GreenStream(self.stream)) gtr.recordStdout = MagicMock() gtr.recordStderr = MagicMock() output = "apple" test1 = MagicMock() test1.dotted_name = "test 1" subtest1 = MagicMock() subtest1.dotted_name = "test 1: the subtest" subtest1.class_name = "SubTest" ptr1 = MagicMock() ptr1.stdout_output = {test1: output} ptr1.stderr_errput = {} errput = "banana" test2 = MagicMock() test2.dotted_name = "test 2" subtest2 = MagicMock() subtest2.dotted_name = "test 2: subtests are annoying" subtest2.class_name = "SubTest" ptr2 = MagicMock() ptr2.stdout_output = {} ptr2.stderr_errput = {test2: errput} gtr.tryRecordingStdoutStderr(subtest1, ptr1, err=True) gtr.recordStdout.assert_called_with(subtest1, output) gtr.tryRecordingStdoutStderr(subtest2, ptr2, err=True) gtr.recordStderr.assert_called_with(subtest2, errput)
def test_printErrorsDots(self): "printErrors() looks correct in verbose=1 (dots) mode" try: raise Exception except: err = sys.exc_info() gtr = GreenTestResult(GreenStream(self.stream), None, 1) test = MagicMock() gtr.addError(test, proto_error(err)) gtr.printErrors() self.assertTrue('\n\n' in self.stream.getvalue()) self.assertTrue('test_printErrorsDots' in self.stream.getvalue()) self.assertTrue('raise Exception' in self.stream.getvalue()) self.assertTrue('Error' in self.stream.getvalue())
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_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_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_reportOutcomeVerboseHTML(self): "html=True causes _reportOutcome() to escape HTML in docstrings" gtr = GreenTestResult(GreenStream(self.stream), None, 3) gtr.colors.html = True r = 'a fake reason' t = MagicMock() t.shortDescription.return_value = 'a fake test output line <>' gtr._reportOutcome(t, '.', lambda x: x, None, r) self.assertTrue(r in self.stream.getvalue()) self.assertTrue('&' in self.stream.getvalue()) self.assertTrue('<' in self.stream.getvalue()) self.assertTrue('>' in self.stream.getvalue()) self.assertFalse(' ' in self.stream.getvalue()) self.assertFalse('<' in self.stream.getvalue()) self.assertFalse('>' in self.stream.getvalue())
def test_printErrorsVerbose3(self): """ printErrors() looks correct in verbose=3 mode """ try: raise Exception except: err = sys.exc_info() gtr = GreenTestResult(GreenStream(self.stream), None, 3, False, False) 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_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 _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 run(self, suite): "Run the given test case or test suite." result = GreenTestResult( self.stream, self.descriptions, self.verbosity, html=self.html, termcolor=self.termcolor) registerResult(result) with warnings.catch_warnings(): if self.warnings: # if self.warnings is set, use it to filter all the warnings warnings.simplefilter(self.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 self.warnings is None. if self.warnings in ['default', 'always']: warnings.filterwarnings('module', category=DeprecationWarning, message='Please use assert\w+ instead.') result.startTestRun() if self.subprocesses == 1: suite.run(result) else: tests = toProtoTestList(suite) pool = LoggingDaemonlessPool(processes=self.subprocesses) if tests: async_responses = [] for index, test in enumerate(tests): if self.run_coverage: coverage_number = index + 1 else: coverage_number = None async_responses.append(pool.apply_async( poolRunner, (test.dotted_name, coverage_number, self.omit))) pool.close() for test, async_response in zip(tests, async_responses): # Prints out the white 'processing...' version of the output result.startTest(test) # This blocks until the worker who is processing this # particular test actually finishes result.addProtoTestResult(async_response.get()) pool.terminate() pool.join() result.stopTestRun() return result
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_startTestVerbose(self): "startTest() contains output we expect in verbose mode" class FakeCase(unittest.TestCase): def runTest(self): pass gtr = GreenTestResult(GreenStream(self.stream), None, 2) tc = FakeCase() gtr.startTest(tc) output = self.stream.getvalue() output_lines = output.split('\n') # Output should look like (I'm not putting the termcolor formatting here) # green.test.test_runner # FakeCase # test_it self.assertEqual(len(output_lines), 3) self.assertFalse(' ' in output_lines[0]) self.assertTrue(' ' in output_lines[1]) self.assertTrue(' ' in output_lines[2])
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_printErrorsVerbose2(self): """ printErrors() looks correct in verbose=2 mode. """ try: raise Exception except: err = sys.exc_info() self.args.verbose = 2 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_printErrorsVerbose2', self.stream.getvalue()) self.assertIn('raise Exception', self.stream.getvalue()) self.assertIn('Error', self.stream.getvalue())
def test_reportOutcomeVerboseHTML(self): """ html=True causes _reportOutcome() to escape HTML in docstrings """ gtr = GreenTestResult(GreenStream(self.stream), None, 3) gtr.colors.html = True r = 'a fake reason' class Injection(unittest.TestCase): def test_method(self): 'a fake test output line <>' t = proto_test(Injection('test_method')) gtr._reportOutcome(t, '.', lambda x: x, None, r) self.assertTrue(r in self.stream.getvalue()) self.assertTrue('&' in self.stream.getvalue()) self.assertTrue('<' in self.stream.getvalue()) self.assertTrue('>' in self.stream.getvalue()) self.assertFalse(' ' in self.stream.getvalue()) self.assertFalse('<' in self.stream.getvalue()) self.assertFalse('>' in self.stream.getvalue())
def test_printErrorsZHTML(self): """ printErrors() looks correct in html mode """ try: raise Exception except: err = sys.exc_info() gtr = GreenTestResult(GreenStream(self.stream), None, 4) gtr.colors.html = True test = MagicMock() gtr.addError(test, proto_error(err)) gtr.printErrors() self.assertIn('\n\n', self.stream.getvalue()) self.assertIn('(most recent call last)', self.stream.getvalue()) self.assertIn('test_printErrorsZHTML', self.stream.getvalue()) self.assertIn('raise Exception', self.stream.getvalue()) self.assertIn('Error', self.stream.getvalue()) self.assertIn('<span', self.stream.getvalue()) self.assertIn('color: rgb(', self.stream.getvalue())
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 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_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_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_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_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())