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