def test_overwrite(self): """ Non-default command-line argument values overwrite config values. """ # This config environment should set the values we look at to False and # a filename in omit-patterns s = StringIO() gs = hmaStream(s) saved_stdout = config.sys.stdout config.sys.stdout = gs self.addCleanup(setattr, config.sys, 'stdout', saved_stdout) with ModifiedEnvironment(hma_CONFIG=self.env_filename, HOME=self.tmpd): new_args = copy.deepcopy(config.default_args) new_args.omit_patterns = 'omitstuff' new_args.run_coverage = True new_args.logging = True new_args.no_skip_report = True new_args.version = True new_args.config = self.cmd_filename computed_args = config.mergeConfig(new_args, testing=True) self.assertEqual(computed_args.omit_patterns, 'omitstuff') self.assertEqual(computed_args.run_coverage, new_args.run_coverage) self.assertEqual(computed_args.logging, new_args.logging) self.assertEqual(computed_args.no_skip_report, new_args.no_skip_report) self.assertEqual(computed_args.version, new_args.version)
def test_tryRecordingStdoutStderr(self): """ Recording stdout and stderr works correctly. """ gtr = hmaTestResult(self.args, hmaStream(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_hmaStream(self): """ run() can use a hmaStream for output. """ gs = hmaStream(self.stream) run(hmaTestSuite(), gs, args=self.args) self.assertIn('No Tests Found', self.stream.getvalue())
def test_reportOutcome(self): """ _reportOutcome contains output we expect """ self.args.verbose = 1 gtr = hmaTestResult(self.args, hmaStream(self.stream)) gtr._reportOutcome(None, '.', lambda x: x) self.assertIn('.', self.stream.getvalue())
def test_colorOutput(self): """ Color output functions on windows """ import colorama gs = hmaStream(sys.stdout, override_appveyor=True) self.assertTrue(issubclass(type(gs.stream), colorama.ansitowin32.StreamWrapper))
def testFormatText(self): """ formatText returns the input text by default """ s = StringIO() gs = hmaStream(s) msg = u"Unindented line.\n Indented.\n Double-indented.\n\n\n" self.assertEqual(gs.formatText(msg), str(msg))
def test_wasSuccessful(self): """ wasSuccessful returns what we expect """ self.args.verbose = 1 gtr = hmaTestResult(self.args, hmaStream(self.stream)) self.assertEqual(gtr.wasSuccessful(), True) gtr.all_errors.append('anything') self.assertEqual(gtr.wasSuccessful(), False)
def test_failfastAddUnexpectedSuccess(self): """ addUnexpectedSuccess triggers failfast when it is set """ self.args.failfast = True gtr = hmaTestResult(self.args, hmaStream(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 = hmaTestResult(self.args, hmaStream(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 = hmaTestResult(self.args, hmaStream(self.stream)) tc = FakeCase() gtr.startTest(tc) output = self.stream.getvalue() return output.split('\n')
def test_printErrorsStdout(self): """ printErrors() prints out the captured stdout """ self.args.verbose = 1 self.args.termcolor = False gtr = hmaTestResult(self.args, hmaStream(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 testBadStringType(self): """ passing the wrong stream type to hmaStream gets auto-converted """ s = StringIO() gs = hmaStream(s) msg = "some string" if sys.version_info[0] == 3: # pragma: no cover bad_str = bytes(msg, 'utf-8') else: # pragma: no cover bad_str = str(msg) gs.write(bad_str) self.assertEqual(s.getvalue(), msg)
def test_failfastAddFailure(self): """ addFailure triggers failfast when it is set """ self.args.failfast = True gtr = hmaTestResult(self.args, hmaStream(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 = hmaStream(self.stream) gs.isatty = isatty gtr = hmaTestResult(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): """ _reportOutcome moves the cursor up when it needs to """ self.args.verbose = 2 def isatty(): return True gs = hmaStream(self.stream) gs.isatty = isatty gtr = hmaTestResult(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 = hmaTestResult(self.args, hmaStream(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 = hmaTestResult(self.args, hmaStream(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_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 = hmaTestResult(self.args, hmaStream(self.stream)) gtr.startTestRun() self.assertRaises(CoverageException, gtr.stopTestRun) self.args.cov.stop = MagicMock( side_effect=CoverageException('No data to report'))
def setUp(self): self.stream = StringIO() self.args = copy.deepcopy(default_args) self.args.verbose = 0 self.gtr = hmaTestResult(self.args, hmaStream(self.stream)) self.gtr._reportOutcome = MagicMock()