1from __future__ import with_statement 2 3import unittest2 4from unittest2.test.support import OldTestResult, catch_warnings 5 6import warnings 7# needed to enable the deprecation warnings 8warnings.simplefilter('default') 9 10 11class TestWith(unittest2.TestCase): 12 """Tests that use the with statement live in this 13 module so that all other tests can be run with Python 2.4. 14 """ 15 16 def testAssertRaisesExcValue(self): 17 class ExceptionMock(Exception): 18 pass 19 20 def Stub(foo): 21 raise ExceptionMock(foo) 22 v = "particular value" 23 24 ctx = self.assertRaises(ExceptionMock) 25 with ctx: 26 Stub(v) 27 e = ctx.exception 28 self.assertIsInstance(e, ExceptionMock) 29 self.assertEqual(e.args[0], v) 30 31 def test_assertRaises(self): 32 def _raise(e): 33 raise e 34 self.assertRaises(KeyError, _raise, KeyError) 35 self.assertRaises(KeyError, _raise, KeyError("key")) 36 try: 37 self.assertRaises(KeyError, lambda: None) 38 except self.failureException as e: 39 self.assertIn("KeyError not raised", e.args) 40 else: 41 self.fail("assertRaises() didn't fail") 42 try: 43 self.assertRaises(KeyError, _raise, ValueError) 44 except ValueError: 45 pass 46 else: 47 self.fail("assertRaises() didn't let exception pass through") 48 with self.assertRaises(KeyError) as cm: 49 try: 50 raise KeyError 51 except Exception as e: 52 raise 53 self.assertIs(cm.exception, e) 54 55 with self.assertRaises(KeyError): 56 raise KeyError("key") 57 try: 58 with self.assertRaises(KeyError): 59 pass 60 except self.failureException as e: 61 self.assertIn("KeyError not raised", e.args) 62 else: 63 self.fail("assertRaises() didn't fail") 64 try: 65 with self.assertRaises(KeyError): 66 raise ValueError 67 except ValueError: 68 pass 69 else: 70 self.fail("assertRaises() didn't let exception pass through") 71 72 def test_assert_dict_unicode_error(self): 73 with catch_warnings(record=True): 74 # This causes a UnicodeWarning due to its craziness 75 one = ''.join(chr(i) for i in range(255)) 76 # this used to cause a UnicodeDecodeError constructing the failure 77 # msg 78 with self.assertRaises(self.failureException): 79 self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'}) 80 81 def test_formatMessage_unicode_error(self): 82 with catch_warnings(record=True): 83 # This causes a UnicodeWarning due to its craziness 84 one = ''.join(chr(i) for i in range(255)) 85 # this used to cause a UnicodeDecodeError constructing msg 86 self._formatMessage(one, u'\uFFFD') 87 88 def assertOldResultWarning(self, test, failures): 89 with catch_warnings(record=True) as log: 90 result = OldTestResult() 91 test.run(result) 92 self.assertEqual(len(result.failures), failures) 93 warning, = log 94 self.assertIs(warning.category, DeprecationWarning) 95 96 def test_old_testresult(self): 97 class Test(unittest2.TestCase): 98 99 def testSkip(self): 100 self.skipTest('foobar') 101 102 @unittest2.expectedFailure 103 def testExpectedFail(self): 104 raise TypeError 105 106 @unittest2.expectedFailure 107 def testUnexpectedSuccess(self): 108 pass 109 110 for test_name, should_pass in (('testSkip', True), 111 ('testExpectedFail', True), 112 ('testUnexpectedSuccess', False)): 113 test = Test(test_name) 114 self.assertOldResultWarning(test, int(not should_pass)) 115 116 def test_old_testresult_setup(self): 117 class Test(unittest2.TestCase): 118 119 def setUp(self): 120 self.skipTest('no reason') 121 122 def testFoo(self): 123 pass 124 self.assertOldResultWarning(Test('testFoo'), 0) 125 126 def test_old_testresult_class(self): 127 class Test(unittest2.TestCase): 128 129 def testFoo(self): 130 pass 131 Test = unittest2.skip('no reason')(Test) 132 self.assertOldResultWarning(Test('testFoo'), 0) 133 134 def testPendingDeprecationMethodNames(self): 135 """Test fail* methods pending deprecation, they will warn in 3.2. 136 137 Do not use these methods. They will go away in 3.3. 138 """ 139 with catch_warnings(record=True): 140 self.failIfEqual(3, 5) 141 self.failUnlessEqual(3, 3) 142 self.failUnlessAlmostEqual(2.0, 2.0) 143 self.failIfAlmostEqual(3.0, 5.0) 144 self.failUnless(True) 145 self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam') 146 self.failIf(False) 147 148 149if __name__ == '__main__': 150 unittest2.main() 151