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 10class TestWith(unittest2.TestCase): 11 """Tests that use the with statement live in this 12 module so that all other tests can be run with Python 2.4. 13 """ 14 15 def testAssertRaisesExcValue(self): 16 class ExceptionMock(Exception): 17 pass 18 19 def Stub(foo): 20 raise ExceptionMock(foo) 21 v = "particular value" 22 23 ctx = self.assertRaises(ExceptionMock) 24 with ctx: 25 Stub(v) 26 e = ctx.exception 27 self.assertIsInstance(e, ExceptionMock) 28 self.assertEqual(e.args[0], v) 29 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, 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, 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, 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 msg 77 with self.assertRaises(self.failureException): 78 self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'}) 79 80 def test_formatMessage_unicode_error(self): 81 with catch_warnings(record=True): 82 # This causes a UnicodeWarning due to its craziness 83 one = ''.join(chr(i) for i in range(255)) 84 # this used to cause a UnicodeDecodeError constructing msg 85 self._formatMessage(one, u'\uFFFD') 86 87 def assertOldResultWarning(self, test, failures): 88 with catch_warnings(record=True) as log: 89 result = OldTestResult() 90 test.run(result) 91 self.assertEqual(len(result.failures), failures) 92 warning, = log 93 self.assertIs(warning.category, DeprecationWarning) 94 95 def test_old_testresult(self): 96 class Test(unittest2.TestCase): 97 def testSkip(self): 98 self.skipTest('foobar') 99 @unittest2.expectedFailure 100 def testExpectedFail(self): 101 raise TypeError 102 @unittest2.expectedFailure 103 def testUnexpectedSuccess(self): 104 pass 105 106 for test_name, should_pass in (('testSkip', True), 107 ('testExpectedFail', True), 108 ('testUnexpectedSuccess', False)): 109 test = Test(test_name) 110 self.assertOldResultWarning(test, int(not should_pass)) 111 112 def test_old_testresult_setup(self): 113 class Test(unittest2.TestCase): 114 def setUp(self): 115 self.skipTest('no reason') 116 def testFoo(self): 117 pass 118 self.assertOldResultWarning(Test('testFoo'), 0) 119 120 def test_old_testresult_class(self): 121 class Test(unittest2.TestCase): 122 def testFoo(self): 123 pass 124 Test = unittest2.skip('no reason')(Test) 125 self.assertOldResultWarning(Test('testFoo'), 0) 126 127 def testPendingDeprecationMethodNames(self): 128 """Test fail* methods pending deprecation, they will warn in 3.2. 129 130 Do not use these methods. They will go away in 3.3. 131 """ 132 with catch_warnings(record=True): 133 self.failIfEqual(3, 5) 134 self.failUnlessEqual(3, 3) 135 self.failUnlessAlmostEqual(2.0, 2.0) 136 self.failIfAlmostEqual(3.0, 5.0) 137 self.failUnless(True) 138 self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam') 139 self.failIf(False) 140 141 142if __name__ == '__main__': 143 unittest2.main() 144