(original) (raw)

import sys import unittest import traceback # # Additions to unittest.TestCase class. # class TestCase(unittest.TestCase): failureException = unittest.TestCase.failureException class Unexpected_Exception_Raised(failureException): def __init__(self, exc, ref): self.exc = exc self.ref = ref def __str__(self): return '\n'.join([repr(self.exc), '\nReference:', self.ref]) class Wrong_Exception_Raised(Unexpected_Exception_Raised): pass class No_Exception_Raised(failureException): def __init__(self, result, ref=""): self.result = repr(result) self.ref = ref def __str__(self): return "returned -> " + '\n'.join([self.result, '\nReference:', self.ref]) class Wrong_Result_Returned(No_Exception_Raised): def __str__(self): return '\n'.join([self.result, '\nReference:', self.ref]) def assertTestReturns(self, test, expect, ref=""): try: result = test() except Exception, e: e0, e1, e2 = sys.exc_info() raise self.Unexpected_Exception_Raised, (e, ref), e2 if result != expect: raise self.Wrong_Result_Returned(result, ref) def assertTestRaises(self, test, expect, ref=""): try: result = test() except Exception, e: if isinstance(e, expect): return e else: e0, e1, e2 = sys.exc_info() raise self.Wrong_Exception_Raised, (e, ref), e2 raise self.No_Exception_Raised(result, ref) # # A minimal function to test in order to generate varous errors # depending on the data. # def some_function(*args, **kwds): bar = args[0] + args[1] baz = kwds['baz'] return (baz, bar) # # Data lists like this could be hundreds of lines. # The ref# is there to help distinguish nearly identical # tests from each other and help find the specific failing item. # # (ref#, expected, args, kwds) test_data_A = [ (1, ('Total baz:', 3), [1, 2], {'baz':'Total baz:'}), # pass (2, ('Total baz:', 3), [1, 2], {'raz':'Total baz:'}) # Unexpected exc ] test_data_B = [ (3, KeyError, [1, 2], {'raz':'Total baz:'}), # pass (4, IndexError, [1, 2], {'raz':'Total baz:'}) # Wrong exception ] test_data_C = [ (5, KeyError, [1, 2], {'raz':'Total baz:'}), # pass (6, KeyError, [1, 2], {'baz':'Total baz:'}) # No exception ] test_data_D = [ (7, ('Total baz:', 3), [1, 2], {'baz':'Total baz:'}), # pass (8, ('Total baz:', 4), [1, 2], {'baz':'Total baz:'}) # Wrong value ] class test1_normal_failures(TestCase): def test_A(self): for n, expect, args, kwds in test_data_A: result = some_function(*args, **kwds) self.assertEqual(expect, result, repr((n, expect, args, kwds))) def test_B(self): for n, expect, args, kwds in test_data_B: def test(args, kwds): return some_function(*args, **kwds) self.assertRaises(expect, test, args, kwds) def test_C(self): for n, expect, args, kwds in test_data_C: def test(args, kwds): return some_function(*args, **kwds) self.assertRaises(expect, test, args, kwds) def test_D(self): for n, expect, args, kwds in test_data_D: result = some_function(*args, **kwds) self.assertEqual(expect, result, repr((n, expect, args, kwds))) class test2_new_failures(TestCase): def test_A(self): for n, expect, args, kwds in test_data_A: def test(): return some_function(*args, **kwds) self.assertTestReturns(test, expect, repr((n, expect, args, kwds))) def test_B(self): for n, expect, args, kwds in test_data_B: def test(): return some_function(*args, **kwds) self.assertTestRaises(test, expect, repr((n, expect, args, kwds))) def test_C(self): for n, expect, args, kwds in test_data_C: def test(): return some_function(*args, **kwds) self.assertTestRaises(test, expect, repr((n, expect, args, kwds))) def test_D(self): for n, expect, args, kwds in test_data_D: def test(): return some_function(*args, **kwds) self.assertTestReturns(test, expect, repr((n, expect, args, kwds))) def runtests(): unittest.main(__name__) if __name__ == "__main__": runtests()