OLD | NEW |
1 """Test script for unittest. | 1 """Test script for unittest. |
2 | 2 |
3 By Collin Winter <collinw at gmail.com> | 3 By Collin Winter <collinw at gmail.com> |
4 | 4 |
5 Still need testing: | 5 Still need testing: |
6 TestCase.{assert,fail}* methods (some are tested implicitly) | 6 TestCase.{assert,fail}* methods (some are tested implicitly) |
7 """ | 7 """ |
8 | 8 |
9 import re | 9 import re |
10 from test import test_support | 10 from test import test_support |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 def addExpectedFailure(self, *args): | 47 def addExpectedFailure(self, *args): |
48 self._events.append('addExpectedFailure') | 48 self._events.append('addExpectedFailure') |
49 super(LoggingResult, self).addExpectedFailure(*args) | 49 super(LoggingResult, self).addExpectedFailure(*args) |
50 | 50 |
51 def addUnexpectedSuccess(self, *args): | 51 def addUnexpectedSuccess(self, *args): |
52 self._events.append('addUnexpectedSuccess') | 52 self._events.append('addUnexpectedSuccess') |
53 super(LoggingResult, self).addUnexpectedSuccess(*args) | 53 super(LoggingResult, self).addUnexpectedSuccess(*args) |
54 | 54 |
55 | 55 |
56 class TestEquality(object): | 56 class TestEquality(object): |
| 57 """Used as a mixin for TestCase""" |
| 58 ···· |
57 # Check for a valid __eq__ implementation | 59 # Check for a valid __eq__ implementation |
58 def test_eq(self): | 60 def test_eq(self): |
59 for obj_1, obj_2 in self.eq_pairs: | 61 for obj_1, obj_2 in self.eq_pairs: |
60 self.assertEqual(obj_1, obj_2) | 62 self.assertEqual(obj_1, obj_2) |
61 self.assertEqual(obj_2, obj_1) | 63 self.assertEqual(obj_2, obj_1) |
62 | 64 |
63 # Check for a valid __ne__ implementation | 65 # Check for a valid __ne__ implementation |
64 def test_ne(self): | 66 def test_ne(self): |
65 for obj_1, obj_2 in self.ne_pairs: | 67 for obj_1, obj_2 in self.ne_pairs: |
66 self.failIfEqual(obj_1, obj_2) | 68 self.failIfEqual(obj_1, obj_2) |
67 self.failIfEqual(obj_2, obj_1) | 69 self.failIfEqual(obj_2, obj_1) |
68 | 70 |
69 class TestHashing(object): | 71 class TestHashing(object): |
| 72 """Used as a mixin for TestCase""" |
| 73 ···· |
70 # Check for a valid __hash__ implementation | 74 # Check for a valid __hash__ implementation |
71 def test_hash(self): | 75 def test_hash(self): |
72 for obj_1, obj_2 in self.eq_pairs: | 76 for obj_1, obj_2 in self.eq_pairs: |
73 try: | 77 try: |
74 if not hash(obj_1) == hash(obj_2): | 78 if not hash(obj_1) == hash(obj_2): |
75 self.fail("%r and %r do not hash equal" % (obj_1, obj_2)) | 79 self.fail("%r and %r do not hash equal" % (obj_1, obj_2)) |
76 except KeyboardInterrupt: | 80 except KeyboardInterrupt: |
77 raise | 81 raise |
78 except Exception, e: | 82 except Exception, e: |
79 self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e)) | 83 self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e)) |
(...skipping 2748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2828 self.fail("assertRaises() didn't fail") | 2832 self.fail("assertRaises() didn't fail") |
2829 try: | 2833 try: |
2830 with self.assertRaises(KeyError): | 2834 with self.assertRaises(KeyError): |
2831 raise ValueError | 2835 raise ValueError |
2832 except ValueError: | 2836 except ValueError: |
2833 pass | 2837 pass |
2834 else: | 2838 else: |
2835 self.fail("assertRaises() didn't let exception pass through") | 2839 self.fail("assertRaises() didn't let exception pass through") |
2836 | 2840 |
2837 | 2841 |
| 2842 class TestLongMessage(TestCase): |
| 2843 """Test that the individual asserts honour longMessage. |
| 2844 This actually tests all the message behaviour for |
| 2845 asserts that use longMessage.""" |
| 2846 |
| 2847 def setUp(self): |
| 2848 class TestableTestFalse(TestCase): |
| 2849 longMessage = False |
| 2850 failureException = self.failureException |
| 2851 ············ |
| 2852 def testTest(self): |
| 2853 pass |
| 2854 ············ |
| 2855 class TestableTestTrue(TestCase): |
| 2856 longMessage = True |
| 2857 failureException = self.failureException |
| 2858 ············ |
| 2859 def testTest(self): |
| 2860 pass |
| 2861 ············ |
| 2862 self.testableTrue = TestableTestTrue('testTest') |
| 2863 self.testableFalse = TestableTestFalse('testTest') |
| 2864 ········ |
| 2865 def testDefault(self): |
| 2866 self.assertFalse(TestCase.longMessage) |
| 2867 ········ |
| 2868 def test_formatMsg(self): |
| 2869 self.assertEquals(self.testableFalse._formatMessage(None, "foo"), "foo") |
| 2870 self.assertEquals(self.testableFalse._formatMessage("foo", "bar"), "foo"
) |
| 2871 |
| 2872 self.assertEquals(self.testableTrue._formatMessage(None, "foo"), "foo") |
| 2873 self.assertEquals(self.testableTrue._formatMessage("foo", "bar"), "bar :
foo") |
| 2874 ········ |
| 2875 def assertMessages(self, methodName, args, errors): |
| 2876 def getMethod(i): |
| 2877 useTestableFalse = i < 2 |
| 2878 if useTestableFalse: |
| 2879 test = self.testableFalse |
| 2880 else: |
| 2881 test = self.testableTrue |
| 2882 return getattr(test, methodName) |
| 2883 ················ |
| 2884 for i, expected_regexp in enumerate(errors): |
| 2885 testMethod = getMethod(i) |
| 2886 kwargs = {} |
| 2887 withMsg = i % 2 |
| 2888 if withMsg: |
| 2889 kwargs = {"msg": "oops"} |
| 2890 ················ |
| 2891 with self.assertRaisesRegexp(self.failureException,· |
| 2892 expected_regexp=expected_regexp): |
| 2893 testMethod(*args, **kwargs) |
| 2894 ············ |
| 2895 def testAssertTrue(self): |
| 2896 self.assertMessages('assertTrue', (False,),· |
| 2897 ["^False is not True$", "^oops$", "^False is not Tru
e$",· |
| 2898 "^False is not True : oops$"]) |
| 2899 · |
| 2900 def testAssertFalse(self): |
| 2901 self.assertMessages('assertFalse', (True,),· |
| 2902 ["^True is not False$", "^oops$", "^True is not Fals
e$",· |
| 2903 "^True is not False : oops$"]) |
| 2904 ········ |
| 2905 def testNotEqual(self): |
| 2906 self.assertMessages('assertNotEqual', (1, 1),· |
| 2907 ["^1 == 1$", "^oops$", "^1 == 1$",· |
| 2908 "^1 == 1 : oops$"]) |
| 2909 ········ |
| 2910 def testAlmostEqual(self): |
| 2911 self.assertMessages('assertAlmostEqual', (1, 2),· |
| 2912 ["^1 != 2 within 7 places$", "^oops$",· |
| 2913 "^1 != 2 within 7 places$", "^1 != 2 within 7 place
s : oops$"]) |
| 2914 ········ |
| 2915 def testNotAlmostEqual(self): |
| 2916 self.assertMessages('assertNotAlmostEqual', (1, 1),· |
| 2917 ["^1 == 1 within 7 places$", "^oops$",· |
| 2918 "^1 == 1 within 7 places$", "^1 == 1 within 7 place
s : oops$"]) |
| 2919 |
| 2920 def test_baseAssertEqual(self): |
| 2921 self.assertMessages('_baseAssertEqual', (1, 2),· |
| 2922 ["^1 != 2$", "^oops$", "^1 != 2$", "^1 != 2 : oops$"
]) |
| 2923 |
| 2924 def testAssertSequenceEqual(self): |
| 2925 # Error messages are multiline so not testing on full message |
| 2926 # assertTupleEqual and assertListEqual delegate to this method |
| 2927 self.assertMessages('assertSequenceEqual', ([], [None]),· |
| 2928 ["\+ \[None\]$", "^oops$", r"\+ \[None\]$",· |
| 2929 r"\+ \[None\] : oops$"]) |
| 2930 |
| 2931 def testAssertSetEqual(self): |
| 2932 self.assertMessages('assertSetEqual', (set(), set([None])),· |
| 2933 ["None$", "^oops$", "None$",· |
| 2934 "None : oops$"]) |
| 2935 ········ |
| 2936 def testAssertIn(self): |
| 2937 self.assertMessages('assertIn', (None, []),· |
| 2938 ['^None not found in \[\]$', "^oops$",· |
| 2939 '^None not found in \[\]$', |
| 2940 '^None not found in \[\] : oops$']) |
| 2941 |
| 2942 def testAssertNotIn(self): |
| 2943 self.assertMessages('assertNotIn', (None, [None]),· |
| 2944 ['^None unexpectedly found in \[None\]$', "^oops$",· |
| 2945 '^None unexpectedly found in \[None\]$', |
| 2946 '^None unexpectedly found in \[None\] : oops$']) |
| 2947 |
| 2948 def testAssertDictEqual(self): |
| 2949 self.assertMessages('assertDictEqual', ({}, {'key': 'value'}),· |
| 2950 [r"\+ \{'key': 'value'\}$", "^oops$",· |
| 2951 "\+ \{'key': 'value'\}$", |
| 2952 "\+ \{'key': 'value'\} : oops$"]) |
| 2953 |
| 2954 def testAssertDictContainsSubset(self): |
| 2955 self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}),· |
| 2956 ["^Missing: 'key'$", "^oops$",· |
| 2957 "^Missing: 'key'$", |
| 2958 "^Missing: 'key' : oops$"]) |
| 2959 ········ |
| 2960 def testAssertSameElements(self): |
| 2961 self.assertMessages('assertSameElements', ([], [None]),· |
| 2962 [r"\[None\]$", "^oops$",· |
| 2963 r"\[None\]$", |
| 2964 r"\[None\] : oops$"]) |
| 2965 ········ |
| 2966 def testAssertMultiLineEqual(self): |
| 2967 self.assertMessages('assertMultiLineEqual', ("", "foo"),· |
| 2968 [r"\+ foo$", "^oops$",· |
| 2969 r"\+ foo$", |
| 2970 r"\+ foo : oops$"]) |
| 2971 ············ |
| 2972 def testAssertLess(self): |
| 2973 self.assertMessages('assertLess', (2, 1),· |
| 2974 ["^2 not less than 1$", "^oops$",· |
| 2975 "^2 not less than 1$", "^2 not less than 1 : oops$"
]) |
| 2976 ········ |
| 2977 def testAssertLessEqual(self): |
| 2978 self.assertMessages('assertLessEqual', (2, 1),· |
| 2979 ["^2 not less than or equal to 1$", "^oops$",· |
| 2980 "^2 not less than or equal to 1$",· |
| 2981 "^2 not less than or equal to 1 : oops$"]) |
| 2982 ········ |
| 2983 def testAssertGreater(self): |
| 2984 self.assertMessages('assertGreater', (1, 2),· |
| 2985 ["^1 not greater than 2$", "^oops$",· |
| 2986 "^1 not greater than 2$",· |
| 2987 "^1 not greater than 2 : oops$"]) |
| 2988 ········ |
| 2989 def testAssertGreaterEqual(self): |
| 2990 self.assertMessages('assertGreaterEqual', (1, 2),· |
| 2991 ["^1 not greater than or equal to 2$", "^oops$",· |
| 2992 "^1 not greater than or equal to 2$",· |
| 2993 "^1 not greater than or equal to 2 : oops$"]) |
| 2994 |
| 2995 def testAssertIsNone(self): |
| 2996 self.assertMessages('assertIsNone', ('not None',),· |
| 2997 ["^'not None' is not None$", "^oops$",· |
| 2998 "^'not None' is not None$",· |
| 2999 "^'not None' is not None : oops$"]) |
| 3000 · |
| 3001 def testAssertIsNotNone(self): |
| 3002 self.assertMessages('assertIsNotNone', (None,),· |
| 3003 ["^unexpectedly None$", "^oops$",· |
| 3004 "^unexpectedly None$",· |
| 3005 "^unexpectedly None : oops$"]) |
| 3006 ········ |
| 3007 |
2838 ###################################################################### | 3008 ###################################################################### |
2839 ## Main | 3009 ## Main |
2840 ###################################################################### | 3010 ###################################################################### |
2841 | 3011 |
2842 def test_main(): | 3012 def test_main(): |
2843 test_support.run_unittest(Test_TestCase, Test_TestLoader, | 3013 test_support.run_unittest(Test_TestCase, Test_TestLoader, |
2844 Test_TestSuite, Test_TestResult, Test_FunctionTestCase, | 3014 Test_TestSuite, Test_TestResult, Test_FunctionTestCase, |
2845 Test_TestSkipping, Test_Assertions) | 3015 Test_TestSkipping, Test_Assertions, TestLongMessage) |
2846 | 3016 |
2847 if __name__ == "__main__": | 3017 if __name__ == "__main__": |
2848 test_main() | 3018 test_main() |
OLD | NEW |