blob: c9c17d3dff9473fe1cc8c5b28e3562c8c049658c [file] [log] [blame]
Jim Fultonfafd8742004-08-28 15:22:12 +00001"""Test script for unittest.
2
Guido van Rossumd8faa362007-04-27 19:54:29 +00003By Collin Winter <collinw at gmail.com>
4
5Still need testing:
6 TestCase.{assert,fail}* methods (some are tested implicitly)
Jim Fultonfafd8742004-08-28 15:22:12 +00007"""
8
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Jim Fultonfafd8742004-08-28 15:22:12 +000010import unittest
Guido van Rossumd8faa362007-04-27 19:54:29 +000011from unittest import TestCase
Christian Heimes45f9af32007-11-27 21:50:00 +000012import types
Jim Fultonfafd8742004-08-28 15:22:12 +000013
Guido van Rossumd8faa362007-04-27 19:54:29 +000014### Support code
15################################################################
Jim Fultonfafd8742004-08-28 15:22:12 +000016
Guido van Rossumd8faa362007-04-27 19:54:29 +000017class LoggingResult(unittest.TestResult):
18 def __init__(self, log):
19 self._events = log
Guido van Rossumcd16bf62007-06-13 18:07:49 +000020 super().__init__()
Guido van Rossumd8faa362007-04-27 19:54:29 +000021
22 def startTest(self, test):
23 self._events.append('startTest')
Guido van Rossumcd16bf62007-06-13 18:07:49 +000024 super().startTest(test)
Guido van Rossumd8faa362007-04-27 19:54:29 +000025
26 def stopTest(self, test):
27 self._events.append('stopTest')
Guido van Rossumcd16bf62007-06-13 18:07:49 +000028 super().stopTest(test)
Guido van Rossumd8faa362007-04-27 19:54:29 +000029
30 def addFailure(self, *args):
31 self._events.append('addFailure')
Guido van Rossumcd16bf62007-06-13 18:07:49 +000032 super().addFailure(*args)
Guido van Rossumd8faa362007-04-27 19:54:29 +000033
34 def addError(self, *args):
35 self._events.append('addError')
Guido van Rossumcd16bf62007-06-13 18:07:49 +000036 super().addError(*args)
Guido van Rossumd8faa362007-04-27 19:54:29 +000037
38class TestEquality(object):
39 # Check for a valid __eq__ implementation
40 def test_eq(self):
41 for obj_1, obj_2 in self.eq_pairs:
42 self.assertEqual(obj_1, obj_2)
43 self.assertEqual(obj_2, obj_1)
44
45 # Check for a valid __ne__ implementation
46 def test_ne(self):
47 for obj_1, obj_2 in self.ne_pairs:
48 self.failIfEqual(obj_1, obj_2)
49 self.failIfEqual(obj_2, obj_1)
50
51class TestHashing(object):
52 # Check for a valid __hash__ implementation
53 def test_hash(self):
54 for obj_1, obj_2 in self.eq_pairs:
55 try:
56 assert hash(obj_1) == hash(obj_2)
57 except KeyboardInterrupt:
58 raise
59 except AssertionError:
60 self.fail("%s and %s do not hash equal" % (obj_1, obj_2))
61 except Exception as e:
62 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
63
64 for obj_1, obj_2 in self.ne_pairs:
65 try:
66 assert hash(obj_1) != hash(obj_2)
67 except KeyboardInterrupt:
68 raise
69 except AssertionError:
70 self.fail("%s and %s hash equal, but shouldn't" % (obj_1, obj_2))
71 except Exception as e:
72 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
73
74
75################################################################
76### /Support code
77
78class Test_TestLoader(TestCase):
79
80 ### Tests for TestLoader.loadTestsFromTestCase
81 ################################################################
82
83 # "Return a suite of all tests cases contained in the TestCase-derived
84 # class testCaseClass"
85 def test_loadTestsFromTestCase(self):
86 class Foo(unittest.TestCase):
87 def test_1(self): pass
88 def test_2(self): pass
89 def foo_bar(self): pass
90
91 tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
92
93 loader = unittest.TestLoader()
94 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
95
96 # "Return a suite of all tests cases contained in the TestCase-derived
97 # class testCaseClass"
98 #
99 # Make sure it does the right thing even if no tests were found
100 def test_loadTestsFromTestCase__no_matches(self):
101 class Foo(unittest.TestCase):
102 def foo_bar(self): pass
103
104 empty_suite = unittest.TestSuite()
105
106 loader = unittest.TestLoader()
107 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
108
109 # "Return a suite of all tests cases contained in the TestCase-derived
110 # class testCaseClass"
111 #
112 # What happens if loadTestsFromTestCase() is given an object
113 # that isn't a subclass of TestCase? Specifically, what happens
114 # if testCaseClass is a subclass of TestSuite?
115 #
116 # This is checked for specifically in the code, so we better add a
117 # test for it.
118 def test_loadTestsFromTestCase__TestSuite_subclass(self):
119 class NotATestCase(unittest.TestSuite):
120 pass
121
122 loader = unittest.TestLoader()
123 try:
124 loader.loadTestsFromTestCase(NotATestCase)
125 except TypeError:
126 pass
127 else:
128 self.fail('Should raise TypeError')
129
130 # "Return a suite of all tests cases contained in the TestCase-derived
131 # class testCaseClass"
132 #
133 # Make sure loadTestsFromTestCase() picks up the default test method
134 # name (as specified by TestCase), even though the method name does
135 # not match the default TestLoader.testMethodPrefix string
136 def test_loadTestsFromTestCase__default_method_name(self):
137 class Foo(unittest.TestCase):
138 def runTest(self):
139 pass
140
141 loader = unittest.TestLoader()
142 # This has to be false for the test to succeed
143 self.failIf('runTest'.startswith(loader.testMethodPrefix))
144
145 suite = loader.loadTestsFromTestCase(Foo)
146 self.failUnless(isinstance(suite, loader.suiteClass))
147 self.assertEqual(list(suite), [Foo('runTest')])
148
149 ################################################################
150 ### /Tests for TestLoader.loadTestsFromTestCase
151
152 ### Tests for TestLoader.loadTestsFromModule
153 ################################################################
154
155 # "This method searches `module` for classes derived from TestCase"
156 def test_loadTestsFromModule__TestCase_subclass(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000157 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000158 class MyTestCase(unittest.TestCase):
159 def test(self):
160 pass
161 m.testcase_1 = MyTestCase
162
163 loader = unittest.TestLoader()
164 suite = loader.loadTestsFromModule(m)
165 self.failUnless(isinstance(suite, loader.suiteClass))
166
167 expected = [loader.suiteClass([MyTestCase('test')])]
168 self.assertEqual(list(suite), expected)
169
170 # "This method searches `module` for classes derived from TestCase"
171 #
172 # What happens if no tests are found (no TestCase instances)?
173 def test_loadTestsFromModule__no_TestCase_instances(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000174 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000175
176 loader = unittest.TestLoader()
177 suite = loader.loadTestsFromModule(m)
178 self.failUnless(isinstance(suite, loader.suiteClass))
179 self.assertEqual(list(suite), [])
180
181 # "This method searches `module` for classes derived from TestCase"
182 #
183 # What happens if no tests are found (TestCases instances, but no tests)?
184 def test_loadTestsFromModule__no_TestCase_tests(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000185 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 class MyTestCase(unittest.TestCase):
187 pass
188 m.testcase_1 = MyTestCase
189
190 loader = unittest.TestLoader()
191 suite = loader.loadTestsFromModule(m)
192 self.failUnless(isinstance(suite, loader.suiteClass))
193
194 self.assertEqual(list(suite), [loader.suiteClass()])
195
196 # "This method searches `module` for classes derived from TestCase"s
197 #
198 # What happens if loadTestsFromModule() is given something other
199 # than a module?
200 #
201 # XXX Currently, it succeeds anyway. This flexibility
202 # should either be documented or loadTestsFromModule() should
203 # raise a TypeError
204 #
205 # XXX Certain people are using this behaviour. We'll add a test for it
206 def test_loadTestsFromModule__not_a_module(self):
207 class MyTestCase(unittest.TestCase):
208 def test(self):
209 pass
210
211 class NotAModule(object):
212 test_2 = MyTestCase
213
214 loader = unittest.TestLoader()
215 suite = loader.loadTestsFromModule(NotAModule)
216
217 reference = [unittest.TestSuite([MyTestCase('test')])]
218 self.assertEqual(list(suite), reference)
219
220 ################################################################
221 ### /Tests for TestLoader.loadTestsFromModule()
222
223 ### Tests for TestLoader.loadTestsFromName()
224 ################################################################
225
226 # "The specifier name is a ``dotted name'' that may resolve either to
227 # a module, a test case class, a TestSuite instance, a test method
228 # within a test case class, or a callable object which returns a
229 # TestCase or TestSuite instance."
230 #
231 # Is ValueError raised in response to an empty name?
232 def test_loadTestsFromName__empty_name(self):
233 loader = unittest.TestLoader()
234
235 try:
236 loader.loadTestsFromName('')
237 except ValueError as e:
238 self.assertEqual(str(e), "Empty module name")
239 else:
240 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
241
242 # "The specifier name is a ``dotted name'' that may resolve either to
243 # a module, a test case class, a TestSuite instance, a test method
244 # within a test case class, or a callable object which returns a
245 # TestCase or TestSuite instance."
246 #
247 # What happens when the name contains invalid characters?
248 def test_loadTestsFromName__malformed_name(self):
249 loader = unittest.TestLoader()
250
251 # XXX Should this raise ValueError or ImportError?
252 try:
253 loader.loadTestsFromName('abc () //')
254 except ValueError:
255 pass
256 except ImportError:
257 pass
258 else:
259 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
260
261 # "The specifier name is a ``dotted name'' that may resolve ... to a
262 # module"
263 #
264 # What happens when a module by that name can't be found?
265 def test_loadTestsFromName__unknown_module_name(self):
266 loader = unittest.TestLoader()
267
268 try:
269 loader.loadTestsFromName('sdasfasfasdf')
270 except ImportError as e:
271 self.assertEqual(str(e), "No module named sdasfasfasdf")
272 else:
273 self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
274
275 # "The specifier name is a ``dotted name'' that may resolve either to
276 # a module, a test case class, a TestSuite instance, a test method
277 # within a test case class, or a callable object which returns a
278 # TestCase or TestSuite instance."
279 #
280 # What happens when the module is found, but the attribute can't?
281 def test_loadTestsFromName__unknown_attr_name(self):
282 loader = unittest.TestLoader()
283
284 try:
285 loader.loadTestsFromName('unittest.sdasfasfasdf')
286 except AttributeError as e:
287 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
288 else:
289 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
290
291 # "The specifier name is a ``dotted name'' that may resolve either to
292 # a module, a test case class, a TestSuite instance, a test method
293 # within a test case class, or a callable object which returns a
294 # TestCase or TestSuite instance."
295 #
296 # What happens when we provide the module, but the attribute can't be
297 # found?
298 def test_loadTestsFromName__relative_unknown_name(self):
299 loader = unittest.TestLoader()
300
301 try:
302 loader.loadTestsFromName('sdasfasfasdf', unittest)
303 except AttributeError as e:
304 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
305 else:
306 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
307
308 # "The specifier name is a ``dotted name'' that may resolve either to
309 # a module, a test case class, a TestSuite instance, a test method
310 # within a test case class, or a callable object which returns a
311 # TestCase or TestSuite instance."
312 # ...
313 # "The method optionally resolves name relative to the given module"
314 #
315 # Does loadTestsFromName raise ValueError when passed an empty
316 # name relative to a provided module?
317 #
318 # XXX Should probably raise a ValueError instead of an AttributeError
319 def test_loadTestsFromName__relative_empty_name(self):
320 loader = unittest.TestLoader()
321
322 try:
323 loader.loadTestsFromName('', unittest)
324 except AttributeError as e:
325 pass
326 else:
327 self.fail("Failed to raise AttributeError")
328
329 # "The specifier name is a ``dotted name'' that may resolve either to
330 # a module, a test case class, a TestSuite instance, a test method
331 # within a test case class, or a callable object which returns a
332 # TestCase or TestSuite instance."
333 # ...
334 # "The method optionally resolves name relative to the given module"
335 #
336 # What happens when an impossible name is given, relative to the provided
337 # `module`?
338 def test_loadTestsFromName__relative_malformed_name(self):
339 loader = unittest.TestLoader()
340
341 # XXX Should this raise AttributeError or ValueError?
342 try:
343 loader.loadTestsFromName('abc () //', unittest)
344 except ValueError:
345 pass
346 except AttributeError:
347 pass
348 else:
349 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
350
351 # "The method optionally resolves name relative to the given module"
352 #
353 # Does loadTestsFromName raise TypeError when the `module` argument
354 # isn't a module object?
355 #
356 # XXX Accepts the not-a-module object, ignorning the object's type
357 # This should raise an exception or the method name should be changed
358 #
359 # XXX Some people are relying on this, so keep it for now
360 def test_loadTestsFromName__relative_not_a_module(self):
361 class MyTestCase(unittest.TestCase):
362 def test(self):
363 pass
364
365 class NotAModule(object):
366 test_2 = MyTestCase
367
368 loader = unittest.TestLoader()
369 suite = loader.loadTestsFromName('test_2', NotAModule)
370
371 reference = [MyTestCase('test')]
372 self.assertEqual(list(suite), reference)
373
374 # "The specifier name is a ``dotted name'' that may resolve either to
375 # a module, a test case class, a TestSuite instance, a test method
376 # within a test case class, or a callable object which returns a
377 # TestCase or TestSuite instance."
378 #
379 # Does it raise an exception if the name resolves to an invalid
380 # object?
381 def test_loadTestsFromName__relative_bad_object(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000382 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000383 m.testcase_1 = object()
384
385 loader = unittest.TestLoader()
386 try:
387 loader.loadTestsFromName('testcase_1', m)
388 except TypeError:
389 pass
390 else:
391 self.fail("Should have raised TypeError")
392
393 # "The specifier name is a ``dotted name'' that may
394 # resolve either to ... a test case class"
395 def test_loadTestsFromName__relative_TestCase_subclass(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000396 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000397 class MyTestCase(unittest.TestCase):
398 def test(self):
399 pass
400 m.testcase_1 = MyTestCase
401
402 loader = unittest.TestLoader()
403 suite = loader.loadTestsFromName('testcase_1', m)
404 self.failUnless(isinstance(suite, loader.suiteClass))
405 self.assertEqual(list(suite), [MyTestCase('test')])
406
407 # "The specifier name is a ``dotted name'' that may resolve either to
408 # a module, a test case class, a TestSuite instance, a test method
409 # within a test case class, or a callable object which returns a
410 # TestCase or TestSuite instance."
411 def test_loadTestsFromName__relative_TestSuite(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000412 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000413 class MyTestCase(unittest.TestCase):
414 def test(self):
415 pass
416 m.testsuite = unittest.TestSuite([MyTestCase('test')])
417
418 loader = unittest.TestLoader()
419 suite = loader.loadTestsFromName('testsuite', m)
420 self.failUnless(isinstance(suite, loader.suiteClass))
421
422 self.assertEqual(list(suite), [MyTestCase('test')])
423
424 # "The specifier name is a ``dotted name'' that may resolve ... to
425 # ... a test method within a test case class"
426 def test_loadTestsFromName__relative_testmethod(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000427 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000428 class MyTestCase(unittest.TestCase):
429 def test(self):
430 pass
431 m.testcase_1 = MyTestCase
432
433 loader = unittest.TestLoader()
434 suite = loader.loadTestsFromName('testcase_1.test', m)
435 self.failUnless(isinstance(suite, loader.suiteClass))
436
437 self.assertEqual(list(suite), [MyTestCase('test')])
438
439 # "The specifier name is a ``dotted name'' that may resolve either to
440 # a module, a test case class, a TestSuite instance, a test method
441 # within a test case class, or a callable object which returns a
442 # TestCase or TestSuite instance."
443 #
444 # Does loadTestsFromName() raise the proper exception when trying to
445 # resolve "a test method within a test case class" that doesn't exist
446 # for the given name (relative to a provided module)?
447 def test_loadTestsFromName__relative_invalid_testmethod(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000448 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000449 class MyTestCase(unittest.TestCase):
450 def test(self):
451 pass
452 m.testcase_1 = MyTestCase
453
454 loader = unittest.TestLoader()
455 try:
456 loader.loadTestsFromName('testcase_1.testfoo', m)
457 except AttributeError as e:
458 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
459 else:
460 self.fail("Failed to raise AttributeError")
461
462 # "The specifier name is a ``dotted name'' that may resolve ... to
463 # ... a callable object which returns a ... TestSuite instance"
464 def test_loadTestsFromName__callable__TestSuite(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000465 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000466 testcase_1 = unittest.FunctionTestCase(lambda: None)
467 testcase_2 = unittest.FunctionTestCase(lambda: None)
468 def return_TestSuite():
469 return unittest.TestSuite([testcase_1, testcase_2])
470 m.return_TestSuite = return_TestSuite
471
472 loader = unittest.TestLoader()
473 suite = loader.loadTestsFromName('return_TestSuite', m)
474 self.failUnless(isinstance(suite, loader.suiteClass))
475 self.assertEqual(list(suite), [testcase_1, testcase_2])
476
477 # "The specifier name is a ``dotted name'' that may resolve ... to
478 # ... a callable object which returns a TestCase ... instance"
479 def test_loadTestsFromName__callable__TestCase_instance(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000480 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000481 testcase_1 = unittest.FunctionTestCase(lambda: None)
482 def return_TestCase():
483 return testcase_1
484 m.return_TestCase = return_TestCase
485
486 loader = unittest.TestLoader()
487 suite = loader.loadTestsFromName('return_TestCase', m)
488 self.failUnless(isinstance(suite, loader.suiteClass))
489 self.assertEqual(list(suite), [testcase_1])
490
491 # "The specifier name is a ``dotted name'' that may resolve ... to
492 # ... a callable object which returns a TestCase or TestSuite instance"
493 #
494 # What happens if the callable returns something else?
495 def test_loadTestsFromName__callable__wrong_type(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000496 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000497 def return_wrong():
498 return 6
499 m.return_wrong = return_wrong
500
501 loader = unittest.TestLoader()
502 try:
503 suite = loader.loadTestsFromName('return_wrong', m)
504 except TypeError:
505 pass
506 else:
507 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
508
509 # "The specifier can refer to modules and packages which have not been
510 # imported; they will be imported as a side-effect"
511 def test_loadTestsFromName__module_not_loaded(self):
512 # We're going to try to load this module as a side-effect, so it
513 # better not be loaded before we try.
514 #
515 # Why pick audioop? Google shows it isn't used very often, so there's
516 # a good chance that it won't be imported when this test is run
517 module_name = 'audioop'
518
519 import sys
520 if module_name in sys.modules:
521 del sys.modules[module_name]
522
523 loader = unittest.TestLoader()
524 try:
525 suite = loader.loadTestsFromName(module_name)
526
527 self.failUnless(isinstance(suite, loader.suiteClass))
528 self.assertEqual(list(suite), [])
529
530 # audioop should now be loaded, thanks to loadTestsFromName()
531 self.failUnless(module_name in sys.modules)
532 finally:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000533 if module_name in sys.modules:
534 del sys.modules[module_name]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000535
536 ################################################################
537 ### Tests for TestLoader.loadTestsFromName()
538
539 ### Tests for TestLoader.loadTestsFromNames()
540 ################################################################
541
542 # "Similar to loadTestsFromName(), but takes a sequence of names rather
543 # than a single name."
544 #
545 # What happens if that sequence of names is empty?
546 def test_loadTestsFromNames__empty_name_list(self):
547 loader = unittest.TestLoader()
548
549 suite = loader.loadTestsFromNames([])
550 self.failUnless(isinstance(suite, loader.suiteClass))
551 self.assertEqual(list(suite), [])
552
553 # "Similar to loadTestsFromName(), but takes a sequence of names rather
554 # than a single name."
555 # ...
556 # "The method optionally resolves name relative to the given module"
557 #
558 # What happens if that sequence of names is empty?
559 #
560 # XXX Should this raise a ValueError or just return an empty TestSuite?
561 def test_loadTestsFromNames__relative_empty_name_list(self):
562 loader = unittest.TestLoader()
563
564 suite = loader.loadTestsFromNames([], unittest)
565 self.failUnless(isinstance(suite, loader.suiteClass))
566 self.assertEqual(list(suite), [])
567
568 # "The specifier name is a ``dotted name'' that may resolve either to
569 # a module, a test case class, a TestSuite instance, a test method
570 # within a test case class, or a callable object which returns a
571 # TestCase or TestSuite instance."
572 #
573 # Is ValueError raised in response to an empty name?
574 def test_loadTestsFromNames__empty_name(self):
575 loader = unittest.TestLoader()
576
577 try:
578 loader.loadTestsFromNames([''])
579 except ValueError as e:
580 self.assertEqual(str(e), "Empty module name")
581 else:
582 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
583
584 # "The specifier name is a ``dotted name'' that may resolve either to
585 # a module, a test case class, a TestSuite instance, a test method
586 # within a test case class, or a callable object which returns a
587 # TestCase or TestSuite instance."
588 #
589 # What happens when presented with an impossible module name?
590 def test_loadTestsFromNames__malformed_name(self):
591 loader = unittest.TestLoader()
592
593 # XXX Should this raise ValueError or ImportError?
594 try:
595 loader.loadTestsFromNames(['abc () //'])
596 except ValueError:
597 pass
598 except ImportError:
599 pass
600 else:
601 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
602
603 # "The specifier name is a ``dotted name'' that may resolve either to
604 # a module, a test case class, a TestSuite instance, a test method
605 # within a test case class, or a callable object which returns a
606 # TestCase or TestSuite instance."
607 #
608 # What happens when no module can be found for the given name?
609 def test_loadTestsFromNames__unknown_module_name(self):
610 loader = unittest.TestLoader()
611
612 try:
613 loader.loadTestsFromNames(['sdasfasfasdf'])
614 except ImportError as e:
615 self.assertEqual(str(e), "No module named sdasfasfasdf")
616 else:
617 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
618
619 # "The specifier name is a ``dotted name'' that may resolve either to
620 # a module, a test case class, a TestSuite instance, a test method
621 # within a test case class, or a callable object which returns a
622 # TestCase or TestSuite instance."
623 #
624 # What happens when the module can be found, but not the attribute?
625 def test_loadTestsFromNames__unknown_attr_name(self):
626 loader = unittest.TestLoader()
627
628 try:
629 loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
630 except AttributeError as e:
631 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
632 else:
633 self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
634
635 # "The specifier name is a ``dotted name'' that may resolve either to
636 # a module, a test case class, a TestSuite instance, a test method
637 # within a test case class, or a callable object which returns a
638 # TestCase or TestSuite instance."
639 # ...
640 # "The method optionally resolves name relative to the given module"
641 #
642 # What happens when given an unknown attribute on a specified `module`
643 # argument?
644 def test_loadTestsFromNames__unknown_name_relative_1(self):
645 loader = unittest.TestLoader()
646
647 try:
648 loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
649 except AttributeError as e:
650 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
651 else:
652 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
653
654 # "The specifier name is a ``dotted name'' that may resolve either to
655 # a module, a test case class, a TestSuite instance, a test method
656 # within a test case class, or a callable object which returns a
657 # TestCase or TestSuite instance."
658 # ...
659 # "The method optionally resolves name relative to the given module"
660 #
661 # Do unknown attributes (relative to a provided module) still raise an
662 # exception even in the presence of valid attribute names?
663 def test_loadTestsFromNames__unknown_name_relative_2(self):
664 loader = unittest.TestLoader()
665
666 try:
667 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
668 except AttributeError as e:
669 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
670 else:
671 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
672
673 # "The specifier name is a ``dotted name'' that may resolve either to
674 # a module, a test case class, a TestSuite instance, a test method
675 # within a test case class, or a callable object which returns a
676 # TestCase or TestSuite instance."
677 # ...
678 # "The method optionally resolves name relative to the given module"
679 #
680 # What happens when faced with the empty string?
681 #
682 # XXX This currently raises AttributeError, though ValueError is probably
683 # more appropriate
684 def test_loadTestsFromNames__relative_empty_name(self):
685 loader = unittest.TestLoader()
686
687 try:
688 loader.loadTestsFromNames([''], unittest)
689 except AttributeError:
690 pass
691 else:
692 self.fail("Failed to raise ValueError")
693
694 # "The specifier name is a ``dotted name'' that may resolve either to
695 # a module, a test case class, a TestSuite instance, a test method
696 # within a test case class, or a callable object which returns a
697 # TestCase or TestSuite instance."
698 # ...
699 # "The method optionally resolves name relative to the given module"
700 #
701 # What happens when presented with an impossible attribute name?
702 def test_loadTestsFromNames__relative_malformed_name(self):
703 loader = unittest.TestLoader()
704
705 # XXX Should this raise AttributeError or ValueError?
706 try:
707 loader.loadTestsFromNames(['abc () //'], unittest)
708 except AttributeError:
709 pass
710 except ValueError:
711 pass
712 else:
713 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
714
715 # "The method optionally resolves name relative to the given module"
716 #
717 # Does loadTestsFromNames() make sure the provided `module` is in fact
718 # a module?
719 #
720 # XXX This validation is currently not done. This flexibility should
721 # either be documented or a TypeError should be raised.
722 def test_loadTestsFromNames__relative_not_a_module(self):
723 class MyTestCase(unittest.TestCase):
724 def test(self):
725 pass
726
727 class NotAModule(object):
728 test_2 = MyTestCase
729
730 loader = unittest.TestLoader()
731 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
732
733 reference = [unittest.TestSuite([MyTestCase('test')])]
734 self.assertEqual(list(suite), reference)
735
736 # "The specifier name is a ``dotted name'' that may resolve either to
737 # a module, a test case class, a TestSuite instance, a test method
738 # within a test case class, or a callable object which returns a
739 # TestCase or TestSuite instance."
740 #
741 # Does it raise an exception if the name resolves to an invalid
742 # object?
743 def test_loadTestsFromNames__relative_bad_object(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000744 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000745 m.testcase_1 = object()
746
747 loader = unittest.TestLoader()
748 try:
749 loader.loadTestsFromNames(['testcase_1'], m)
750 except TypeError:
751 pass
752 else:
753 self.fail("Should have raised TypeError")
754
755 # "The specifier name is a ``dotted name'' that may resolve ... to
756 # ... a test case class"
757 def test_loadTestsFromNames__relative_TestCase_subclass(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000758 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000759 class MyTestCase(unittest.TestCase):
760 def test(self):
761 pass
762 m.testcase_1 = MyTestCase
763
764 loader = unittest.TestLoader()
765 suite = loader.loadTestsFromNames(['testcase_1'], m)
766 self.failUnless(isinstance(suite, loader.suiteClass))
767
768 expected = loader.suiteClass([MyTestCase('test')])
769 self.assertEqual(list(suite), [expected])
770
771 # "The specifier name is a ``dotted name'' that may resolve ... to
772 # ... a TestSuite instance"
773 def test_loadTestsFromNames__relative_TestSuite(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000774 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000775 class MyTestCase(unittest.TestCase):
776 def test(self):
777 pass
778 m.testsuite = unittest.TestSuite([MyTestCase('test')])
779
780 loader = unittest.TestLoader()
781 suite = loader.loadTestsFromNames(['testsuite'], m)
782 self.failUnless(isinstance(suite, loader.suiteClass))
783
784 self.assertEqual(list(suite), [m.testsuite])
785
786 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
787 # test method within a test case class"
788 def test_loadTestsFromNames__relative_testmethod(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000789 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000790 class MyTestCase(unittest.TestCase):
791 def test(self):
792 pass
793 m.testcase_1 = MyTestCase
794
795 loader = unittest.TestLoader()
796 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
797 self.failUnless(isinstance(suite, loader.suiteClass))
798
799 ref_suite = unittest.TestSuite([MyTestCase('test')])
800 self.assertEqual(list(suite), [ref_suite])
801
802 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
803 # test method within a test case class"
804 #
805 # Does the method gracefully handle names that initially look like they
806 # resolve to "a test method within a test case class" but don't?
807 def test_loadTestsFromNames__relative_invalid_testmethod(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000808 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000809 class MyTestCase(unittest.TestCase):
810 def test(self):
811 pass
812 m.testcase_1 = MyTestCase
813
814 loader = unittest.TestLoader()
815 try:
816 loader.loadTestsFromNames(['testcase_1.testfoo'], m)
817 except AttributeError as e:
818 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
819 else:
820 self.fail("Failed to raise AttributeError")
821
822 # "The specifier name is a ``dotted name'' that may resolve ... to
823 # ... a callable object which returns a ... TestSuite instance"
824 def test_loadTestsFromNames__callable__TestSuite(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000825 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000826 testcase_1 = unittest.FunctionTestCase(lambda: None)
827 testcase_2 = unittest.FunctionTestCase(lambda: None)
828 def return_TestSuite():
829 return unittest.TestSuite([testcase_1, testcase_2])
830 m.return_TestSuite = return_TestSuite
831
832 loader = unittest.TestLoader()
833 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
834 self.failUnless(isinstance(suite, loader.suiteClass))
835
836 expected = unittest.TestSuite([testcase_1, testcase_2])
837 self.assertEqual(list(suite), [expected])
838
839 # "The specifier name is a ``dotted name'' that may resolve ... to
840 # ... a callable object which returns a TestCase ... instance"
841 def test_loadTestsFromNames__callable__TestCase_instance(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000842 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000843 testcase_1 = unittest.FunctionTestCase(lambda: None)
844 def return_TestCase():
845 return testcase_1
846 m.return_TestCase = return_TestCase
847
848 loader = unittest.TestLoader()
849 suite = loader.loadTestsFromNames(['return_TestCase'], m)
850 self.failUnless(isinstance(suite, loader.suiteClass))
851
852 ref_suite = unittest.TestSuite([testcase_1])
853 self.assertEqual(list(suite), [ref_suite])
854
855 # "The specifier name is a ``dotted name'' that may resolve ... to
856 # ... a callable object which returns a TestCase or TestSuite instance"
857 #
858 # Are staticmethods handled correctly?
859 def test_loadTestsFromNames__callable__call_staticmethod(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000860 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000861 class Test1(unittest.TestCase):
862 def test(self):
863 pass
864
865 testcase_1 = Test1('test')
866 class Foo(unittest.TestCase):
867 @staticmethod
868 def foo():
869 return testcase_1
870 m.Foo = Foo
871
872 loader = unittest.TestLoader()
873 suite = loader.loadTestsFromNames(['Foo.foo'], m)
874 self.failUnless(isinstance(suite, loader.suiteClass))
875
876 ref_suite = unittest.TestSuite([testcase_1])
877 self.assertEqual(list(suite), [ref_suite])
878
879 # "The specifier name is a ``dotted name'' that may resolve ... to
880 # ... a callable object which returns a TestCase or TestSuite instance"
881 #
882 # What happens when the callable returns something else?
883 def test_loadTestsFromNames__callable__wrong_type(self):
Christian Heimes45f9af32007-11-27 21:50:00 +0000884 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000885 def return_wrong():
886 return 6
887 m.return_wrong = return_wrong
888
889 loader = unittest.TestLoader()
890 try:
891 suite = loader.loadTestsFromNames(['return_wrong'], m)
892 except TypeError:
893 pass
894 else:
895 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
896
897 # "The specifier can refer to modules and packages which have not been
898 # imported; they will be imported as a side-effect"
899 def test_loadTestsFromNames__module_not_loaded(self):
900 # We're going to try to load this module as a side-effect, so it
901 # better not be loaded before we try.
902 #
903 # Why pick audioop? Google shows it isn't used very often, so there's
904 # a good chance that it won't be imported when this test is run
905 module_name = 'audioop'
906
907 import sys
908 if module_name in sys.modules:
909 del sys.modules[module_name]
910
911 loader = unittest.TestLoader()
912 try:
913 suite = loader.loadTestsFromNames([module_name])
914
915 self.failUnless(isinstance(suite, loader.suiteClass))
916 self.assertEqual(list(suite), [unittest.TestSuite()])
917
918 # audioop should now be loaded, thanks to loadTestsFromName()
919 self.failUnless(module_name in sys.modules)
920 finally:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000921 if module_name in sys.modules:
922 del sys.modules[module_name]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000923
924 ################################################################
925 ### /Tests for TestLoader.loadTestsFromNames()
926
927 ### Tests for TestLoader.getTestCaseNames()
928 ################################################################
929
930 # "Return a sorted sequence of method names found within testCaseClass"
931 #
932 # Test.foobar is defined to make sure getTestCaseNames() respects
933 # loader.testMethodPrefix
934 def test_getTestCaseNames(self):
935 class Test(unittest.TestCase):
936 def test_1(self): pass
937 def test_2(self): pass
938 def foobar(self): pass
939
940 loader = unittest.TestLoader()
941
942 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
943
944 # "Return a sorted sequence of method names found within testCaseClass"
945 #
946 # Does getTestCaseNames() behave appropriately if no tests are found?
947 def test_getTestCaseNames__no_tests(self):
948 class Test(unittest.TestCase):
949 def foobar(self): pass
950
951 loader = unittest.TestLoader()
952
953 self.assertEqual(loader.getTestCaseNames(Test), [])
954
955 # "Return a sorted sequence of method names found within testCaseClass"
956 #
957 # Are not-TestCases handled gracefully?
958 #
959 # XXX This should raise a TypeError, not return a list
960 #
961 # XXX It's too late in the 2.5 release cycle to fix this, but it should
962 # probably be revisited for 2.6
963 def test_getTestCaseNames__not_a_TestCase(self):
964 class BadCase(int):
965 def test_foo(self):
966 pass
967
968 loader = unittest.TestLoader()
969 names = loader.getTestCaseNames(BadCase)
970
971 self.assertEqual(names, ['test_foo'])
972
973 # "Return a sorted sequence of method names found within testCaseClass"
974 #
975 # Make sure inherited names are handled.
976 #
977 # TestP.foobar is defined to make sure getTestCaseNames() respects
978 # loader.testMethodPrefix
979 def test_getTestCaseNames__inheritance(self):
980 class TestP(unittest.TestCase):
981 def test_1(self): pass
982 def test_2(self): pass
983 def foobar(self): pass
984
985 class TestC(TestP):
986 def test_1(self): pass
987 def test_3(self): pass
988
989 loader = unittest.TestLoader()
990
991 names = ['test_1', 'test_2', 'test_3']
992 self.assertEqual(loader.getTestCaseNames(TestC), names)
993
994 ################################################################
995 ### /Tests for TestLoader.getTestCaseNames()
996
997 ### Tests for TestLoader.testMethodPrefix
998 ################################################################
999
1000 # "String giving the prefix of method names which will be interpreted as
1001 # test methods"
1002 #
1003 # Implicit in the documentation is that testMethodPrefix is respected by
1004 # all loadTestsFrom* methods.
1005 def test_testMethodPrefix__loadTestsFromTestCase(self):
1006 class Foo(unittest.TestCase):
1007 def test_1(self): pass
1008 def test_2(self): pass
1009 def foo_bar(self): pass
1010
1011 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1012 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1013
1014 loader = unittest.TestLoader()
1015 loader.testMethodPrefix = 'foo'
1016 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1017
1018 loader.testMethodPrefix = 'test'
1019 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
1020
1021 # "String giving the prefix of method names which will be interpreted as
1022 # test methods"
1023 #
1024 # Implicit in the documentation is that testMethodPrefix is respected by
1025 # all loadTestsFrom* methods.
1026 def test_testMethodPrefix__loadTestsFromModule(self):
Christian Heimes45f9af32007-11-27 21:50:00 +00001027 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001028 class Foo(unittest.TestCase):
1029 def test_1(self): pass
1030 def test_2(self): pass
1031 def foo_bar(self): pass
1032 m.Foo = Foo
1033
1034 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
1035 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
1036
1037 loader = unittest.TestLoader()
1038 loader.testMethodPrefix = 'foo'
1039 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1040
1041 loader.testMethodPrefix = 'test'
1042 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
1043
1044 # "String giving the prefix of method names which will be interpreted as
1045 # test methods"
1046 #
1047 # Implicit in the documentation is that testMethodPrefix is respected by
1048 # all loadTestsFrom* methods.
1049 def test_testMethodPrefix__loadTestsFromName(self):
Christian Heimes45f9af32007-11-27 21:50:00 +00001050 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051 class Foo(unittest.TestCase):
1052 def test_1(self): pass
1053 def test_2(self): pass
1054 def foo_bar(self): pass
1055 m.Foo = Foo
1056
1057 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1058 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1059
1060 loader = unittest.TestLoader()
1061 loader.testMethodPrefix = 'foo'
1062 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1063
1064 loader.testMethodPrefix = 'test'
1065 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
1066
1067 # "String giving the prefix of method names which will be interpreted as
1068 # test methods"
1069 #
1070 # Implicit in the documentation is that testMethodPrefix is respected by
1071 # all loadTestsFrom* methods.
1072 def test_testMethodPrefix__loadTestsFromNames(self):
Christian Heimes45f9af32007-11-27 21:50:00 +00001073 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001074 class Foo(unittest.TestCase):
1075 def test_1(self): pass
1076 def test_2(self): pass
1077 def foo_bar(self): pass
1078 m.Foo = Foo
1079
1080 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
1081 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1082 tests_2 = unittest.TestSuite([tests_2])
1083
1084 loader = unittest.TestLoader()
1085 loader.testMethodPrefix = 'foo'
1086 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1087
1088 loader.testMethodPrefix = 'test'
1089 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
1090
1091 # "The default value is 'test'"
1092 def test_testMethodPrefix__default_value(self):
1093 loader = unittest.TestLoader()
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001094 self.assertEqual(loader.testMethodPrefix, 'test')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001095
1096 ################################################################
1097 ### /Tests for TestLoader.testMethodPrefix
1098
1099 ### Tests for TestLoader.sortTestMethodsUsing
1100 ################################################################
1101
1102 # "Function to be used to compare method names when sorting them in
1103 # getTestCaseNames() and all the loadTestsFromX() methods"
1104 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1105 def reversed_cmp(x, y):
1106 return -cmp(x, y)
1107
1108 class Foo(unittest.TestCase):
1109 def test_1(self): pass
1110 def test_2(self): pass
1111
1112 loader = unittest.TestLoader()
1113 loader.sortTestMethodsUsing = reversed_cmp
1114
1115 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1116 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1117
1118 # "Function to be used to compare method names when sorting them in
1119 # getTestCaseNames() and all the loadTestsFromX() methods"
1120 def test_sortTestMethodsUsing__loadTestsFromModule(self):
1121 def reversed_cmp(x, y):
1122 return -cmp(x, y)
1123
Christian Heimes45f9af32007-11-27 21:50:00 +00001124 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001125 class Foo(unittest.TestCase):
1126 def test_1(self): pass
1127 def test_2(self): pass
1128 m.Foo = Foo
1129
1130 loader = unittest.TestLoader()
1131 loader.sortTestMethodsUsing = reversed_cmp
1132
1133 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1134 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
1135
1136 # "Function to be used to compare method names when sorting them in
1137 # getTestCaseNames() and all the loadTestsFromX() methods"
1138 def test_sortTestMethodsUsing__loadTestsFromName(self):
1139 def reversed_cmp(x, y):
1140 return -cmp(x, y)
1141
Christian Heimes45f9af32007-11-27 21:50:00 +00001142 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001143 class Foo(unittest.TestCase):
1144 def test_1(self): pass
1145 def test_2(self): pass
1146 m.Foo = Foo
1147
1148 loader = unittest.TestLoader()
1149 loader.sortTestMethodsUsing = reversed_cmp
1150
1151 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1152 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1153
1154 # "Function to be used to compare method names when sorting them in
1155 # getTestCaseNames() and all the loadTestsFromX() methods"
1156 def test_sortTestMethodsUsing__loadTestsFromNames(self):
1157 def reversed_cmp(x, y):
1158 return -cmp(x, y)
1159
Christian Heimes45f9af32007-11-27 21:50:00 +00001160 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001161 class Foo(unittest.TestCase):
1162 def test_1(self): pass
1163 def test_2(self): pass
1164 m.Foo = Foo
1165
1166 loader = unittest.TestLoader()
1167 loader.sortTestMethodsUsing = reversed_cmp
1168
1169 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1170 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
1171
1172 # "Function to be used to compare method names when sorting them in
1173 # getTestCaseNames()"
1174 #
1175 # Does it actually affect getTestCaseNames()?
1176 def test_sortTestMethodsUsing__getTestCaseNames(self):
1177 def reversed_cmp(x, y):
1178 return -cmp(x, y)
1179
1180 class Foo(unittest.TestCase):
1181 def test_1(self): pass
1182 def test_2(self): pass
1183
1184 loader = unittest.TestLoader()
1185 loader.sortTestMethodsUsing = reversed_cmp
1186
1187 test_names = ['test_2', 'test_1']
1188 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
1189
1190 # "The default value is the built-in cmp() function"
1191 def test_sortTestMethodsUsing__default_value(self):
1192 loader = unittest.TestLoader()
1193 self.failUnless(loader.sortTestMethodsUsing is cmp)
1194
1195 # "it can be set to None to disable the sort."
1196 #
1197 # XXX How is this different from reassigning cmp? Are the tests returned
1198 # in a random order or something? This behaviour should die
1199 def test_sortTestMethodsUsing__None(self):
1200 class Foo(unittest.TestCase):
1201 def test_1(self): pass
1202 def test_2(self): pass
1203
1204 loader = unittest.TestLoader()
1205 loader.sortTestMethodsUsing = None
1206
1207 test_names = ['test_2', 'test_1']
1208 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
1209
1210 ################################################################
1211 ### /Tests for TestLoader.sortTestMethodsUsing
1212
1213 ### Tests for TestLoader.suiteClass
1214 ################################################################
1215
1216 # "Callable object that constructs a test suite from a list of tests."
1217 def test_suiteClass__loadTestsFromTestCase(self):
1218 class Foo(unittest.TestCase):
1219 def test_1(self): pass
1220 def test_2(self): pass
1221 def foo_bar(self): pass
1222
1223 tests = [Foo('test_1'), Foo('test_2')]
1224
1225 loader = unittest.TestLoader()
1226 loader.suiteClass = list
1227 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1228
1229 # It is implicit in the documentation for TestLoader.suiteClass that
1230 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1231 def test_suiteClass__loadTestsFromModule(self):
Christian Heimes45f9af32007-11-27 21:50:00 +00001232 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001233 class Foo(unittest.TestCase):
1234 def test_1(self): pass
1235 def test_2(self): pass
1236 def foo_bar(self): pass
1237 m.Foo = Foo
1238
1239 tests = [[Foo('test_1'), Foo('test_2')]]
1240
1241 loader = unittest.TestLoader()
1242 loader.suiteClass = list
1243 self.assertEqual(loader.loadTestsFromModule(m), tests)
1244
1245 # It is implicit in the documentation for TestLoader.suiteClass that
1246 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1247 def test_suiteClass__loadTestsFromName(self):
Christian Heimes45f9af32007-11-27 21:50:00 +00001248 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001249 class Foo(unittest.TestCase):
1250 def test_1(self): pass
1251 def test_2(self): pass
1252 def foo_bar(self): pass
1253 m.Foo = Foo
1254
1255 tests = [Foo('test_1'), Foo('test_2')]
1256
1257 loader = unittest.TestLoader()
1258 loader.suiteClass = list
1259 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1260
1261 # It is implicit in the documentation for TestLoader.suiteClass that
1262 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1263 def test_suiteClass__loadTestsFromNames(self):
Christian Heimes45f9af32007-11-27 21:50:00 +00001264 m = types.ModuleType('m')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001265 class Foo(unittest.TestCase):
1266 def test_1(self): pass
1267 def test_2(self): pass
1268 def foo_bar(self): pass
1269 m.Foo = Foo
1270
1271 tests = [[Foo('test_1'), Foo('test_2')]]
1272
1273 loader = unittest.TestLoader()
1274 loader.suiteClass = list
1275 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
1276
1277 # "The default value is the TestSuite class"
1278 def test_suiteClass__default_value(self):
1279 loader = unittest.TestLoader()
1280 self.failUnless(loader.suiteClass is unittest.TestSuite)
1281
1282 ################################################################
1283 ### /Tests for TestLoader.suiteClass
1284
1285### Support code for Test_TestSuite
1286################################################################
1287
1288class Foo(unittest.TestCase):
1289 def test_1(self): pass
1290 def test_2(self): pass
1291 def test_3(self): pass
1292 def runTest(self): pass
1293
1294def _mk_TestSuite(*names):
1295 return unittest.TestSuite(Foo(n) for n in names)
1296
1297################################################################
1298### /Support code for Test_TestSuite
1299
1300class Test_TestSuite(TestCase, TestEquality):
1301
1302 ### Set up attributes needed by inherited tests
1303 ################################################################
1304
1305 # Used by TestEquality.test_eq
1306 eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
1307 ,(unittest.TestSuite(), unittest.TestSuite([]))
1308 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
1309
1310 # Used by TestEquality.test_ne
1311 ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
1312 ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
1313 ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
1314 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
1315
1316 ################################################################
1317 ### /Set up attributes needed by inherited tests
1318
1319 ### Tests for TestSuite.__init__
1320 ################################################################
1321
1322 # "class TestSuite([tests])"
1323 #
1324 # The tests iterable should be optional
1325 def test_init__tests_optional(self):
1326 suite = unittest.TestSuite()
1327
1328 self.assertEqual(suite.countTestCases(), 0)
1329
1330 # "class TestSuite([tests])"
1331 # ...
1332 # "If tests is given, it must be an iterable of individual test cases
1333 # or other test suites that will be used to build the suite initially"
1334 #
1335 # TestSuite should deal with empty tests iterables by allowing the
1336 # creation of an empty suite
1337 def test_init__empty_tests(self):
1338 suite = unittest.TestSuite([])
1339
1340 self.assertEqual(suite.countTestCases(), 0)
1341
1342 # "class TestSuite([tests])"
1343 # ...
1344 # "If tests is given, it must be an iterable of individual test cases
1345 # or other test suites that will be used to build the suite initially"
1346 #
1347 # TestSuite should allow any iterable to provide tests
1348 def test_init__tests_from_any_iterable(self):
1349 def tests():
1350 yield unittest.FunctionTestCase(lambda: None)
1351 yield unittest.FunctionTestCase(lambda: None)
1352
1353 suite_1 = unittest.TestSuite(tests())
1354 self.assertEqual(suite_1.countTestCases(), 2)
1355
1356 suite_2 = unittest.TestSuite(suite_1)
1357 self.assertEqual(suite_2.countTestCases(), 2)
1358
1359 suite_3 = unittest.TestSuite(set(suite_1))
1360 self.assertEqual(suite_3.countTestCases(), 2)
1361
1362 # "class TestSuite([tests])"
1363 # ...
1364 # "If tests is given, it must be an iterable of individual test cases
1365 # or other test suites that will be used to build the suite initially"
1366 #
1367 # Does TestSuite() also allow other TestSuite() instances to be present
1368 # in the tests iterable?
1369 def test_init__TestSuite_instances_in_tests(self):
1370 def tests():
1371 ftc = unittest.FunctionTestCase(lambda: None)
1372 yield unittest.TestSuite([ftc])
1373 yield unittest.FunctionTestCase(lambda: None)
1374
1375 suite = unittest.TestSuite(tests())
1376 self.assertEqual(suite.countTestCases(), 2)
1377
1378 ################################################################
1379 ### /Tests for TestSuite.__init__
1380
1381 # Container types should support the iter protocol
1382 def test_iter(self):
1383 test1 = unittest.FunctionTestCase(lambda: None)
1384 test2 = unittest.FunctionTestCase(lambda: None)
1385 suite = unittest.TestSuite((test1, test2))
1386
1387 self.assertEqual(list(suite), [test1, test2])
1388
1389 # "Return the number of tests represented by the this test object.
1390 # ...this method is also implemented by the TestSuite class, which can
1391 # return larger [greater than 1] values"
1392 #
1393 # Presumably an empty TestSuite returns 0?
1394 def test_countTestCases_zero_simple(self):
1395 suite = unittest.TestSuite()
1396
1397 self.assertEqual(suite.countTestCases(), 0)
1398
1399 # "Return the number of tests represented by the this test object.
1400 # ...this method is also implemented by the TestSuite class, which can
1401 # return larger [greater than 1] values"
1402 #
1403 # Presumably an empty TestSuite (even if it contains other empty
1404 # TestSuite instances) returns 0?
1405 def test_countTestCases_zero_nested(self):
1406 class Test1(unittest.TestCase):
1407 def test(self):
1408 pass
1409
1410 suite = unittest.TestSuite([unittest.TestSuite()])
1411
1412 self.assertEqual(suite.countTestCases(), 0)
1413
1414 # "Return the number of tests represented by the this test object.
1415 # ...this method is also implemented by the TestSuite class, which can
1416 # return larger [greater than 1] values"
1417 def test_countTestCases_simple(self):
1418 test1 = unittest.FunctionTestCase(lambda: None)
1419 test2 = unittest.FunctionTestCase(lambda: None)
1420 suite = unittest.TestSuite((test1, test2))
1421
1422 self.assertEqual(suite.countTestCases(), 2)
1423
1424 # "Return the number of tests represented by the this test object.
1425 # ...this method is also implemented by the TestSuite class, which can
1426 # return larger [greater than 1] values"
1427 #
1428 # Make sure this holds for nested TestSuite instances, too
1429 def test_countTestCases_nested(self):
1430 class Test1(unittest.TestCase):
1431 def test1(self): pass
1432 def test2(self): pass
1433
1434 test2 = unittest.FunctionTestCase(lambda: None)
1435 test3 = unittest.FunctionTestCase(lambda: None)
1436 child = unittest.TestSuite((Test1('test2'), test2))
1437 parent = unittest.TestSuite((test3, child, Test1('test1')))
1438
1439 self.assertEqual(parent.countTestCases(), 4)
1440
1441 # "Run the tests associated with this suite, collecting the result into
1442 # the test result object passed as result."
1443 #
1444 # And if there are no tests? What then?
1445 def test_run__empty_suite(self):
1446 events = []
1447 result = LoggingResult(events)
1448
1449 suite = unittest.TestSuite()
1450
1451 suite.run(result)
1452
1453 self.assertEqual(events, [])
1454
1455 # "Note that unlike TestCase.run(), TestSuite.run() requires the
1456 # "result object to be passed in."
1457 def test_run__requires_result(self):
1458 suite = unittest.TestSuite()
1459
1460 try:
1461 suite.run()
1462 except TypeError:
1463 pass
1464 else:
1465 self.fail("Failed to raise TypeError")
1466
1467 # "Run the tests associated with this suite, collecting the result into
1468 # the test result object passed as result."
1469 def test_run(self):
1470 events = []
1471 result = LoggingResult(events)
1472
1473 class LoggingCase(unittest.TestCase):
1474 def run(self, result):
1475 events.append('run %s' % self._testMethodName)
1476
1477 def test1(self): pass
1478 def test2(self): pass
1479
1480 tests = [LoggingCase('test1'), LoggingCase('test2')]
1481
1482 unittest.TestSuite(tests).run(result)
1483
1484 self.assertEqual(events, ['run test1', 'run test2'])
1485
1486 # "Add a TestCase ... to the suite"
1487 def test_addTest__TestCase(self):
1488 class Foo(unittest.TestCase):
1489 def test(self): pass
1490
1491 test = Foo('test')
1492 suite = unittest.TestSuite()
1493
1494 suite.addTest(test)
1495
1496 self.assertEqual(suite.countTestCases(), 1)
1497 self.assertEqual(list(suite), [test])
1498
1499 # "Add a ... TestSuite to the suite"
1500 def test_addTest__TestSuite(self):
1501 class Foo(unittest.TestCase):
1502 def test(self): pass
1503
1504 suite_2 = unittest.TestSuite([Foo('test')])
1505
1506 suite = unittest.TestSuite()
1507 suite.addTest(suite_2)
1508
1509 self.assertEqual(suite.countTestCases(), 1)
1510 self.assertEqual(list(suite), [suite_2])
1511
1512 # "Add all the tests from an iterable of TestCase and TestSuite
1513 # instances to this test suite."
1514 #
1515 # "This is equivalent to iterating over tests, calling addTest() for
1516 # each element"
1517 def test_addTests(self):
1518 class Foo(unittest.TestCase):
1519 def test_1(self): pass
1520 def test_2(self): pass
1521
1522 test_1 = Foo('test_1')
1523 test_2 = Foo('test_2')
1524 inner_suite = unittest.TestSuite([test_2])
1525
1526 def gen():
1527 yield test_1
1528 yield test_2
1529 yield inner_suite
1530
1531 suite_1 = unittest.TestSuite()
1532 suite_1.addTests(gen())
1533
1534 self.assertEqual(list(suite_1), list(gen()))
1535
1536 # "This is equivalent to iterating over tests, calling addTest() for
1537 # each element"
1538 suite_2 = unittest.TestSuite()
1539 for t in gen():
1540 suite_2.addTest(t)
1541
1542 self.assertEqual(suite_1, suite_2)
1543
1544 # "Add all the tests from an iterable of TestCase and TestSuite
1545 # instances to this test suite."
1546 #
1547 # What happens if it doesn't get an iterable?
1548 def test_addTest__noniterable(self):
1549 suite = unittest.TestSuite()
1550
1551 try:
1552 suite.addTests(5)
1553 except TypeError:
1554 pass
1555 else:
1556 self.fail("Failed to raise TypeError")
1557
1558 def test_addTest__noncallable(self):
1559 suite = unittest.TestSuite()
1560 self.assertRaises(TypeError, suite.addTest, 5)
1561
1562 def test_addTest__casesuiteclass(self):
1563 suite = unittest.TestSuite()
1564 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
1565 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
1566
1567 def test_addTests__string(self):
1568 suite = unittest.TestSuite()
1569 self.assertRaises(TypeError, suite.addTests, "foo")
1570
1571
1572class Test_FunctionTestCase(TestCase):
1573
1574 # "Return the number of tests represented by the this test object. For
1575 # TestCase instances, this will always be 1"
1576 def test_countTestCases(self):
1577 test = unittest.FunctionTestCase(lambda: None)
1578
1579 self.assertEqual(test.countTestCases(), 1)
1580
1581 # "When a setUp() method is defined, the test runner will run that method
1582 # prior to each test. Likewise, if a tearDown() method is defined, the
1583 # test runner will invoke that method after each test. In the example,
1584 # setUp() was used to create a fresh sequence for each test."
1585 #
1586 # Make sure the proper call order is maintained, even if setUp() raises
1587 # an exception.
1588 def test_run_call_order__error_in_setUp(self):
1589 events = []
1590 result = LoggingResult(events)
1591
1592 def setUp():
1593 events.append('setUp')
1594 raise RuntimeError('raised by setUp')
1595
1596 def test():
1597 events.append('test')
1598
1599 def tearDown():
1600 events.append('tearDown')
1601
1602 expected = ['startTest', 'setUp', 'addError', 'stopTest']
1603 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1604 self.assertEqual(events, expected)
1605
1606 # "When a setUp() method is defined, the test runner will run that method
1607 # prior to each test. Likewise, if a tearDown() method is defined, the
1608 # test runner will invoke that method after each test. In the example,
1609 # setUp() was used to create a fresh sequence for each test."
1610 #
1611 # Make sure the proper call order is maintained, even if the test raises
1612 # an error (as opposed to a failure).
1613 def test_run_call_order__error_in_test(self):
1614 events = []
1615 result = LoggingResult(events)
1616
1617 def setUp():
1618 events.append('setUp')
1619
1620 def test():
1621 events.append('test')
1622 raise RuntimeError('raised by test')
1623
1624 def tearDown():
1625 events.append('tearDown')
1626
1627 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
1628 'stopTest']
1629 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1630 self.assertEqual(events, expected)
1631
1632 # "When a setUp() method is defined, the test runner will run that method
1633 # prior to each test. Likewise, if a tearDown() method is defined, the
1634 # test runner will invoke that method after each test. In the example,
1635 # setUp() was used to create a fresh sequence for each test."
1636 #
1637 # Make sure the proper call order is maintained, even if the test signals
1638 # a failure (as opposed to an error).
1639 def test_run_call_order__failure_in_test(self):
1640 events = []
1641 result = LoggingResult(events)
1642
1643 def setUp():
1644 events.append('setUp')
1645
1646 def test():
1647 events.append('test')
1648 self.fail('raised by test')
1649
1650 def tearDown():
1651 events.append('tearDown')
1652
1653 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
1654 'stopTest']
1655 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1656 self.assertEqual(events, expected)
1657
1658 # "When a setUp() method is defined, the test runner will run that method
1659 # prior to each test. Likewise, if a tearDown() method is defined, the
1660 # test runner will invoke that method after each test. In the example,
1661 # setUp() was used to create a fresh sequence for each test."
1662 #
1663 # Make sure the proper call order is maintained, even if tearDown() raises
1664 # an exception.
1665 def test_run_call_order__error_in_tearDown(self):
1666 events = []
1667 result = LoggingResult(events)
1668
1669 def setUp():
1670 events.append('setUp')
1671
1672 def test():
1673 events.append('test')
1674
1675 def tearDown():
1676 events.append('tearDown')
1677 raise RuntimeError('raised by tearDown')
1678
1679 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
1680 'stopTest']
1681 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1682 self.assertEqual(events, expected)
1683
1684 # "Return a string identifying the specific test case."
1685 #
1686 # Because of the vague nature of the docs, I'm not going to lock this
1687 # test down too much. Really all that can be asserted is that the id()
1688 # will be a string (either 8-byte or unicode -- again, because the docs
1689 # just say "string")
1690 def test_id(self):
1691 test = unittest.FunctionTestCase(lambda: None)
1692
Guido van Rossum3172c5d2007-10-16 18:12:55 +00001693 self.failUnless(isinstance(test.id(), str))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001694
1695 # "Returns a one-line description of the test, or None if no description
1696 # has been provided. The default implementation of this method returns
1697 # the first line of the test method's docstring, if available, or None."
1698 def test_shortDescription__no_docstring(self):
1699 test = unittest.FunctionTestCase(lambda: None)
1700
1701 self.assertEqual(test.shortDescription(), None)
1702
1703 # "Returns a one-line description of the test, or None if no description
1704 # has been provided. The default implementation of this method returns
1705 # the first line of the test method's docstring, if available, or None."
1706 def test_shortDescription__singleline_docstring(self):
1707 desc = "this tests foo"
1708 test = unittest.FunctionTestCase(lambda: None, description=desc)
1709
1710 self.assertEqual(test.shortDescription(), "this tests foo")
1711
1712class Test_TestResult(TestCase):
1713 # Note: there are not separate tests for TestResult.wasSuccessful(),
1714 # TestResult.errors, TestResult.failures, TestResult.testsRun or
1715 # TestResult.shouldStop because these only have meaning in terms of
1716 # other TestResult methods.
1717 #
1718 # Accordingly, tests for the aforenamed attributes are incorporated
1719 # in with the tests for the defining methods.
1720 ################################################################
1721
1722 def test_init(self):
1723 result = unittest.TestResult()
1724
1725 self.failUnless(result.wasSuccessful())
1726 self.assertEqual(len(result.errors), 0)
1727 self.assertEqual(len(result.failures), 0)
1728 self.assertEqual(result.testsRun, 0)
1729 self.assertEqual(result.shouldStop, False)
1730
1731 # "This method can be called to signal that the set of tests being
1732 # run should be aborted by setting the TestResult's shouldStop
1733 # attribute to True."
1734 def test_stop(self):
1735 result = unittest.TestResult()
1736
1737 result.stop()
1738
1739 self.assertEqual(result.shouldStop, True)
1740
1741 # "Called when the test case test is about to be run. The default
1742 # implementation simply increments the instance's testsRun counter."
1743 def test_startTest(self):
1744 class Foo(unittest.TestCase):
1745 def test_1(self):
1746 pass
1747
1748 test = Foo('test_1')
1749
1750 result = unittest.TestResult()
1751
1752 result.startTest(test)
1753
1754 self.failUnless(result.wasSuccessful())
1755 self.assertEqual(len(result.errors), 0)
1756 self.assertEqual(len(result.failures), 0)
1757 self.assertEqual(result.testsRun, 1)
1758 self.assertEqual(result.shouldStop, False)
1759
1760 result.stopTest(test)
1761
1762 # "Called after the test case test has been executed, regardless of
1763 # the outcome. The default implementation does nothing."
1764 def test_stopTest(self):
1765 class Foo(unittest.TestCase):
1766 def test_1(self):
1767 pass
1768
1769 test = Foo('test_1')
1770
1771 result = unittest.TestResult()
1772
1773 result.startTest(test)
1774
1775 self.failUnless(result.wasSuccessful())
1776 self.assertEqual(len(result.errors), 0)
1777 self.assertEqual(len(result.failures), 0)
1778 self.assertEqual(result.testsRun, 1)
1779 self.assertEqual(result.shouldStop, False)
1780
1781 result.stopTest(test)
1782
1783 # Same tests as above; make sure nothing has changed
1784 self.failUnless(result.wasSuccessful())
1785 self.assertEqual(len(result.errors), 0)
1786 self.assertEqual(len(result.failures), 0)
1787 self.assertEqual(result.testsRun, 1)
1788 self.assertEqual(result.shouldStop, False)
1789
1790 # "addSuccess(test)"
1791 # ...
1792 # "Called when the test case test succeeds"
1793 # ...
1794 # "wasSuccessful() - Returns True if all tests run so far have passed,
1795 # otherwise returns False"
1796 # ...
1797 # "testsRun - The total number of tests run so far."
1798 # ...
1799 # "errors - A list containing 2-tuples of TestCase instances and
1800 # formatted tracebacks. Each tuple represents a test which raised an
1801 # unexpected exception. Contains formatted
1802 # tracebacks instead of sys.exc_info() results."
1803 # ...
1804 # "failures - A list containing 2-tuples of TestCase instances and
1805 # formatted tracebacks. Each tuple represents a test where a failure was
1806 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1807 # methods. Contains formatted tracebacks instead
1808 # of sys.exc_info() results."
1809 def test_addSuccess(self):
1810 class Foo(unittest.TestCase):
1811 def test_1(self):
1812 pass
1813
1814 test = Foo('test_1')
1815
1816 result = unittest.TestResult()
1817
1818 result.startTest(test)
1819 result.addSuccess(test)
1820 result.stopTest(test)
1821
1822 self.failUnless(result.wasSuccessful())
1823 self.assertEqual(len(result.errors), 0)
1824 self.assertEqual(len(result.failures), 0)
1825 self.assertEqual(result.testsRun, 1)
1826 self.assertEqual(result.shouldStop, False)
1827
1828 # "addFailure(test, err)"
1829 # ...
1830 # "Called when the test case test signals a failure. err is a tuple of
1831 # the form returned by sys.exc_info(): (type, value, traceback)"
1832 # ...
1833 # "wasSuccessful() - Returns True if all tests run so far have passed,
1834 # otherwise returns False"
1835 # ...
1836 # "testsRun - The total number of tests run so far."
1837 # ...
1838 # "errors - A list containing 2-tuples of TestCase instances and
1839 # formatted tracebacks. Each tuple represents a test which raised an
1840 # unexpected exception. Contains formatted
1841 # tracebacks instead of sys.exc_info() results."
1842 # ...
1843 # "failures - A list containing 2-tuples of TestCase instances and
1844 # formatted tracebacks. Each tuple represents a test where a failure was
1845 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1846 # methods. Contains formatted tracebacks instead
1847 # of sys.exc_info() results."
1848 def test_addFailure(self):
1849 import sys
1850
1851 class Foo(unittest.TestCase):
1852 def test_1(self):
1853 pass
1854
1855 test = Foo('test_1')
1856 try:
1857 test.fail("foo")
1858 except:
1859 exc_info_tuple = sys.exc_info()
1860
1861 result = unittest.TestResult()
1862
1863 result.startTest(test)
1864 result.addFailure(test, exc_info_tuple)
1865 result.stopTest(test)
1866
1867 self.failIf(result.wasSuccessful())
1868 self.assertEqual(len(result.errors), 0)
1869 self.assertEqual(len(result.failures), 1)
1870 self.assertEqual(result.testsRun, 1)
1871 self.assertEqual(result.shouldStop, False)
1872
1873 test_case, formatted_exc = result.failures[0]
1874 self.failUnless(test_case is test)
1875 self.failUnless(isinstance(formatted_exc, str))
1876
1877 # "addError(test, err)"
1878 # ...
1879 # "Called when the test case test raises an unexpected exception err
1880 # is a tuple of the form returned by sys.exc_info():
1881 # (type, value, traceback)"
1882 # ...
1883 # "wasSuccessful() - Returns True if all tests run so far have passed,
1884 # otherwise returns False"
1885 # ...
1886 # "testsRun - The total number of tests run so far."
1887 # ...
1888 # "errors - A list containing 2-tuples of TestCase instances and
1889 # formatted tracebacks. Each tuple represents a test which raised an
1890 # unexpected exception. Contains formatted
1891 # tracebacks instead of sys.exc_info() results."
1892 # ...
1893 # "failures - A list containing 2-tuples of TestCase instances and
1894 # formatted tracebacks. Each tuple represents a test where a failure was
1895 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1896 # methods. Contains formatted tracebacks instead
1897 # of sys.exc_info() results."
1898 def test_addError(self):
1899 import sys
1900
1901 class Foo(unittest.TestCase):
1902 def test_1(self):
1903 pass
1904
1905 test = Foo('test_1')
1906 try:
1907 raise TypeError()
1908 except:
1909 exc_info_tuple = sys.exc_info()
1910
1911 result = unittest.TestResult()
1912
1913 result.startTest(test)
1914 result.addError(test, exc_info_tuple)
1915 result.stopTest(test)
1916
1917 self.failIf(result.wasSuccessful())
1918 self.assertEqual(len(result.errors), 1)
1919 self.assertEqual(len(result.failures), 0)
1920 self.assertEqual(result.testsRun, 1)
1921 self.assertEqual(result.shouldStop, False)
1922
1923 test_case, formatted_exc = result.errors[0]
1924 self.failUnless(test_case is test)
1925 self.failUnless(isinstance(formatted_exc, str))
1926
1927### Support code for Test_TestCase
1928################################################################
1929
1930class Foo(unittest.TestCase):
1931 def runTest(self): pass
1932 def test1(self): pass
1933
1934class Bar(Foo):
1935 def test2(self): pass
1936
1937################################################################
1938### /Support code for Test_TestCase
1939
1940class Test_TestCase(TestCase, TestEquality, TestHashing):
1941
1942 ### Set up attributes used by inherited tests
1943 ################################################################
1944
1945 # Used by TestHashing.test_hash and TestEquality.test_eq
1946 eq_pairs = [(Foo('test1'), Foo('test1'))]
1947
1948 # Used by TestEquality.test_ne
1949 ne_pairs = [(Foo('test1'), Foo('runTest'))
1950 ,(Foo('test1'), Bar('test1'))
1951 ,(Foo('test1'), Bar('test2'))]
1952
1953 ################################################################
1954 ### /Set up attributes used by inherited tests
1955
1956
1957 # "class TestCase([methodName])"
1958 # ...
1959 # "Each instance of TestCase will run a single test method: the
1960 # method named methodName."
1961 # ...
1962 # "methodName defaults to "runTest"."
1963 #
1964 # Make sure it really is optional, and that it defaults to the proper
1965 # thing.
1966 def test_init__no_test_name(self):
1967 class Test(unittest.TestCase):
1968 def runTest(self): raise MyException()
1969 def test(self): pass
1970
1971 self.assertEqual(Test().id()[-13:], '.Test.runTest')
1972
1973 # "class TestCase([methodName])"
1974 # ...
1975 # "Each instance of TestCase will run a single test method: the
1976 # method named methodName."
1977 def test_init__test_name__valid(self):
1978 class Test(unittest.TestCase):
1979 def runTest(self): raise MyException()
1980 def test(self): pass
1981
1982 self.assertEqual(Test('test').id()[-10:], '.Test.test')
1983
1984 # "class TestCase([methodName])"
1985 # ...
1986 # "Each instance of TestCase will run a single test method: the
1987 # method named methodName."
1988 def test_init__test_name__invalid(self):
1989 class Test(unittest.TestCase):
1990 def runTest(self): raise MyException()
1991 def test(self): pass
1992
1993 try:
1994 Test('testfoo')
1995 except ValueError:
1996 pass
1997 else:
1998 self.fail("Failed to raise ValueError")
1999
2000 # "Return the number of tests represented by the this test object. For
2001 # TestCase instances, this will always be 1"
2002 def test_countTestCases(self):
2003 class Foo(unittest.TestCase):
2004 def test(self): pass
2005
2006 self.assertEqual(Foo('test').countTestCases(), 1)
2007
2008 # "Return the default type of test result object to be used to run this
2009 # test. For TestCase instances, this will always be
2010 # unittest.TestResult; subclasses of TestCase should
2011 # override this as necessary."
2012 def test_defaultTestResult(self):
2013 class Foo(unittest.TestCase):
2014 def runTest(self):
2015 pass
2016
2017 result = Foo().defaultTestResult()
2018 self.assertEqual(type(result), unittest.TestResult)
2019
2020 # "When a setUp() method is defined, the test runner will run that method
2021 # prior to each test. Likewise, if a tearDown() method is defined, the
2022 # test runner will invoke that method after each test. In the example,
2023 # setUp() was used to create a fresh sequence for each test."
2024 #
2025 # Make sure the proper call order is maintained, even if setUp() raises
2026 # an exception.
2027 def test_run_call_order__error_in_setUp(self):
2028 events = []
2029 result = LoggingResult(events)
2030
2031 class Foo(unittest.TestCase):
2032 def setUp(self):
2033 events.append('setUp')
2034 raise RuntimeError('raised by Foo.setUp')
2035
2036 def test(self):
2037 events.append('test')
2038
2039 def tearDown(self):
2040 events.append('tearDown')
2041
2042 Foo('test').run(result)
2043 expected = ['startTest', 'setUp', 'addError', 'stopTest']
2044 self.assertEqual(events, expected)
2045
2046 # "When a setUp() method is defined, the test runner will run that method
2047 # prior to each test. Likewise, if a tearDown() method is defined, the
2048 # test runner will invoke that method after each test. In the example,
2049 # setUp() was used to create a fresh sequence for each test."
2050 #
2051 # Make sure the proper call order is maintained, even if the test raises
2052 # an error (as opposed to a failure).
2053 def test_run_call_order__error_in_test(self):
2054 events = []
2055 result = LoggingResult(events)
2056
2057 class Foo(unittest.TestCase):
2058 def setUp(self):
2059 events.append('setUp')
2060
2061 def test(self):
2062 events.append('test')
2063 raise RuntimeError('raised by Foo.test')
2064
2065 def tearDown(self):
2066 events.append('tearDown')
2067
2068 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
2069 'stopTest']
2070 Foo('test').run(result)
2071 self.assertEqual(events, expected)
2072
2073 # "When a setUp() method is defined, the test runner will run that method
2074 # prior to each test. Likewise, if a tearDown() method is defined, the
2075 # test runner will invoke that method after each test. In the example,
2076 # setUp() was used to create a fresh sequence for each test."
2077 #
2078 # Make sure the proper call order is maintained, even if the test signals
2079 # a failure (as opposed to an error).
2080 def test_run_call_order__failure_in_test(self):
2081 events = []
2082 result = LoggingResult(events)
2083
2084 class Foo(unittest.TestCase):
2085 def setUp(self):
2086 events.append('setUp')
2087
2088 def test(self):
2089 events.append('test')
2090 self.fail('raised by Foo.test')
2091
2092 def tearDown(self):
2093 events.append('tearDown')
2094
2095 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
2096 'stopTest']
2097 Foo('test').run(result)
2098 self.assertEqual(events, expected)
2099
2100 # "When a setUp() method is defined, the test runner will run that method
2101 # prior to each test. Likewise, if a tearDown() method is defined, the
2102 # test runner will invoke that method after each test. In the example,
2103 # setUp() was used to create a fresh sequence for each test."
2104 #
2105 # Make sure the proper call order is maintained, even if tearDown() raises
2106 # an exception.
2107 def test_run_call_order__error_in_tearDown(self):
2108 events = []
2109 result = LoggingResult(events)
2110
2111 class Foo(unittest.TestCase):
2112 def setUp(self):
2113 events.append('setUp')
2114
2115 def test(self):
2116 events.append('test')
2117
2118 def tearDown(self):
2119 events.append('tearDown')
2120 raise RuntimeError('raised by Foo.tearDown')
2121
2122 Foo('test').run(result)
2123 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
2124 'stopTest']
2125 self.assertEqual(events, expected)
2126
2127 # "This class attribute gives the exception raised by the test() method.
2128 # If a test framework needs to use a specialized exception, possibly to
2129 # carry additional information, it must subclass this exception in
2130 # order to ``play fair'' with the framework. The initial value of this
2131 # attribute is AssertionError"
2132 def test_failureException__default(self):
2133 class Foo(unittest.TestCase):
2134 def test(self):
2135 pass
2136
2137 self.failUnless(Foo('test').failureException is AssertionError)
2138
2139 # "This class attribute gives the exception raised by the test() method.
2140 # If a test framework needs to use a specialized exception, possibly to
2141 # carry additional information, it must subclass this exception in
2142 # order to ``play fair'' with the framework."
2143 #
2144 # Make sure TestCase.run() respects the designated failureException
2145 def test_failureException__subclassing__explicit_raise(self):
2146 events = []
2147 result = LoggingResult(events)
2148
2149 class Foo(unittest.TestCase):
2150 def test(self):
2151 raise RuntimeError()
2152
2153 failureException = RuntimeError
2154
2155 self.failUnless(Foo('test').failureException is RuntimeError)
2156
2157
2158 Foo('test').run(result)
2159 expected = ['startTest', 'addFailure', 'stopTest']
2160 self.assertEqual(events, expected)
2161
2162 # "This class attribute gives the exception raised by the test() method.
2163 # If a test framework needs to use a specialized exception, possibly to
2164 # carry additional information, it must subclass this exception in
2165 # order to ``play fair'' with the framework."
2166 #
2167 # Make sure TestCase.run() respects the designated failureException
2168 def test_failureException__subclassing__implicit_raise(self):
2169 events = []
2170 result = LoggingResult(events)
2171
2172 class Foo(unittest.TestCase):
2173 def test(self):
2174 self.fail("foo")
2175
2176 failureException = RuntimeError
2177
2178 self.failUnless(Foo('test').failureException is RuntimeError)
2179
2180
2181 Foo('test').run(result)
2182 expected = ['startTest', 'addFailure', 'stopTest']
2183 self.assertEqual(events, expected)
2184
2185 # "The default implementation does nothing."
2186 def test_setUp(self):
2187 class Foo(unittest.TestCase):
2188 def runTest(self):
2189 pass
2190
2191 # ... and nothing should happen
2192 Foo().setUp()
2193
2194 # "The default implementation does nothing."
2195 def test_tearDown(self):
2196 class Foo(unittest.TestCase):
2197 def runTest(self):
2198 pass
2199
2200 # ... and nothing should happen
2201 Foo().tearDown()
2202
2203 # "Return a string identifying the specific test case."
2204 #
2205 # Because of the vague nature of the docs, I'm not going to lock this
2206 # test down too much. Really all that can be asserted is that the id()
2207 # will be a string (either 8-byte or unicode -- again, because the docs
2208 # just say "string")
2209 def test_id(self):
2210 class Foo(unittest.TestCase):
2211 def runTest(self):
2212 pass
2213
Guido van Rossum3172c5d2007-10-16 18:12:55 +00002214 self.failUnless(isinstance(Foo().id(), str))
Guido van Rossumd8faa362007-04-27 19:54:29 +00002215
2216 # "Returns a one-line description of the test, or None if no description
2217 # has been provided. The default implementation of this method returns
2218 # the first line of the test method's docstring, if available, or None."
2219 def test_shortDescription__no_docstring(self):
2220 class Foo(unittest.TestCase):
2221 def runTest(self):
2222 pass
2223
2224 self.assertEqual(Foo().shortDescription(), None)
2225
2226 # "Returns a one-line description of the test, or None if no description
2227 # has been provided. The default implementation of this method returns
2228 # the first line of the test method's docstring, if available, or None."
2229 def test_shortDescription__singleline_docstring(self):
2230 class Foo(unittest.TestCase):
2231 def runTest(self):
2232 "this tests foo"
2233 pass
2234
2235 self.assertEqual(Foo().shortDescription(), "this tests foo")
2236
2237 # "Returns a one-line description of the test, or None if no description
2238 # has been provided. The default implementation of this method returns
2239 # the first line of the test method's docstring, if available, or None."
2240 def test_shortDescription__multiline_docstring(self):
2241 class Foo(unittest.TestCase):
2242 def runTest(self):
2243 """this tests foo
2244 blah, bar and baz are also tested"""
2245 pass
2246
2247 self.assertEqual(Foo().shortDescription(), "this tests foo")
2248
2249 # "If result is omitted or None, a temporary result object is created
2250 # and used, but is not made available to the caller"
2251 def test_run__uses_defaultTestResult(self):
2252 events = []
2253
2254 class Foo(unittest.TestCase):
2255 def test(self):
2256 events.append('test')
2257
2258 def defaultTestResult(self):
2259 return LoggingResult(events)
2260
2261 # Make run() find a result object on its own
2262 Foo('test').run()
2263
2264 expected = ['startTest', 'test', 'stopTest']
2265 self.assertEqual(events, expected)
Jim Fultonfafd8742004-08-28 15:22:12 +00002266
Jeffrey Yasskinaaaef112007-09-07 15:00:39 +00002267class Test_Assertions(TestCase):
2268 def test_AlmostEqual(self):
2269 self.failUnlessAlmostEqual(1.00000001, 1.0)
2270 self.failIfAlmostEqual(1.0000001, 1.0)
2271 self.assertRaises(AssertionError,
2272 self.failUnlessAlmostEqual, 1.0000001, 1.0)
2273 self.assertRaises(AssertionError,
2274 self.failIfAlmostEqual, 1.00000001, 1.0)
2275
2276 self.failUnlessAlmostEqual(1.1, 1.0, places=0)
2277 self.assertRaises(AssertionError,
2278 self.failUnlessAlmostEqual, 1.1, 1.0, places=1)
2279
2280 self.failUnlessAlmostEqual(0, .1+.1j, places=0)
2281 self.failIfAlmostEqual(0, .1+.1j, places=1)
2282 self.assertRaises(AssertionError,
2283 self.failUnlessAlmostEqual, 0, .1+.1j, places=1)
2284 self.assertRaises(AssertionError,
2285 self.failIfAlmostEqual, 0, .1+.1j, places=0)
2286
Jim Fultonfafd8742004-08-28 15:22:12 +00002287######################################################################
2288## Main
2289######################################################################
2290
2291def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002292 support.run_unittest(Test_TestCase, Test_TestLoader,
Jeffrey Yasskinaaaef112007-09-07 15:00:39 +00002293 Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
2294 Test_Assertions)
Jim Fultonfafd8742004-08-28 15:22:12 +00002295
Guido van Rossumd8faa362007-04-27 19:54:29 +00002296if __name__ == "__main__":
Jim Fultonfafd8742004-08-28 15:22:12 +00002297 test_main()