unittest test skipping - Code Review (original) (raw)
OLD
NEW
1 """Test script for unittest.
1 """Test script for unittest.
2
2
3 By Collin Winter
3 By Collin Winter
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 from test import test_support
9 from test import test_support
10 import unittest
10 import unittest
(...skipping 13 matching lines...) Expand all Loading...
24 super(LoggingResult, self).startTest(test)
24 super(LoggingResult, self).startTest(test)
25
25
26 def stopTest(self, test):
26 def stopTest(self, test):
27 self._events.append('stopTest')
27 self._events.append('stopTest')
28 super(LoggingResult, self).stopTest(test)
28 super(LoggingResult, self).stopTest(test)
29
29
30 def addFailure(self, *args):
30 def addFailure(self, *args):
31 self._events.append('addFailure')
31 self._events.append('addFailure')
32 super(LoggingResult, self).addFailure(*args)
32 super(LoggingResult, self).addFailure(*args)
33
33
34 def addSuccess(self, *args):
35 self._events.append('addSuccess')
36 super(LoggingResult, self).addSuccess(*args)
37
34 def addError(self, *args):
38 def addError(self, *args):
35 self._events.append('addError')
39 self._events.append('addError')
36 super(LoggingResult, self).addError(*args)
40 super(LoggingResult, self).addError(*args)
37
41
42 def addSkip(self, *args):
43 self._events.append('addSkip')
44 super(LoggingResult, self).addSkip(*args)
45
46 def addExpectedFailure(self, *args):
47 self._events.append('addExpectedFailure')
48 super(LoggingResult, self).addExpectedFailure(*args)
49
50 def addUnexpectedSuccess(self, *args):
51 self._events.append('addUnexpectedSuccess')
52 super(LoggingResult, self).addUnexpectedSuccess(*args)
53
54
38 class TestEquality(object):
55 class TestEquality(object):
39 # Check for a valid __eq__ implementation
56 # Check for a valid __eq__ implementation
40 def test_eq(self):
57 def test_eq(self):
41 for obj_1, obj_2 in self.eq_pairs:
58 for obj_1, obj_2 in self.eq_pairs:
42 self.assertEqual(obj_1, obj_2)
59 self.assertEqual(obj_1, obj_2)
43 self.assertEqual(obj_2, obj_1)
60 self.assertEqual(obj_2, obj_1)
44
61
45 # Check for a valid __ne__ implementation
62 # Check for a valid __ne__ implementation
46 def test_ne(self):
63 def test_ne(self):
47 for obj_1, obj_2 in self.ne_pairs:
64 for obj_1, obj_2 in self.ne_pairs:
(...skipping 17 matching lines...) Expand all Loading...
65 try:
82 try:
66 assert hash(obj_1) != hash(obj_2)
83 assert hash(obj_1) != hash(obj_2)
67 except KeyboardInterrupt:
84 except KeyboardInterrupt:
68 raise
85 raise
69 except AssertionError:
86 except AssertionError:
70 self.fail("%s and %s hash equal, but shouldn't" % (obj_1, obj_2) )
87 self.fail("%s and %s hash equal, but shouldn't" % (obj_1, obj_2) )
71 except Exception, e:
88 except Exception, e:
72 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
89 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
73
90
74
91
92 # List subclass we can add attributes to.
93 class MyClassSuite(list):
94
95 def __init__(self, tests, klass):
96 super(MyClassSuite, self).__init__(tests)
97
98
75 ################################################################
99 ################################################################
76 ### /Support code
100 ### /Support code
77
101
78 class Test_TestLoader(TestCase):
102 class Test_TestLoader(TestCase):
79
103
80 ### Tests for TestLoader.loadTestsFromTestCase
104 ### Tests for TestLoader.loadTestsFromTestCase
81 ################################################################
105 ################################################################
82
106
83 # "Return a suite of all tests cases contained in the TestCase-derived
107 # "Return a suite of all tests cases contained in the TestCase-derived
84 # class testCaseClass"
108 # class testCaseClass"
(...skipping 1131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
1216 # "Callable object that constructs a test suite from a list of tests."
1240 # "Callable object that constructs a test suite from a list of tests."
1217 def test_suiteClass__loadTestsFromTestCase(self):
1241 def test_suiteClass__loadTestsFromTestCase(self):
1218 class Foo(unittest.TestCase):
1242 class Foo(unittest.TestCase):
1219 def test_1(self): pass
1243 def test_1(self): pass
1220 def test_2(self): pass
1244 def test_2(self): pass
1221 def foo_bar(self): pass
1245 def foo_bar(self): pass
1222
1246
1223 tests = [Foo('test_1'), Foo('test_2')]
1247 tests = [Foo('test_1'), Foo('test_2')]
1224
1248
1225 loader = unittest.TestLoader()
1249 loader = unittest.TestLoader()
1226 loader.suiteClass = list
1250 loader.classSuiteClass = MyClassSuite
1227 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1251 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1228
1252
1229 # It is implicit in the documentation for TestLoader.suiteClass that
1253 # It is implicit in the documentation for TestLoader.suiteClass that
1230 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1254 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1231 def test_suiteClass__loadTestsFromModule(self):
1255 def test_suiteClass__loadTestsFromModule(self):
1232 m = types.ModuleType('m')
1256 m = types.ModuleType('m')
1233 class Foo(unittest.TestCase):
1257 class Foo(unittest.TestCase):
1234 def test_1(self): pass
1258 def test_1(self): pass
1235 def test_2(self): pass
1259 def test_2(self): pass
1236 def foo_bar(self): pass
1260 def foo_bar(self): pass
1237 m.Foo = Foo
1261 m.Foo = Foo
1238
1262
1239 tests = [[Foo('test_1'), Foo('test_2')]]
1263 tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)]
1240
1264
1241 loader = unittest.TestLoader()
1265 loader = unittest.TestLoader()
1242 loader.suiteClass = list
1266 loader.suiteClass = list
1243 self.assertEqual(loader.loadTestsFromModule(m), tests)
1267 self.assertEqual(loader.loadTestsFromModule(m), tests)
1244
1268
1245 # It is implicit in the documentation for TestLoader.suiteClass that
1269 # It is implicit in the documentation for TestLoader.suiteClass that
1246 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1270 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1247 def test_suiteClass__loadTestsFromName(self):
1271 def test_suiteClass__loadTestsFromName(self):
1248 m = types.ModuleType('m')
1272 m = types.ModuleType('m')
1249 class Foo(unittest.TestCase):
1273 class Foo(unittest.TestCase):
1250 def test_1(self): pass
1274 def test_1(self): pass
1251 def test_2(self): pass
1275 def test_2(self): pass
1252 def foo_bar(self): pass
1276 def foo_bar(self): pass
1253 m.Foo = Foo
1277 m.Foo = Foo
1254
1278
1255 tests = [Foo('test_1'), Foo('test_2')]
1279 tests = [Foo('test_1'), Foo('test_2')]
1256
1280
1257 loader = unittest.TestLoader()
1281 loader = unittest.TestLoader()
1258 loader.suiteClass = list
1282 loader.classSuiteClass = MyClassSuite
1259 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1283 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1260
1284
1261 # It is implicit in the documentation for TestLoader.suiteClass that
1285 # It is implicit in the documentation for TestLoader.suiteClass that
1262 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1286 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1263 def test_suiteClass__loadTestsFromNames(self):
1287 def test_suiteClass__loadTestsFromNames(self):
1264 m = types.ModuleType('m')
1288 m = types.ModuleType('m')
1265 class Foo(unittest.TestCase):
1289 class Foo(unittest.TestCase):
1266 def test_1(self): pass
1290 def test_1(self): pass
1267 def test_2(self): pass
1291 def test_2(self): pass
1268 def foo_bar(self): pass
1292 def foo_bar(self): pass
1269 m.Foo = Foo
1293 m.Foo = Foo
1270
1294
1271 tests = [[Foo('test_1'), Foo('test_2')]]
1295 tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)]
1272
1296
1273 loader = unittest.TestLoader()
1297 loader = unittest.TestLoader()
1274 loader.suiteClass = list
1298 loader.suiteClass = list
1275 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
1299 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
1276
1300
1277 # "The default value is the TestSuite class"
1301 # "The default value is the TestSuite class"
1278 def test_suiteClass__default_value(self):
1302 def test_suiteClass__default_value(self):
1279 loader = unittest.TestLoader()
1303 loader = unittest.TestLoader()
1280 self.failUnless(loader.suiteClass is unittest.TestSuite)
1304 self.failUnless(loader.suiteClass is unittest.TestSuite)
1281
1305
(...skipping 972 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
2254 class Foo(unittest.TestCase):
2278 class Foo(unittest.TestCase):
2255 def test(self):
2279 def test(self):
2256 events.append('test')
2280 events.append('test')
2257
2281
2258 def defaultTestResult(self):
2282 def defaultTestResult(self):
2259 return LoggingResult(events)
2283 return LoggingResult(events)
2260
2284
2261 # Make run() find a result object on its own
2285 # Make run() find a result object on its own
2262 Foo('test').run()
2286 Foo('test').run()
2263
2287
2264 expected = ['startTest', 'test', 'stopTest']
2288 expected = ['startTest', 'test', 'addSuccess', 'stopTest']
2265 self.assertEqual(events, expected)
2289 self.assertEqual(events, expected)
2266
2290
2291
2292 class Test_TestSkipping(TestCase):
2293
2294 def test_skipping_behvior(self):
2295 class Foo(unittest.TestCase):
2296 def test_skip_me(self):
2297 self.skip("skip")
2298 events = []
2299 result = LoggingResult(events)
2300 test = Foo("test_skip_me")
2301 test.run(result)
2302 self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
2303 self.assertEqual(result.skipped, [(test, "skip")])
2304
2305 def test_skipping_decorators(self):
2306 op_table = ((unittest.skipUnless, False, True),
2307 (unittest.skipIf, True, False))
2308 for deco, do_skip, dont_skip in op_table:
2309 class Foo(unittest.TestCase):
2310 @deco(do_skip, "testing")
2311 def test_skip(self): pass
2312
2313 @deco(dont_skip, "testing")
2314 def test_dont_skip(self): pass
2315 test_do_skip = Foo("test_skip")
2316 test_dont_skip = Foo("test_dont_skip")
2317 suite = unittest.ClassTestSuite([test_do_skip, test_dont_skip], Foo)
2318 events = []
2319 result = LoggingResult(events)
2320 suite.run(result)
2321 self.assertEqual(len(result.skipped), 1)
2322 expected = ['startTest', 'addSkip', 'stopTest',
2323 'startTest', 'addSuccess', 'stopTest']
2324 self.assertEqual(events, expected)
2325 self.assertEqual(result.testsRun, 2)
2326 self.assertEqual(result.skipped, [(test_do_skip, "testing")])
2327 self.assertTrue(result.wasSuccessful())
2328
2329 def test_skip_class(self):
2330 @unittest.skip("testing")
2331 class Foo(unittest.TestCase):
2332 def test_1(self):
2333 record.append(1)
2334 record = []
2335 result = unittest.TestResult()
2336 suite = unittest.ClassTestSuite([Foo("test_1")], Foo)
2337 suite.run(result)
2338 self.assertEqual(result.skipped, [(suite, "testing")])
2339 self.assertEqual(record, [])
2340
2341 def test_expected_failure(self):
2342 class Foo(unittest.TestCase):
2343 @unittest.expectedFailure
2344 def test_die(self):
2345 self.fail("help me!")
2346 events = []
2347 result = LoggingResult(events)
2348 test = Foo("test_die")
2349 test.run(result)
2350 self.assertEqual(events,
2351 ['startTest', 'addExpectedFailure', 'stopTest'])
2352 self.assertEqual(result.expected_failures[0][0], test)
2353 self.assertTrue(result.wasSuccessful())
2354
2355 def test_unexpected_success(self):
2356 class Foo(unittest.TestCase):
2357 @unittest.expectedFailure
2358 def test_die(self):
2359 pass
2360 events = []
2361 result = LoggingResult(events)
2362 test = Foo("test_die")
2363 test.run(result)
2364 self.assertEqual(events,
2365 ['startTest', 'addUnexpectedSuccess', 'stopTest'])
2366 self.assertFalse(result.failures)
2367 self.assertEqual(result.unexpected_successes, [test])
2368 self.assertTrue(result.wasSuccessful())
2369
2370
2371
2267 class Test_Assertions(TestCase):
2372 class Test_Assertions(TestCase):
2268 def test_AlmostEqual(self):
2373 def test_AlmostEqual(self):
2269 self.failUnlessAlmostEqual(1.00000001, 1.0)
2374 self.failUnlessAlmostEqual(1.00000001, 1.0)
2270 self.failIfAlmostEqual(1.0000001, 1.0)
2375 self.failIfAlmostEqual(1.0000001, 1.0)
2271 self.assertRaises(AssertionError,
2376 self.assertRaises(AssertionError,
2272 self.failUnlessAlmostEqual, 1.0000001, 1.0)
2377 self.failUnlessAlmostEqual, 1.0000001, 1.0)
2273 self.assertRaises(AssertionError,
2378 self.assertRaises(AssertionError,
2274 self.failIfAlmostEqual, 1.00000001, 1.0)
2379 self.failIfAlmostEqual, 1.00000001, 1.0)
2275
2380
2276 self.failUnlessAlmostEqual(1.1, 1.0, places=0)
2381 self.failUnlessAlmostEqual(1.1, 1.0, places=0)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
2321 self.fail("assertRaises() didn't let exception pass through")
2426 self.fail("assertRaises() didn't let exception pass through")
2322
2427
2323
2428
2324 ######################################################################
2429 ######################################################################
2325 ## Main
2430 ## Main
2326 ######################################################################
2431 ######################################################################
2327
2432
2328 def test_main():
2433 def test_main():
2329 test_support.run_unittest(Test_TestCase, Test_TestLoader,
2434 test_support.run_unittest(Test_TestCase, Test_TestLoader,
2330 Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
2435 Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
2331 Test_Assertions)
2436 Test_TestSkipping, Test_Assertions)
2332
2437
2333 if __name__ == "__main__":
2438 if __name__ == "__main__":
2334 test_main()
2439 test_main()
OLD
NEW