blob: ac52e720c025e5ccf488cba3385fc60da860209b [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")
Georg Brandld9e50262007-03-07 11:54:49 +00001583
1584 def test_addTest__noncallable(self):
1585 suite = unittest.TestSuite()
1586 self.assertRaises(TypeError, suite.addTest, 5)
1587
1588 def test_addTest__casesuiteclass(self):
1589 suite = unittest.TestSuite()
1590 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
1591 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
1592
1593 def test_addTests__string(self):
1594 suite = unittest.TestSuite()
1595 self.assertRaises(TypeError, suite.addTests, "foo")
Georg Brandl15c5ce92007-03-07 09:09:40 +00001596
1597
1598class Test_FunctionTestCase(TestCase):
1599
1600 # "Return the number of tests represented by the this test object. For
1601 # TestCase instances, this will always be 1"
1602 def test_countTestCases(self):
1603 test = unittest.FunctionTestCase(lambda: None)
1604
1605 self.assertEqual(test.countTestCases(), 1)
1606
1607 # "When a setUp() method is defined, the test runner will run that method
1608 # prior to each test. Likewise, if a tearDown() method is defined, the
1609 # test runner will invoke that method after each test. In the example,
1610 # setUp() was used to create a fresh sequence for each test."
1611 #
1612 # Make sure the proper call order is maintained, even if setUp() raises
1613 # an exception.
1614 def test_run_call_order__error_in_setUp(self):
1615 events = []
1616 result = LoggingResult(events)
1617
1618 def setUp():
1619 events.append('setUp')
1620 raise RuntimeError('raised by setUp')
1621
1622 def test():
1623 events.append('test')
1624
1625 def tearDown():
1626 events.append('tearDown')
1627
1628 expected = ['startTest', 'setUp', 'addError', '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 raises
1638 # an error (as opposed to a failure).
1639 def test_run_call_order__error_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 raise RuntimeError('raised by test')
1649
1650 def tearDown():
1651 events.append('tearDown')
1652
1653 expected = ['startTest', 'setUp', 'test', 'addError', '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 the test signals
1664 # a failure (as opposed to an error).
1665 def test_run_call_order__failure_in_test(self):
1666 events = []
1667 result = LoggingResult(events)
1668
1669 def setUp():
1670 events.append('setUp')
1671
1672 def test():
1673 events.append('test')
1674 self.fail('raised by test')
1675
1676 def tearDown():
1677 events.append('tearDown')
1678
1679 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
1680 'stopTest']
1681 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1682 self.assertEqual(events, expected)
1683
1684 # "When a setUp() method is defined, the test runner will run that method
1685 # prior to each test. Likewise, if a tearDown() method is defined, the
1686 # test runner will invoke that method after each test. In the example,
1687 # setUp() was used to create a fresh sequence for each test."
1688 #
1689 # Make sure the proper call order is maintained, even if tearDown() raises
1690 # an exception.
1691 def test_run_call_order__error_in_tearDown(self):
1692 events = []
1693 result = LoggingResult(events)
1694
1695 def setUp():
1696 events.append('setUp')
1697
1698 def test():
1699 events.append('test')
1700
1701 def tearDown():
1702 events.append('tearDown')
1703 raise RuntimeError('raised by tearDown')
1704
1705 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
1706 'stopTest']
1707 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1708 self.assertEqual(events, expected)
1709
1710 # "Return a string identifying the specific test case."
1711 #
1712 # Because of the vague nature of the docs, I'm not going to lock this
1713 # test down too much. Really all that can be asserted is that the id()
1714 # will be a string (either 8-byte or unicode -- again, because the docs
1715 # just say "string")
1716 def test_id(self):
1717 test = unittest.FunctionTestCase(lambda: None)
1718
1719 self.failUnless(isinstance(test.id(), basestring))
1720
1721 # "Returns a one-line description of the test, or None if no description
1722 # has been provided. The default implementation of this method returns
1723 # the first line of the test method's docstring, if available, or None."
1724 def test_shortDescription__no_docstring(self):
1725 test = unittest.FunctionTestCase(lambda: None)
1726
1727 self.assertEqual(test.shortDescription(), None)
1728
1729 # "Returns a one-line description of the test, or None if no description
1730 # has been provided. The default implementation of this method returns
1731 # the first line of the test method's docstring, if available, or None."
1732 def test_shortDescription__singleline_docstring(self):
1733 desc = "this tests foo"
1734 test = unittest.FunctionTestCase(lambda: None, description=desc)
1735
1736 self.assertEqual(test.shortDescription(), "this tests foo")
1737
1738class Test_TestResult(TestCase):
1739 # Note: there are not separate tests for TestResult.wasSuccessful(),
1740 # TestResult.errors, TestResult.failures, TestResult.testsRun or
1741 # TestResult.shouldStop because these only have meaning in terms of
1742 # other TestResult methods.
1743 #
1744 # Accordingly, tests for the aforenamed attributes are incorporated
1745 # in with the tests for the defining methods.
1746 ################################################################
1747
1748 def test_init(self):
1749 result = unittest.TestResult()
1750
1751 self.failUnless(result.wasSuccessful())
1752 self.assertEqual(len(result.errors), 0)
1753 self.assertEqual(len(result.failures), 0)
1754 self.assertEqual(result.testsRun, 0)
1755 self.assertEqual(result.shouldStop, False)
1756
1757 # "This method can be called to signal that the set of tests being
1758 # run should be aborted by setting the TestResult's shouldStop
1759 # attribute to True."
1760 def test_stop(self):
1761 result = unittest.TestResult()
1762
1763 result.stop()
1764
1765 self.assertEqual(result.shouldStop, True)
1766
1767 # "Called when the test case test is about to be run. The default
1768 # implementation simply increments the instance's testsRun counter."
1769 def test_startTest(self):
1770 class Foo(unittest.TestCase):
1771 def test_1(self):
1772 pass
1773
1774 test = Foo('test_1')
1775
1776 result = unittest.TestResult()
1777
1778 result.startTest(test)
1779
1780 self.failUnless(result.wasSuccessful())
1781 self.assertEqual(len(result.errors), 0)
1782 self.assertEqual(len(result.failures), 0)
1783 self.assertEqual(result.testsRun, 1)
1784 self.assertEqual(result.shouldStop, False)
1785
1786 result.stopTest(test)
1787
1788 # "Called after the test case test has been executed, regardless of
1789 # the outcome. The default implementation does nothing."
1790 def test_stopTest(self):
1791 class Foo(unittest.TestCase):
1792 def test_1(self):
1793 pass
1794
1795 test = Foo('test_1')
1796
1797 result = unittest.TestResult()
1798
1799 result.startTest(test)
1800
1801 self.failUnless(result.wasSuccessful())
1802 self.assertEqual(len(result.errors), 0)
1803 self.assertEqual(len(result.failures), 0)
1804 self.assertEqual(result.testsRun, 1)
1805 self.assertEqual(result.shouldStop, False)
1806
1807 result.stopTest(test)
1808
1809 # Same tests as above; make sure nothing has changed
1810 self.failUnless(result.wasSuccessful())
1811 self.assertEqual(len(result.errors), 0)
1812 self.assertEqual(len(result.failures), 0)
1813 self.assertEqual(result.testsRun, 1)
1814 self.assertEqual(result.shouldStop, False)
1815
1816 # "addSuccess(test)"
1817 # ...
1818 # "Called when the test case test succeeds"
1819 # ...
1820 # "wasSuccessful() - Returns True if all tests run so far have passed,
1821 # otherwise returns False"
1822 # ...
1823 # "testsRun - The total number of tests run so far."
1824 # ...
1825 # "errors - A list containing 2-tuples of TestCase instances and
1826 # formatted tracebacks. Each tuple represents a test which raised an
1827 # unexpected exception. Contains formatted
1828 # tracebacks instead of sys.exc_info() results."
1829 # ...
1830 # "failures - A list containing 2-tuples of TestCase instances and
1831 # formatted tracebacks. Each tuple represents a test where a failure was
1832 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1833 # methods. Contains formatted tracebacks instead
1834 # of sys.exc_info() results."
1835 def test_addSuccess(self):
1836 class Foo(unittest.TestCase):
1837 def test_1(self):
1838 pass
1839
1840 test = Foo('test_1')
1841
1842 result = unittest.TestResult()
1843
1844 result.startTest(test)
1845 result.addSuccess(test)
1846 result.stopTest(test)
1847
1848 self.failUnless(result.wasSuccessful())
1849 self.assertEqual(len(result.errors), 0)
1850 self.assertEqual(len(result.failures), 0)
1851 self.assertEqual(result.testsRun, 1)
1852 self.assertEqual(result.shouldStop, False)
1853
1854 # "addFailure(test, err)"
1855 # ...
1856 # "Called when the test case test signals a failure. err is a tuple of
1857 # the form returned by sys.exc_info(): (type, value, traceback)"
1858 # ...
1859 # "wasSuccessful() - Returns True if all tests run so far have passed,
1860 # otherwise returns False"
1861 # ...
1862 # "testsRun - The total number of tests run so far."
1863 # ...
1864 # "errors - A list containing 2-tuples of TestCase instances and
1865 # formatted tracebacks. Each tuple represents a test which raised an
1866 # unexpected exception. Contains formatted
1867 # tracebacks instead of sys.exc_info() results."
1868 # ...
1869 # "failures - A list containing 2-tuples of TestCase instances and
1870 # formatted tracebacks. Each tuple represents a test where a failure was
1871 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1872 # methods. Contains formatted tracebacks instead
1873 # of sys.exc_info() results."
1874 def test_addFailure(self):
1875 import sys
1876
1877 class Foo(unittest.TestCase):
1878 def test_1(self):
1879 pass
1880
1881 test = Foo('test_1')
1882 try:
1883 test.fail("foo")
1884 except:
1885 exc_info_tuple = sys.exc_info()
1886
1887 result = unittest.TestResult()
1888
1889 result.startTest(test)
1890 result.addFailure(test, exc_info_tuple)
1891 result.stopTest(test)
1892
1893 self.failIf(result.wasSuccessful())
1894 self.assertEqual(len(result.errors), 0)
1895 self.assertEqual(len(result.failures), 1)
1896 self.assertEqual(result.testsRun, 1)
1897 self.assertEqual(result.shouldStop, False)
1898
1899 test_case, formatted_exc = result.failures[0]
1900 self.failUnless(test_case is test)
1901 self.failUnless(isinstance(formatted_exc, str))
1902
1903 # "addError(test, err)"
1904 # ...
1905 # "Called when the test case test raises an unexpected exception err
1906 # is a tuple of the form returned by sys.exc_info():
1907 # (type, value, traceback)"
1908 # ...
1909 # "wasSuccessful() - Returns True if all tests run so far have passed,
1910 # otherwise returns False"
1911 # ...
1912 # "testsRun - The total number of tests run so far."
1913 # ...
1914 # "errors - A list containing 2-tuples of TestCase instances and
1915 # formatted tracebacks. Each tuple represents a test which raised an
1916 # unexpected exception. Contains formatted
1917 # tracebacks instead of sys.exc_info() results."
1918 # ...
1919 # "failures - A list containing 2-tuples of TestCase instances and
1920 # formatted tracebacks. Each tuple represents a test where a failure was
1921 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1922 # methods. Contains formatted tracebacks instead
1923 # of sys.exc_info() results."
1924 def test_addError(self):
1925 import sys
1926
1927 class Foo(unittest.TestCase):
1928 def test_1(self):
1929 pass
1930
1931 test = Foo('test_1')
1932 try:
1933 raise TypeError()
1934 except:
1935 exc_info_tuple = sys.exc_info()
1936
1937 result = unittest.TestResult()
1938
1939 result.startTest(test)
1940 result.addError(test, exc_info_tuple)
1941 result.stopTest(test)
1942
1943 self.failIf(result.wasSuccessful())
1944 self.assertEqual(len(result.errors), 1)
1945 self.assertEqual(len(result.failures), 0)
1946 self.assertEqual(result.testsRun, 1)
1947 self.assertEqual(result.shouldStop, False)
1948
1949 test_case, formatted_exc = result.errors[0]
1950 self.failUnless(test_case is test)
1951 self.failUnless(isinstance(formatted_exc, str))
1952
1953### Support code for Test_TestCase
1954################################################################
1955
1956class Foo(unittest.TestCase):
1957 def runTest(self): pass
1958 def test1(self): pass
1959
1960class Bar(Foo):
1961 def test2(self): pass
1962
1963################################################################
1964### /Support code for Test_TestCase
1965
1966class Test_TestCase(TestCase, TestEquality, TestHashing):
1967
1968 ### Set up attributes used by inherited tests
1969 ################################################################
1970
1971 # Used by TestHashing.test_hash and TestEquality.test_eq
1972 eq_pairs = [(Foo('test1'), Foo('test1'))]
1973
1974 # Used by TestEquality.test_ne
1975 ne_pairs = [(Foo('test1'), Foo('runTest'))
1976 ,(Foo('test1'), Bar('test1'))
1977 ,(Foo('test1'), Bar('test2'))]
1978
1979 ################################################################
1980 ### /Set up attributes used by inherited tests
1981
1982
1983 # "class TestCase([methodName])"
1984 # ...
1985 # "Each instance of TestCase will run a single test method: the
1986 # method named methodName."
1987 # ...
1988 # "methodName defaults to "runTest"."
1989 #
1990 # Make sure it really is optional, and that it defaults to the proper
1991 # thing.
1992 def test_init__no_test_name(self):
1993 class Test(unittest.TestCase):
1994 def runTest(self): raise MyException()
1995 def test(self): pass
1996
1997 self.assertEqual(Test().id()[-13:], '.Test.runTest')
1998
1999 # "class TestCase([methodName])"
2000 # ...
2001 # "Each instance of TestCase will run a single test method: the
2002 # method named methodName."
2003 def test_init__test_name__valid(self):
2004 class Test(unittest.TestCase):
2005 def runTest(self): raise MyException()
2006 def test(self): pass
2007
2008 self.assertEqual(Test('test').id()[-10:], '.Test.test')
2009
2010 # "class TestCase([methodName])"
2011 # ...
2012 # "Each instance of TestCase will run a single test method: the
2013 # method named methodName."
2014 def test_init__test_name__invalid(self):
2015 class Test(unittest.TestCase):
2016 def runTest(self): raise MyException()
2017 def test(self): pass
2018
2019 try:
2020 Test('testfoo')
2021 except ValueError:
2022 pass
2023 else:
2024 self.fail("Failed to raise ValueError")
2025
2026 # "Return the number of tests represented by the this test object. For
2027 # TestCase instances, this will always be 1"
2028 def test_countTestCases(self):
2029 class Foo(unittest.TestCase):
2030 def test(self): pass
2031
2032 self.assertEqual(Foo('test').countTestCases(), 1)
2033
2034 # "Return the default type of test result object to be used to run this
2035 # test. For TestCase instances, this will always be
2036 # unittest.TestResult; subclasses of TestCase should
2037 # override this as necessary."
2038 def test_defaultTestResult(self):
2039 class Foo(unittest.TestCase):
2040 def runTest(self):
2041 pass
2042
2043 result = Foo().defaultTestResult()
2044 self.assertEqual(type(result), unittest.TestResult)
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 setUp() raises
2052 # an exception.
2053 def test_run_call_order__error_in_setUp(self):
2054 events = []
2055 result = LoggingResult(events)
2056
2057 class Foo(unittest.TestCase):
2058 def setUp(self):
2059 events.append('setUp')
2060 raise RuntimeError('raised by Foo.setUp')
2061
2062 def test(self):
2063 events.append('test')
2064
2065 def tearDown(self):
2066 events.append('tearDown')
2067
2068 Foo('test').run(result)
2069 expected = ['startTest', 'setUp', 'addError', 'stopTest']
2070 self.assertEqual(events, expected)
2071
2072 # "When a setUp() method is defined, the test runner will run that method
2073 # prior to each test. Likewise, if a tearDown() method is defined, the
2074 # test runner will invoke that method after each test. In the example,
2075 # setUp() was used to create a fresh sequence for each test."
2076 #
2077 # Make sure the proper call order is maintained, even if the test raises
2078 # an error (as opposed to a failure).
2079 def test_run_call_order__error_in_test(self):
2080 events = []
2081 result = LoggingResult(events)
2082
2083 class Foo(unittest.TestCase):
2084 def setUp(self):
2085 events.append('setUp')
2086
2087 def test(self):
2088 events.append('test')
2089 raise RuntimeError('raised by Foo.test')
2090
2091 def tearDown(self):
2092 events.append('tearDown')
2093
2094 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
2095 'stopTest']
2096 Foo('test').run(result)
2097 self.assertEqual(events, expected)
2098
2099 # "When a setUp() method is defined, the test runner will run that method
2100 # prior to each test. Likewise, if a tearDown() method is defined, the
2101 # test runner will invoke that method after each test. In the example,
2102 # setUp() was used to create a fresh sequence for each test."
2103 #
2104 # Make sure the proper call order is maintained, even if the test signals
2105 # a failure (as opposed to an error).
2106 def test_run_call_order__failure_in_test(self):
2107 events = []
2108 result = LoggingResult(events)
2109
2110 class Foo(unittest.TestCase):
2111 def setUp(self):
2112 events.append('setUp')
2113
2114 def test(self):
2115 events.append('test')
2116 self.fail('raised by Foo.test')
2117
2118 def tearDown(self):
2119 events.append('tearDown')
2120
2121 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
2122 'stopTest']
2123 Foo('test').run(result)
2124 self.assertEqual(events, expected)
2125
2126 # "When a setUp() method is defined, the test runner will run that method
2127 # prior to each test. Likewise, if a tearDown() method is defined, the
2128 # test runner will invoke that method after each test. In the example,
2129 # setUp() was used to create a fresh sequence for each test."
2130 #
2131 # Make sure the proper call order is maintained, even if tearDown() raises
2132 # an exception.
2133 def test_run_call_order__error_in_tearDown(self):
2134 events = []
2135 result = LoggingResult(events)
2136
2137 class Foo(unittest.TestCase):
2138 def setUp(self):
2139 events.append('setUp')
2140
2141 def test(self):
2142 events.append('test')
2143
2144 def tearDown(self):
2145 events.append('tearDown')
2146 raise RuntimeError('raised by Foo.tearDown')
2147
2148 Foo('test').run(result)
2149 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
2150 'stopTest']
2151 self.assertEqual(events, expected)
2152
2153 # "This class attribute gives the exception raised by the test() method.
2154 # If a test framework needs to use a specialized exception, possibly to
2155 # carry additional information, it must subclass this exception in
2156 # order to ``play fair'' with the framework. The initial value of this
2157 # attribute is AssertionError"
2158 def test_failureException__default(self):
2159 class Foo(unittest.TestCase):
2160 def test(self):
2161 pass
2162
2163 self.failUnless(Foo('test').failureException is AssertionError)
2164
2165 # "This class attribute gives the exception raised by the test() method.
2166 # If a test framework needs to use a specialized exception, possibly to
2167 # carry additional information, it must subclass this exception in
2168 # order to ``play fair'' with the framework."
2169 #
2170 # Make sure TestCase.run() respects the designated failureException
2171 def test_failureException__subclassing__explicit_raise(self):
2172 events = []
2173 result = LoggingResult(events)
2174
2175 class Foo(unittest.TestCase):
2176 def test(self):
2177 raise RuntimeError()
2178
2179 failureException = RuntimeError
2180
2181 self.failUnless(Foo('test').failureException is RuntimeError)
2182
2183
2184 Foo('test').run(result)
2185 expected = ['startTest', 'addFailure', 'stopTest']
2186 self.assertEqual(events, expected)
2187
2188 # "This class attribute gives the exception raised by the test() method.
2189 # If a test framework needs to use a specialized exception, possibly to
2190 # carry additional information, it must subclass this exception in
2191 # order to ``play fair'' with the framework."
2192 #
2193 # Make sure TestCase.run() respects the designated failureException
2194 def test_failureException__subclassing__implicit_raise(self):
2195 events = []
2196 result = LoggingResult(events)
2197
2198 class Foo(unittest.TestCase):
2199 def test(self):
2200 self.fail("foo")
2201
2202 failureException = RuntimeError
2203
2204 self.failUnless(Foo('test').failureException is RuntimeError)
2205
2206
2207 Foo('test').run(result)
2208 expected = ['startTest', 'addFailure', 'stopTest']
2209 self.assertEqual(events, expected)
2210
2211 # "The default implementation does nothing."
2212 def test_setUp(self):
2213 class Foo(unittest.TestCase):
2214 def runTest(self):
2215 pass
2216
2217 # ... and nothing should happen
2218 Foo().setUp()
2219
2220 # "The default implementation does nothing."
2221 def test_tearDown(self):
2222 class Foo(unittest.TestCase):
2223 def runTest(self):
2224 pass
2225
2226 # ... and nothing should happen
2227 Foo().tearDown()
2228
2229 # "Return a string identifying the specific test case."
2230 #
2231 # Because of the vague nature of the docs, I'm not going to lock this
2232 # test down too much. Really all that can be asserted is that the id()
2233 # will be a string (either 8-byte or unicode -- again, because the docs
2234 # just say "string")
2235 def test_id(self):
2236 class Foo(unittest.TestCase):
2237 def runTest(self):
2238 pass
2239
2240 self.failUnless(isinstance(Foo().id(), basestring))
2241
2242 # "Returns a one-line description of the test, or None if no description
2243 # has been provided. The default implementation of this method returns
2244 # the first line of the test method's docstring, if available, or None."
2245 def test_shortDescription__no_docstring(self):
2246 class Foo(unittest.TestCase):
2247 def runTest(self):
2248 pass
2249
2250 self.assertEqual(Foo().shortDescription(), None)
2251
2252 # "Returns a one-line description of the test, or None if no description
2253 # has been provided. The default implementation of this method returns
2254 # the first line of the test method's docstring, if available, or None."
2255 def test_shortDescription__singleline_docstring(self):
2256 class Foo(unittest.TestCase):
2257 def runTest(self):
2258 "this tests foo"
2259 pass
2260
2261 self.assertEqual(Foo().shortDescription(), "this tests foo")
2262
2263 # "Returns a one-line description of the test, or None if no description
2264 # has been provided. The default implementation of this method returns
2265 # the first line of the test method's docstring, if available, or None."
2266 def test_shortDescription__multiline_docstring(self):
2267 class Foo(unittest.TestCase):
2268 def runTest(self):
2269 """this tests foo
2270 blah, bar and baz are also tested"""
2271 pass
2272
2273 self.assertEqual(Foo().shortDescription(), "this tests foo")
2274
2275 # "If result is omitted or None, a temporary result object is created
2276 # and used, but is not made available to the caller"
2277 def test_run__uses_defaultTestResult(self):
2278 events = []
2279
2280 class Foo(unittest.TestCase):
2281 def test(self):
2282 events.append('test')
2283
2284 def defaultTestResult(self):
2285 return LoggingResult(events)
2286
2287 # Make run() find a result object on its own
2288 Foo('test').run()
2289
2290 expected = ['startTest', 'test', 'stopTest']
2291 self.assertEqual(events, expected)
Jim Fultonfafd8742004-08-28 15:22:12 +00002292
2293######################################################################
2294## Main
2295######################################################################
2296
2297def test_main():
Georg Brandl15c5ce92007-03-07 09:09:40 +00002298 test_support.run_unittest(Test_TestCase, Test_TestLoader,
2299 Test_TestSuite, Test_TestResult, Test_FunctionTestCase)
Jim Fultonfafd8742004-08-28 15:22:12 +00002300
Georg Brandl15c5ce92007-03-07 09:09:40 +00002301if __name__ == "__main__":
2302 test_main()