def test_getDescription(self): """ getDescription() returns what we expect for all verbose levels """ # With a docstring class Fruit(unittest.TestCase): def test_stuff(self): "apple" pass t = proto_test(Fruit("test_stuff")) self.assertEqual(t.getDescription(1), "") self.assertEqual(t.getDescription(2), "test_stuff") self.assertEqual(t.getDescription(3), "apple") self.assertEqual(t.getDescription(4), "apple") # Without a docstring class Vegetable(unittest.TestCase): def test_stuff(self): pass t = proto_test(Vegetable("test_stuff")) self.assertEqual(t.getDescription(1), "") self.assertEqual(t.getDescription(2), "test_stuff") self.assertEqual(t.getDescription(3), "test_stuff") self.assertEqual(t.getDescription(4), "test_stuff")
def test_doctest(self): """ If we parse a doctest, we get all the fields we need. """ test = """ >>> f() 42 """ def f(): return 42 parser = doctest.DocTestParser() dt = parser.get_doctest(test, {"f": f}, "doctest.name", "somefile.py", 20) dt.__module__ = "somefile" p = proto_test(doctest.DocTestCase(dt)) # short description self.assertEqual(p.getDescription(2), "doctest.name") # long description description = p.getDescription(3) self.assertIn("doctest.name", description) self.assertIn("somefile.py", description) self.assertIn("20", description) # dotted name self.assertEqual(p.dotted_name, "doctest.name")
def toProtoTestList(suite, test_list=None, doing_completions=False): """ Take a test suite and turn it into a list of ProtoTests. This function is recursive. Pass it a suite, and it will re-call itself with smaller parts of the suite. """ if test_list == None: test_list = [] # Python's lousy handling of module import failures during loader discovery # makes this crazy special case necessary. See _make_failed_import_test in # the source code for unittest.loader if suite.__class__.__name__ == 'ModuleImportFailure': if doing_completions: return test_list exception_method = str(suite).split()[0] getattr(suite, exception_method)() # On to the real stuff if issubclass(type(suite), unittest.TestCase): # Skip actual blank TestCase objects that twisted inserts if str(type(suite)) != "<class 'twisted.trial.unittest.TestCase'>": test_list.append(proto_test(suite)) else: for i in suite: toProtoTestList(i, test_list, doing_completions) return test_list
def test_addSkip(self): "addSkip() makes the correct calls to other functions." test = proto_test(MagicMock()) reason = 'skip reason' self.gtr.addSkip(test, reason) self.gtr._reportOutcome.assert_called_with( test, 's', self.gtr.colors.skipped, reason=reason)
def toProtoTestList(suite, test_list=None, doing_completions=False): """ Take a test suite and turn it into a list of ProtoTests. This function is recursive. Pass it a suite, and it will re-call itself with smaller parts of the suite. """ if test_list is None: test_list = [] # Python's lousy handling of module import failures during loader discovery # makes this crazy special case necessary. See _make_failed_import_test in # the source code for unittest.loader if suite.__class__.__name__ == 'ModuleImportFailure': if doing_completions: return test_list exception_method = str(suite).split()[0] getattr(suite, exception_method)() # On to the real stuff if issubclass(type(suite), unittest.TestCase): # Skip actual blank TestCase objects that twisted inserts if str(type(suite)) != "<class 'twisted.trial.unittest.TestCase'>": test_list.append(proto_test(suite)) else: for i in suite: toProtoTestList(i, test_list, doing_completions) return test_list
def test_addSuccess(self): """ addSuccess adds a test correctly """ ptr = ProtoTestResult() test = proto_test(MagicMock()) ptr.addSuccess(test) self.assertEqual(test, ptr.passing[0])
def test_addUnexpectedSuccess(self): """ addUnexpectedSuccess() makes the correct calls to other functions. """ test = proto_test(MagicMock()) self.gtr.addUnexpectedSuccess(test) self.gtr._reportOutcome.assert_called_with( test, 'u', self.gtr.colors.unexpectedSuccess)
def test_addSkip(self): "addSkip adds a test and reason correctly" ptr = ProtoTestResult() test = proto_test(MagicMock()) reason = "some plausible reason" ptr.addSkip(test, reason) self.assertEqual(test, ptr.skipped[0][0]) self.assertEqual(reason, ptr.skipped[0][1])
def test_addUnexpectedSuccess(self): """ addUnexpectedSuccess adds a test correctly """ ptr = ProtoTestResult() test = proto_test(MagicMock()) ptr.addUnexpectedSuccess(test) self.assertEqual(test, ptr.unexpectedSuccesses[0])
def test_addUnexpectedSuccess_with_test_time(self): """ addUnexpectedSuccess() makes test time with correct value """ test = proto_test(MagicMock()) self.gtr.addUnexpectedSuccess(test, "0.42") self.assertEqual(test.test_time, "0.42")
def test_addSkip_with_test_time(self): """ addSkip() makes test time the correct value """ test = proto_test(MagicMock()) reason = "skip reason" self.gtr.addSkip(test, reason, "0.42") self.assertEqual(test.test_time, "0.42")
def test_addSuccess(self): "addSuccess() makes the correct calls to other functions." test = MagicMock() test.shortDescription.return_value = 'a' test.__str__.return_value = 'b' test = proto_test(test) self.gtr.addSuccess(test) self.gtr._reportOutcome.assert_called_with( test, '.', self.gtr.colors.passing)
def test_addSkip(self): """ addSkip() makes the correct calls to other functions. """ test = proto_test(MagicMock()) reason = 'skip reason' self.gtr.addSkip(test, reason) self.gtr._reportOutcome.assert_called_with( test, 's', self.gtr.colors.skipped, reason=reason)
def test_addSkip(self): """ addSkip adds a test and reason correctly """ ptr = ProtoTestResult() test = proto_test(MagicMock()) reason = "some plausible reason" ptr.addSkip(test, reason) self.assertEqual(test, ptr.skipped[0][0]) self.assertEqual(reason, ptr.skipped[0][1])
def test_addSuccess(self): """ addSuccess() makes the correct calls to other functions. """ test = MagicMock() test.shortDescription.return_value = "a" test.__str__.return_value = "b" test = proto_test(test) self.gtr.addSuccess(test) self.gtr._reportOutcome.assert_called_with(test, ".", self.gtr.colors.passing)
def test_addSuccess_with_test_time(self): """ addSuccess() sets test time to correct value """ test = MagicMock() test.shortDescription.return_value = "a" test.__str__.return_value = "b" test = proto_test(test) self.gtr.addSuccess(test, "0.42") self.assertEqual(test.test_time, "0.42")
def test_addSubTest_error(self, mock_addFailure, mock_addError): """ addSubTest calls over to addError for errors """ ptr = ProtoTestResult() test = proto_test(MagicMock()) test.failureException = KeyError subtest = MagicMock() err = [Exception] ptr.addSubTest(test, subtest, err) mock_addError.assert_called_with(subtest, err)
def test_addExpectedFailure(self): "addExpectedFailure() makes the correct calls to other functions." try: raise Exception except: err = sys.exc_info() test = proto_test(MagicMock()) err = proto_error(err) self.gtr.addExpectedFailure(test, err) self.gtr._reportOutcome.assert_called_with( test, 'x', self.gtr.colors.expectedFailure, err)
def test_addExpectedFailure(self): "addExpectedFailure adds a test and error correctly" ptr = ProtoTestResult() test = proto_test(MagicMock()) try: raise Exception except: err = proto_error(sys.exc_info()) ptr.addExpectedFailure(test, err) self.assertEqual(test, ptr.expectedFailures[0][0]) self.assertEqual(err, ptr.expectedFailures[0][1])
def test_newlineDocstring(self): """ Docstrings starting with a newline are properly handled. """ class MyTests(unittest.TestCase): def test_stuff(self): """ tricky """ pass test = proto_test(MyTests('test_stuff')) self.assertIn('tricky', test.getDescription(3))
def test_addExcepectedFailure_with_test_time(self): """ addExpectedFailure() makes test time correct value """ try: raise Exception except: err = sys.exc_info() test = proto_test(MagicMock()) err = proto_error(err) self.gtr.addExpectedFailure(test, err, "0.42") self.assertEqual(test.test_time, "0.42")
def test_addExpectedFailure(self): """ addExpectedFailure() makes the correct calls to other functions. """ try: raise Exception except: err = sys.exc_info() test = proto_test(MagicMock()) err = proto_error(err) self.gtr.addExpectedFailure(test, err) self.gtr._reportOutcome.assert_called_with( test, 'x', self.gtr.colors.expectedFailure, err)
def test_addError_with_test_time(self): """ addError() sets test time to correct value """ try: raise Exception except: err = sys.exc_info() test = proto_test(MagicMock()) err = proto_error(err) self.gtr.addError(test, err, '0.42') self.assertEqual(test.test_time, '0.42')
def test_addExpectedFailure(self): """ addExpectedFailure adds a test and error correctly """ ptr = ProtoTestResult() test = proto_test(MagicMock()) try: raise Exception except: err = proto_error(sys.exc_info()) ptr.addExpectedFailure(test, err) self.assertEqual(test, ptr.expectedFailures[0][0]) self.assertEqual(err, ptr.expectedFailures[0][1])
def test_getDescription(self): """ getDescription() returns what we expect for all verbose levels """ # With a docstring class Fruit(unittest.TestCase): def test_stuff(self): 'apple' pass t = proto_test(Fruit('test_stuff')) self.assertEqual(t.getDescription(1), '') self.assertEqual(t.getDescription(2), 'test_stuff') self.assertEqual(t.getDescription(3), 'apple') self.assertEqual(t.getDescription(4), 'apple') # Without a docstring class Vegetable(unittest.TestCase): def test_stuff(self): pass t = proto_test(Vegetable('test_stuff')) self.assertEqual(t.getDescription(1), '') self.assertEqual(t.getDescription(2), 'test_stuff') self.assertEqual(t.getDescription(3), 'test_stuff') self.assertEqual(t.getDescription(4), 'test_stuff')
def test_multilineDocstring(self): """ The description includes all of docstring until the first blank line. """ class LongDocs(unittest.TestCase): def test_long(self): """First line is tricky! garbage """ pass test = proto_test(LongDocs('test_long')) self.assertIn('tricky', test.getDescription(3)) self.assertNotIn('garbage', test.getDescription(3))
def test_adjustedDescription(self): "adjustedDescription() returns what we expect for all verbosity levels" # With a docstring t = MagicMock() val1 = 'apple' val2 = 'banana' t.__str__.return_value = val1 t.shortDescription.return_value = val2 t = proto_test(t) self.assertEqual(t.adjustedDescription(1), '') self.assertEqual(t.adjustedDescription(2), val1) self.assertEqual(t.adjustedDescription(3), val2) self.assertEqual(t.adjustedDescription(4), val2) # Without a docstring t = MagicMock() val1 = 'apple' t.__str__.return_value = val1 t.shortDescription.return_value = None t = proto_test(t) self.assertEqual(t.adjustedDescription(1), '') self.assertEqual(t.adjustedDescription(2), val1) self.assertEqual(t.adjustedDescription(3), val1) self.assertEqual(t.adjustedDescription(4), val1)
def getTestList(item, test_list=None): if test_list == None: test_list = [] # Python's lousy handling of module import failures during loader discovery # makes this crazy special case necessary. See _make_failed_import_test in # the source code for unittest.loader if item.__class__.__name__ == 'ModuleImportFailure': exception_method = str(item).split()[0] getattr(item, exception_method)() # On to the real stuff if issubclass(type(item), TestCase): test_list.append(proto_test(item)) else: for i in item: getTestList(i, test_list) return test_list
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_addFailureTwistedSkip(self): """ Twisted's practice of calling addFailure() with their skips is detected and redirected to addSkip() """ err = None try: raise Exception except: err = sys.exc_info() test = proto_test(MagicMock()) reason = "Twisted is odd" err = proto_error(err) err.traceback_lines = ["UnsupportedTrialFeature: ('skip', '{}')" .format(reason)] self.gtr.addFailure(test, err) self.gtr._reportOutcome.assert_called_with( test, 's', self.gtr.colors.skipped, reason=reason)
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 toProtoTestList(suite_part, test_list=None): """ Take a test suite and turn it into a list of ProtoTests. This function is recursive. Pass it a suite, and it will re-call itself with smaller parts of the suite. """ if test_list == None: test_list = [] # Python's lousy handling of module import failures during loader discovery # makes this crazy special case necessary. See _make_failed_import_test in # the source code for unittest.loader if suite_part.__class__.__name__ == 'ModuleImportFailure': exception_method = str(suite_part).split()[0] getattr(suite_part, exception_method)() # On to the real stuff if issubclass(type(suite_part), unittest.TestCase): test_list.append(proto_test(suite_part)) else: for i in suite_part: toProtoTestList(i, test_list) return test_list
def start_callback(test): # Let the main process know what test we are starting test = proto_test(test) if test not in already_sent: queue.put(test) already_sent.add(test)
def start_callback(test): # Let the main process know what test we are starting queue.put(proto_test(test))
def test_addUnexpectedSuccess(self): "addUnexpectedSuccess adds a test correctly" ptr = ProtoTestResult() test = proto_test(MagicMock()) ptr.addUnexpectedSuccess(test) self.assertEqual(test, ptr.unexpectedSuccesses[0])