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