]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/unittest/test/test_loader.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / unittest / test / test_loader.py
CommitLineData
4710c53d 1import sys\r
2import types\r
3\r
4\r
5import unittest\r
6\r
7\r
8class Test_TestLoader(unittest.TestCase):\r
9\r
10 ### Tests for TestLoader.loadTestsFromTestCase\r
11 ################################################################\r
12\r
13 # "Return a suite of all tests cases contained in the TestCase-derived\r
14 # class testCaseClass"\r
15 def test_loadTestsFromTestCase(self):\r
16 class Foo(unittest.TestCase):\r
17 def test_1(self): pass\r
18 def test_2(self): pass\r
19 def foo_bar(self): pass\r
20\r
21 tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])\r
22\r
23 loader = unittest.TestLoader()\r
24 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)\r
25\r
26 # "Return a suite of all tests cases contained in the TestCase-derived\r
27 # class testCaseClass"\r
28 #\r
29 # Make sure it does the right thing even if no tests were found\r
30 def test_loadTestsFromTestCase__no_matches(self):\r
31 class Foo(unittest.TestCase):\r
32 def foo_bar(self): pass\r
33\r
34 empty_suite = unittest.TestSuite()\r
35\r
36 loader = unittest.TestLoader()\r
37 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)\r
38\r
39 # "Return a suite of all tests cases contained in the TestCase-derived\r
40 # class testCaseClass"\r
41 #\r
42 # What happens if loadTestsFromTestCase() is given an object\r
43 # that isn't a subclass of TestCase? Specifically, what happens\r
44 # if testCaseClass is a subclass of TestSuite?\r
45 #\r
46 # This is checked for specifically in the code, so we better add a\r
47 # test for it.\r
48 def test_loadTestsFromTestCase__TestSuite_subclass(self):\r
49 class NotATestCase(unittest.TestSuite):\r
50 pass\r
51\r
52 loader = unittest.TestLoader()\r
53 try:\r
54 loader.loadTestsFromTestCase(NotATestCase)\r
55 except TypeError:\r
56 pass\r
57 else:\r
58 self.fail('Should raise TypeError')\r
59\r
60 # "Return a suite of all tests cases contained in the TestCase-derived\r
61 # class testCaseClass"\r
62 #\r
63 # Make sure loadTestsFromTestCase() picks up the default test method\r
64 # name (as specified by TestCase), even though the method name does\r
65 # not match the default TestLoader.testMethodPrefix string\r
66 def test_loadTestsFromTestCase__default_method_name(self):\r
67 class Foo(unittest.TestCase):\r
68 def runTest(self):\r
69 pass\r
70\r
71 loader = unittest.TestLoader()\r
72 # This has to be false for the test to succeed\r
73 self.assertFalse('runTest'.startswith(loader.testMethodPrefix))\r
74\r
75 suite = loader.loadTestsFromTestCase(Foo)\r
76 self.assertIsInstance(suite, loader.suiteClass)\r
77 self.assertEqual(list(suite), [Foo('runTest')])\r
78\r
79 ################################################################\r
80 ### /Tests for TestLoader.loadTestsFromTestCase\r
81\r
82 ### Tests for TestLoader.loadTestsFromModule\r
83 ################################################################\r
84\r
85 # "This method searches `module` for classes derived from TestCase"\r
86 def test_loadTestsFromModule__TestCase_subclass(self):\r
87 m = types.ModuleType('m')\r
88 class MyTestCase(unittest.TestCase):\r
89 def test(self):\r
90 pass\r
91 m.testcase_1 = MyTestCase\r
92\r
93 loader = unittest.TestLoader()\r
94 suite = loader.loadTestsFromModule(m)\r
95 self.assertIsInstance(suite, loader.suiteClass)\r
96\r
97 expected = [loader.suiteClass([MyTestCase('test')])]\r
98 self.assertEqual(list(suite), expected)\r
99\r
100 # "This method searches `module` for classes derived from TestCase"\r
101 #\r
102 # What happens if no tests are found (no TestCase instances)?\r
103 def test_loadTestsFromModule__no_TestCase_instances(self):\r
104 m = types.ModuleType('m')\r
105\r
106 loader = unittest.TestLoader()\r
107 suite = loader.loadTestsFromModule(m)\r
108 self.assertIsInstance(suite, loader.suiteClass)\r
109 self.assertEqual(list(suite), [])\r
110\r
111 # "This method searches `module` for classes derived from TestCase"\r
112 #\r
113 # What happens if no tests are found (TestCases instances, but no tests)?\r
114 def test_loadTestsFromModule__no_TestCase_tests(self):\r
115 m = types.ModuleType('m')\r
116 class MyTestCase(unittest.TestCase):\r
117 pass\r
118 m.testcase_1 = MyTestCase\r
119\r
120 loader = unittest.TestLoader()\r
121 suite = loader.loadTestsFromModule(m)\r
122 self.assertIsInstance(suite, loader.suiteClass)\r
123\r
124 self.assertEqual(list(suite), [loader.suiteClass()])\r
125\r
126 # "This method searches `module` for classes derived from TestCase"s\r
127 #\r
128 # What happens if loadTestsFromModule() is given something other\r
129 # than a module?\r
130 #\r
131 # XXX Currently, it succeeds anyway. This flexibility\r
132 # should either be documented or loadTestsFromModule() should\r
133 # raise a TypeError\r
134 #\r
135 # XXX Certain people are using this behaviour. We'll add a test for it\r
136 def test_loadTestsFromModule__not_a_module(self):\r
137 class MyTestCase(unittest.TestCase):\r
138 def test(self):\r
139 pass\r
140\r
141 class NotAModule(object):\r
142 test_2 = MyTestCase\r
143\r
144 loader = unittest.TestLoader()\r
145 suite = loader.loadTestsFromModule(NotAModule)\r
146\r
147 reference = [unittest.TestSuite([MyTestCase('test')])]\r
148 self.assertEqual(list(suite), reference)\r
149\r
150\r
151 # Check that loadTestsFromModule honors (or not) a module\r
152 # with a load_tests function.\r
153 def test_loadTestsFromModule__load_tests(self):\r
154 m = types.ModuleType('m')\r
155 class MyTestCase(unittest.TestCase):\r
156 def test(self):\r
157 pass\r
158 m.testcase_1 = MyTestCase\r
159\r
160 load_tests_args = []\r
161 def load_tests(loader, tests, pattern):\r
162 self.assertIsInstance(tests, unittest.TestSuite)\r
163 load_tests_args.extend((loader, tests, pattern))\r
164 return tests\r
165 m.load_tests = load_tests\r
166\r
167 loader = unittest.TestLoader()\r
168 suite = loader.loadTestsFromModule(m)\r
169 self.assertIsInstance(suite, unittest.TestSuite)\r
170 self.assertEqual(load_tests_args, [loader, suite, None])\r
171\r
172 load_tests_args = []\r
173 suite = loader.loadTestsFromModule(m, use_load_tests=False)\r
174 self.assertEqual(load_tests_args, [])\r
175\r
176 def test_loadTestsFromModule__faulty_load_tests(self):\r
177 m = types.ModuleType('m')\r
178\r
179 def load_tests(loader, tests, pattern):\r
180 raise TypeError('some failure')\r
181 m.load_tests = load_tests\r
182\r
183 loader = unittest.TestLoader()\r
184 suite = loader.loadTestsFromModule(m)\r
185 self.assertIsInstance(suite, unittest.TestSuite)\r
186 self.assertEqual(suite.countTestCases(), 1)\r
187 test = list(suite)[0]\r
188\r
189 self.assertRaisesRegexp(TypeError, "some failure", test.m)\r
190\r
191 ################################################################\r
192 ### /Tests for TestLoader.loadTestsFromModule()\r
193\r
194 ### Tests for TestLoader.loadTestsFromName()\r
195 ################################################################\r
196\r
197 # "The specifier name is a ``dotted name'' that may resolve either to\r
198 # a module, a test case class, a TestSuite instance, a test method\r
199 # within a test case class, or a callable object which returns a\r
200 # TestCase or TestSuite instance."\r
201 #\r
202 # Is ValueError raised in response to an empty name?\r
203 def test_loadTestsFromName__empty_name(self):\r
204 loader = unittest.TestLoader()\r
205\r
206 try:\r
207 loader.loadTestsFromName('')\r
208 except ValueError, e:\r
209 self.assertEqual(str(e), "Empty module name")\r
210 else:\r
211 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")\r
212\r
213 # "The specifier name is a ``dotted name'' that may resolve either to\r
214 # a module, a test case class, a TestSuite instance, a test method\r
215 # within a test case class, or a callable object which returns a\r
216 # TestCase or TestSuite instance."\r
217 #\r
218 # What happens when the name contains invalid characters?\r
219 def test_loadTestsFromName__malformed_name(self):\r
220 loader = unittest.TestLoader()\r
221\r
222 # XXX Should this raise ValueError or ImportError?\r
223 try:\r
224 loader.loadTestsFromName('abc () //')\r
225 except ValueError:\r
226 pass\r
227 except ImportError:\r
228 pass\r
229 else:\r
230 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")\r
231\r
232 # "The specifier name is a ``dotted name'' that may resolve ... to a\r
233 # module"\r
234 #\r
235 # What happens when a module by that name can't be found?\r
236 def test_loadTestsFromName__unknown_module_name(self):\r
237 loader = unittest.TestLoader()\r
238\r
239 try:\r
240 loader.loadTestsFromName('sdasfasfasdf')\r
241 except ImportError, e:\r
242 self.assertEqual(str(e), "No module named sdasfasfasdf")\r
243 else:\r
244 self.fail("TestLoader.loadTestsFromName failed to raise ImportError")\r
245\r
246 # "The specifier name is a ``dotted name'' that may resolve either to\r
247 # a module, a test case class, a TestSuite instance, a test method\r
248 # within a test case class, or a callable object which returns a\r
249 # TestCase or TestSuite instance."\r
250 #\r
251 # What happens when the module is found, but the attribute can't?\r
252 def test_loadTestsFromName__unknown_attr_name(self):\r
253 loader = unittest.TestLoader()\r
254\r
255 try:\r
256 loader.loadTestsFromName('unittest.sdasfasfasdf')\r
257 except AttributeError, e:\r
258 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")\r
259 else:\r
260 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")\r
261\r
262 # "The specifier name is a ``dotted name'' that may resolve either to\r
263 # a module, a test case class, a TestSuite instance, a test method\r
264 # within a test case class, or a callable object which returns a\r
265 # TestCase or TestSuite instance."\r
266 #\r
267 # What happens when we provide the module, but the attribute can't be\r
268 # found?\r
269 def test_loadTestsFromName__relative_unknown_name(self):\r
270 loader = unittest.TestLoader()\r
271\r
272 try:\r
273 loader.loadTestsFromName('sdasfasfasdf', unittest)\r
274 except AttributeError, e:\r
275 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")\r
276 else:\r
277 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")\r
278\r
279 # "The specifier name is a ``dotted name'' that may resolve either to\r
280 # a module, a test case class, a TestSuite instance, a test method\r
281 # within a test case class, or a callable object which returns a\r
282 # TestCase or TestSuite instance."\r
283 # ...\r
284 # "The method optionally resolves name relative to the given module"\r
285 #\r
286 # Does loadTestsFromName raise ValueError when passed an empty\r
287 # name relative to a provided module?\r
288 #\r
289 # XXX Should probably raise a ValueError instead of an AttributeError\r
290 def test_loadTestsFromName__relative_empty_name(self):\r
291 loader = unittest.TestLoader()\r
292\r
293 try:\r
294 loader.loadTestsFromName('', unittest)\r
295 except AttributeError:\r
296 pass\r
297 else:\r
298 self.fail("Failed to raise AttributeError")\r
299\r
300 # "The specifier name is a ``dotted name'' that may resolve either to\r
301 # a module, a test case class, a TestSuite instance, a test method\r
302 # within a test case class, or a callable object which returns a\r
303 # TestCase or TestSuite instance."\r
304 # ...\r
305 # "The method optionally resolves name relative to the given module"\r
306 #\r
307 # What happens when an impossible name is given, relative to the provided\r
308 # `module`?\r
309 def test_loadTestsFromName__relative_malformed_name(self):\r
310 loader = unittest.TestLoader()\r
311\r
312 # XXX Should this raise AttributeError or ValueError?\r
313 try:\r
314 loader.loadTestsFromName('abc () //', unittest)\r
315 except ValueError:\r
316 pass\r
317 except AttributeError:\r
318 pass\r
319 else:\r
320 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")\r
321\r
322 # "The method optionally resolves name relative to the given module"\r
323 #\r
324 # Does loadTestsFromName raise TypeError when the `module` argument\r
325 # isn't a module object?\r
326 #\r
327 # XXX Accepts the not-a-module object, ignorning the object's type\r
328 # This should raise an exception or the method name should be changed\r
329 #\r
330 # XXX Some people are relying on this, so keep it for now\r
331 def test_loadTestsFromName__relative_not_a_module(self):\r
332 class MyTestCase(unittest.TestCase):\r
333 def test(self):\r
334 pass\r
335\r
336 class NotAModule(object):\r
337 test_2 = MyTestCase\r
338\r
339 loader = unittest.TestLoader()\r
340 suite = loader.loadTestsFromName('test_2', NotAModule)\r
341\r
342 reference = [MyTestCase('test')]\r
343 self.assertEqual(list(suite), reference)\r
344\r
345 # "The specifier name is a ``dotted name'' that may resolve either to\r
346 # a module, a test case class, a TestSuite instance, a test method\r
347 # within a test case class, or a callable object which returns a\r
348 # TestCase or TestSuite instance."\r
349 #\r
350 # Does it raise an exception if the name resolves to an invalid\r
351 # object?\r
352 def test_loadTestsFromName__relative_bad_object(self):\r
353 m = types.ModuleType('m')\r
354 m.testcase_1 = object()\r
355\r
356 loader = unittest.TestLoader()\r
357 try:\r
358 loader.loadTestsFromName('testcase_1', m)\r
359 except TypeError:\r
360 pass\r
361 else:\r
362 self.fail("Should have raised TypeError")\r
363\r
364 # "The specifier name is a ``dotted name'' that may\r
365 # resolve either to ... a test case class"\r
366 def test_loadTestsFromName__relative_TestCase_subclass(self):\r
367 m = types.ModuleType('m')\r
368 class MyTestCase(unittest.TestCase):\r
369 def test(self):\r
370 pass\r
371 m.testcase_1 = MyTestCase\r
372\r
373 loader = unittest.TestLoader()\r
374 suite = loader.loadTestsFromName('testcase_1', m)\r
375 self.assertIsInstance(suite, loader.suiteClass)\r
376 self.assertEqual(list(suite), [MyTestCase('test')])\r
377\r
378 # "The specifier name is a ``dotted name'' that may resolve either to\r
379 # a module, a test case class, a TestSuite instance, a test method\r
380 # within a test case class, or a callable object which returns a\r
381 # TestCase or TestSuite instance."\r
382 def test_loadTestsFromName__relative_TestSuite(self):\r
383 m = types.ModuleType('m')\r
384 class MyTestCase(unittest.TestCase):\r
385 def test(self):\r
386 pass\r
387 m.testsuite = unittest.TestSuite([MyTestCase('test')])\r
388\r
389 loader = unittest.TestLoader()\r
390 suite = loader.loadTestsFromName('testsuite', m)\r
391 self.assertIsInstance(suite, loader.suiteClass)\r
392\r
393 self.assertEqual(list(suite), [MyTestCase('test')])\r
394\r
395 # "The specifier name is a ``dotted name'' that may resolve ... to\r
396 # ... a test method within a test case class"\r
397 def test_loadTestsFromName__relative_testmethod(self):\r
398 m = types.ModuleType('m')\r
399 class MyTestCase(unittest.TestCase):\r
400 def test(self):\r
401 pass\r
402 m.testcase_1 = MyTestCase\r
403\r
404 loader = unittest.TestLoader()\r
405 suite = loader.loadTestsFromName('testcase_1.test', m)\r
406 self.assertIsInstance(suite, loader.suiteClass)\r
407\r
408 self.assertEqual(list(suite), [MyTestCase('test')])\r
409\r
410 # "The specifier name is a ``dotted name'' that may resolve either to\r
411 # a module, a test case class, a TestSuite instance, a test method\r
412 # within a test case class, or a callable object which returns a\r
413 # TestCase or TestSuite instance."\r
414 #\r
415 # Does loadTestsFromName() raise the proper exception when trying to\r
416 # resolve "a test method within a test case class" that doesn't exist\r
417 # for the given name (relative to a provided module)?\r
418 def test_loadTestsFromName__relative_invalid_testmethod(self):\r
419 m = types.ModuleType('m')\r
420 class MyTestCase(unittest.TestCase):\r
421 def test(self):\r
422 pass\r
423 m.testcase_1 = MyTestCase\r
424\r
425 loader = unittest.TestLoader()\r
426 try:\r
427 loader.loadTestsFromName('testcase_1.testfoo', m)\r
428 except AttributeError, e:\r
429 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")\r
430 else:\r
431 self.fail("Failed to raise AttributeError")\r
432\r
433 # "The specifier name is a ``dotted name'' that may resolve ... to\r
434 # ... a callable object which returns a ... TestSuite instance"\r
435 def test_loadTestsFromName__callable__TestSuite(self):\r
436 m = types.ModuleType('m')\r
437 testcase_1 = unittest.FunctionTestCase(lambda: None)\r
438 testcase_2 = unittest.FunctionTestCase(lambda: None)\r
439 def return_TestSuite():\r
440 return unittest.TestSuite([testcase_1, testcase_2])\r
441 m.return_TestSuite = return_TestSuite\r
442\r
443 loader = unittest.TestLoader()\r
444 suite = loader.loadTestsFromName('return_TestSuite', m)\r
445 self.assertIsInstance(suite, loader.suiteClass)\r
446 self.assertEqual(list(suite), [testcase_1, testcase_2])\r
447\r
448 # "The specifier name is a ``dotted name'' that may resolve ... to\r
449 # ... a callable object which returns a TestCase ... instance"\r
450 def test_loadTestsFromName__callable__TestCase_instance(self):\r
451 m = types.ModuleType('m')\r
452 testcase_1 = unittest.FunctionTestCase(lambda: None)\r
453 def return_TestCase():\r
454 return testcase_1\r
455 m.return_TestCase = return_TestCase\r
456\r
457 loader = unittest.TestLoader()\r
458 suite = loader.loadTestsFromName('return_TestCase', m)\r
459 self.assertIsInstance(suite, loader.suiteClass)\r
460 self.assertEqual(list(suite), [testcase_1])\r
461\r
462 # "The specifier name is a ``dotted name'' that may resolve ... to\r
463 # ... a callable object which returns a TestCase ... instance"\r
464 #*****************************************************************\r
465 #Override the suiteClass attribute to ensure that the suiteClass\r
466 #attribute is used\r
467 def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):\r
468 class SubTestSuite(unittest.TestSuite):\r
469 pass\r
470 m = types.ModuleType('m')\r
471 testcase_1 = unittest.FunctionTestCase(lambda: None)\r
472 def return_TestCase():\r
473 return testcase_1\r
474 m.return_TestCase = return_TestCase\r
475\r
476 loader = unittest.TestLoader()\r
477 loader.suiteClass = SubTestSuite\r
478 suite = loader.loadTestsFromName('return_TestCase', m)\r
479 self.assertIsInstance(suite, loader.suiteClass)\r
480 self.assertEqual(list(suite), [testcase_1])\r
481\r
482 # "The specifier name is a ``dotted name'' that may resolve ... to\r
483 # ... a test method within a test case class"\r
484 #*****************************************************************\r
485 #Override the suiteClass attribute to ensure that the suiteClass\r
486 #attribute is used\r
487 def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):\r
488 class SubTestSuite(unittest.TestSuite):\r
489 pass\r
490 m = types.ModuleType('m')\r
491 class MyTestCase(unittest.TestCase):\r
492 def test(self):\r
493 pass\r
494 m.testcase_1 = MyTestCase\r
495\r
496 loader = unittest.TestLoader()\r
497 loader.suiteClass=SubTestSuite\r
498 suite = loader.loadTestsFromName('testcase_1.test', m)\r
499 self.assertIsInstance(suite, loader.suiteClass)\r
500\r
501 self.assertEqual(list(suite), [MyTestCase('test')])\r
502\r
503 # "The specifier name is a ``dotted name'' that may resolve ... to\r
504 # ... a callable object which returns a TestCase or TestSuite instance"\r
505 #\r
506 # What happens if the callable returns something else?\r
507 def test_loadTestsFromName__callable__wrong_type(self):\r
508 m = types.ModuleType('m')\r
509 def return_wrong():\r
510 return 6\r
511 m.return_wrong = return_wrong\r
512\r
513 loader = unittest.TestLoader()\r
514 try:\r
515 loader.loadTestsFromName('return_wrong', m)\r
516 except TypeError:\r
517 pass\r
518 else:\r
519 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")\r
520\r
521 # "The specifier can refer to modules and packages which have not been\r
522 # imported; they will be imported as a side-effect"\r
523 def test_loadTestsFromName__module_not_loaded(self):\r
524 # We're going to try to load this module as a side-effect, so it\r
525 # better not be loaded before we try.\r
526 #\r
527 module_name = 'unittest.test.dummy'\r
528 sys.modules.pop(module_name, None)\r
529\r
530 loader = unittest.TestLoader()\r
531 try:\r
532 suite = loader.loadTestsFromName(module_name)\r
533\r
534 self.assertIsInstance(suite, loader.suiteClass)\r
535 self.assertEqual(list(suite), [])\r
536\r
537 # module should now be loaded, thanks to loadTestsFromName()\r
538 self.assertIn(module_name, sys.modules)\r
539 finally:\r
540 if module_name in sys.modules:\r
541 del sys.modules[module_name]\r
542\r
543 ################################################################\r
544 ### Tests for TestLoader.loadTestsFromName()\r
545\r
546 ### Tests for TestLoader.loadTestsFromNames()\r
547 ################################################################\r
548\r
549 # "Similar to loadTestsFromName(), but takes a sequence of names rather\r
550 # than a single name."\r
551 #\r
552 # What happens if that sequence of names is empty?\r
553 def test_loadTestsFromNames__empty_name_list(self):\r
554 loader = unittest.TestLoader()\r
555\r
556 suite = loader.loadTestsFromNames([])\r
557 self.assertIsInstance(suite, loader.suiteClass)\r
558 self.assertEqual(list(suite), [])\r
559\r
560 # "Similar to loadTestsFromName(), but takes a sequence of names rather\r
561 # than a single name."\r
562 # ...\r
563 # "The method optionally resolves name relative to the given module"\r
564 #\r
565 # What happens if that sequence of names is empty?\r
566 #\r
567 # XXX Should this raise a ValueError or just return an empty TestSuite?\r
568 def test_loadTestsFromNames__relative_empty_name_list(self):\r
569 loader = unittest.TestLoader()\r
570\r
571 suite = loader.loadTestsFromNames([], unittest)\r
572 self.assertIsInstance(suite, loader.suiteClass)\r
573 self.assertEqual(list(suite), [])\r
574\r
575 # "The specifier name is a ``dotted name'' that may resolve either to\r
576 # a module, a test case class, a TestSuite instance, a test method\r
577 # within a test case class, or a callable object which returns a\r
578 # TestCase or TestSuite instance."\r
579 #\r
580 # Is ValueError raised in response to an empty name?\r
581 def test_loadTestsFromNames__empty_name(self):\r
582 loader = unittest.TestLoader()\r
583\r
584 try:\r
585 loader.loadTestsFromNames([''])\r
586 except ValueError, e:\r
587 self.assertEqual(str(e), "Empty module name")\r
588 else:\r
589 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")\r
590\r
591 # "The specifier name is a ``dotted name'' that may resolve either to\r
592 # a module, a test case class, a TestSuite instance, a test method\r
593 # within a test case class, or a callable object which returns a\r
594 # TestCase or TestSuite instance."\r
595 #\r
596 # What happens when presented with an impossible module name?\r
597 def test_loadTestsFromNames__malformed_name(self):\r
598 loader = unittest.TestLoader()\r
599\r
600 # XXX Should this raise ValueError or ImportError?\r
601 try:\r
602 loader.loadTestsFromNames(['abc () //'])\r
603 except ValueError:\r
604 pass\r
605 except ImportError:\r
606 pass\r
607 else:\r
608 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")\r
609\r
610 # "The specifier name is a ``dotted name'' that may resolve either to\r
611 # a module, a test case class, a TestSuite instance, a test method\r
612 # within a test case class, or a callable object which returns a\r
613 # TestCase or TestSuite instance."\r
614 #\r
615 # What happens when no module can be found for the given name?\r
616 def test_loadTestsFromNames__unknown_module_name(self):\r
617 loader = unittest.TestLoader()\r
618\r
619 try:\r
620 loader.loadTestsFromNames(['sdasfasfasdf'])\r
621 except ImportError, e:\r
622 self.assertEqual(str(e), "No module named sdasfasfasdf")\r
623 else:\r
624 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")\r
625\r
626 # "The specifier name is a ``dotted name'' that may resolve either to\r
627 # a module, a test case class, a TestSuite instance, a test method\r
628 # within a test case class, or a callable object which returns a\r
629 # TestCase or TestSuite instance."\r
630 #\r
631 # What happens when the module can be found, but not the attribute?\r
632 def test_loadTestsFromNames__unknown_attr_name(self):\r
633 loader = unittest.TestLoader()\r
634\r
635 try:\r
636 loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])\r
637 except AttributeError, e:\r
638 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")\r
639 else:\r
640 self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")\r
641\r
642 # "The specifier name is a ``dotted name'' that may resolve either to\r
643 # a module, a test case class, a TestSuite instance, a test method\r
644 # within a test case class, or a callable object which returns a\r
645 # TestCase or TestSuite instance."\r
646 # ...\r
647 # "The method optionally resolves name relative to the given module"\r
648 #\r
649 # What happens when given an unknown attribute on a specified `module`\r
650 # argument?\r
651 def test_loadTestsFromNames__unknown_name_relative_1(self):\r
652 loader = unittest.TestLoader()\r
653\r
654 try:\r
655 loader.loadTestsFromNames(['sdasfasfasdf'], unittest)\r
656 except AttributeError, e:\r
657 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")\r
658 else:\r
659 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")\r
660\r
661 # "The specifier name is a ``dotted name'' that may resolve either to\r
662 # a module, a test case class, a TestSuite instance, a test method\r
663 # within a test case class, or a callable object which returns a\r
664 # TestCase or TestSuite instance."\r
665 # ...\r
666 # "The method optionally resolves name relative to the given module"\r
667 #\r
668 # Do unknown attributes (relative to a provided module) still raise an\r
669 # exception even in the presence of valid attribute names?\r
670 def test_loadTestsFromNames__unknown_name_relative_2(self):\r
671 loader = unittest.TestLoader()\r
672\r
673 try:\r
674 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)\r
675 except AttributeError, e:\r
676 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")\r
677 else:\r
678 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")\r
679\r
680 # "The specifier name is a ``dotted name'' that may resolve either to\r
681 # a module, a test case class, a TestSuite instance, a test method\r
682 # within a test case class, or a callable object which returns a\r
683 # TestCase or TestSuite instance."\r
684 # ...\r
685 # "The method optionally resolves name relative to the given module"\r
686 #\r
687 # What happens when faced with the empty string?\r
688 #\r
689 # XXX This currently raises AttributeError, though ValueError is probably\r
690 # more appropriate\r
691 def test_loadTestsFromNames__relative_empty_name(self):\r
692 loader = unittest.TestLoader()\r
693\r
694 try:\r
695 loader.loadTestsFromNames([''], unittest)\r
696 except AttributeError:\r
697 pass\r
698 else:\r
699 self.fail("Failed to raise ValueError")\r
700\r
701 # "The specifier name is a ``dotted name'' that may resolve either to\r
702 # a module, a test case class, a TestSuite instance, a test method\r
703 # within a test case class, or a callable object which returns a\r
704 # TestCase or TestSuite instance."\r
705 # ...\r
706 # "The method optionally resolves name relative to the given module"\r
707 #\r
708 # What happens when presented with an impossible attribute name?\r
709 def test_loadTestsFromNames__relative_malformed_name(self):\r
710 loader = unittest.TestLoader()\r
711\r
712 # XXX Should this raise AttributeError or ValueError?\r
713 try:\r
714 loader.loadTestsFromNames(['abc () //'], unittest)\r
715 except AttributeError:\r
716 pass\r
717 except ValueError:\r
718 pass\r
719 else:\r
720 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")\r
721\r
722 # "The method optionally resolves name relative to the given module"\r
723 #\r
724 # Does loadTestsFromNames() make sure the provided `module` is in fact\r
725 # a module?\r
726 #\r
727 # XXX This validation is currently not done. This flexibility should\r
728 # either be documented or a TypeError should be raised.\r
729 def test_loadTestsFromNames__relative_not_a_module(self):\r
730 class MyTestCase(unittest.TestCase):\r
731 def test(self):\r
732 pass\r
733\r
734 class NotAModule(object):\r
735 test_2 = MyTestCase\r
736\r
737 loader = unittest.TestLoader()\r
738 suite = loader.loadTestsFromNames(['test_2'], NotAModule)\r
739\r
740 reference = [unittest.TestSuite([MyTestCase('test')])]\r
741 self.assertEqual(list(suite), reference)\r
742\r
743 # "The specifier name is a ``dotted name'' that may resolve either to\r
744 # a module, a test case class, a TestSuite instance, a test method\r
745 # within a test case class, or a callable object which returns a\r
746 # TestCase or TestSuite instance."\r
747 #\r
748 # Does it raise an exception if the name resolves to an invalid\r
749 # object?\r
750 def test_loadTestsFromNames__relative_bad_object(self):\r
751 m = types.ModuleType('m')\r
752 m.testcase_1 = object()\r
753\r
754 loader = unittest.TestLoader()\r
755 try:\r
756 loader.loadTestsFromNames(['testcase_1'], m)\r
757 except TypeError:\r
758 pass\r
759 else:\r
760 self.fail("Should have raised TypeError")\r
761\r
762 # "The specifier name is a ``dotted name'' that may resolve ... to\r
763 # ... a test case class"\r
764 def test_loadTestsFromNames__relative_TestCase_subclass(self):\r
765 m = types.ModuleType('m')\r
766 class MyTestCase(unittest.TestCase):\r
767 def test(self):\r
768 pass\r
769 m.testcase_1 = MyTestCase\r
770\r
771 loader = unittest.TestLoader()\r
772 suite = loader.loadTestsFromNames(['testcase_1'], m)\r
773 self.assertIsInstance(suite, loader.suiteClass)\r
774\r
775 expected = loader.suiteClass([MyTestCase('test')])\r
776 self.assertEqual(list(suite), [expected])\r
777\r
778 # "The specifier name is a ``dotted name'' that may resolve ... to\r
779 # ... a TestSuite instance"\r
780 def test_loadTestsFromNames__relative_TestSuite(self):\r
781 m = types.ModuleType('m')\r
782 class MyTestCase(unittest.TestCase):\r
783 def test(self):\r
784 pass\r
785 m.testsuite = unittest.TestSuite([MyTestCase('test')])\r
786\r
787 loader = unittest.TestLoader()\r
788 suite = loader.loadTestsFromNames(['testsuite'], m)\r
789 self.assertIsInstance(suite, loader.suiteClass)\r
790\r
791 self.assertEqual(list(suite), [m.testsuite])\r
792\r
793 # "The specifier name is a ``dotted name'' that may resolve ... to ... a\r
794 # test method within a test case class"\r
795 def test_loadTestsFromNames__relative_testmethod(self):\r
796 m = types.ModuleType('m')\r
797 class MyTestCase(unittest.TestCase):\r
798 def test(self):\r
799 pass\r
800 m.testcase_1 = MyTestCase\r
801\r
802 loader = unittest.TestLoader()\r
803 suite = loader.loadTestsFromNames(['testcase_1.test'], m)\r
804 self.assertIsInstance(suite, loader.suiteClass)\r
805\r
806 ref_suite = unittest.TestSuite([MyTestCase('test')])\r
807 self.assertEqual(list(suite), [ref_suite])\r
808\r
809 # "The specifier name is a ``dotted name'' that may resolve ... to ... a\r
810 # test method within a test case class"\r
811 #\r
812 # Does the method gracefully handle names that initially look like they\r
813 # resolve to "a test method within a test case class" but don't?\r
814 def test_loadTestsFromNames__relative_invalid_testmethod(self):\r
815 m = types.ModuleType('m')\r
816 class MyTestCase(unittest.TestCase):\r
817 def test(self):\r
818 pass\r
819 m.testcase_1 = MyTestCase\r
820\r
821 loader = unittest.TestLoader()\r
822 try:\r
823 loader.loadTestsFromNames(['testcase_1.testfoo'], m)\r
824 except AttributeError, e:\r
825 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")\r
826 else:\r
827 self.fail("Failed to raise AttributeError")\r
828\r
829 # "The specifier name is a ``dotted name'' that may resolve ... to\r
830 # ... a callable object which returns a ... TestSuite instance"\r
831 def test_loadTestsFromNames__callable__TestSuite(self):\r
832 m = types.ModuleType('m')\r
833 testcase_1 = unittest.FunctionTestCase(lambda: None)\r
834 testcase_2 = unittest.FunctionTestCase(lambda: None)\r
835 def return_TestSuite():\r
836 return unittest.TestSuite([testcase_1, testcase_2])\r
837 m.return_TestSuite = return_TestSuite\r
838\r
839 loader = unittest.TestLoader()\r
840 suite = loader.loadTestsFromNames(['return_TestSuite'], m)\r
841 self.assertIsInstance(suite, loader.suiteClass)\r
842\r
843 expected = unittest.TestSuite([testcase_1, testcase_2])\r
844 self.assertEqual(list(suite), [expected])\r
845\r
846 # "The specifier name is a ``dotted name'' that may resolve ... to\r
847 # ... a callable object which returns a TestCase ... instance"\r
848 def test_loadTestsFromNames__callable__TestCase_instance(self):\r
849 m = types.ModuleType('m')\r
850 testcase_1 = unittest.FunctionTestCase(lambda: None)\r
851 def return_TestCase():\r
852 return testcase_1\r
853 m.return_TestCase = return_TestCase\r
854\r
855 loader = unittest.TestLoader()\r
856 suite = loader.loadTestsFromNames(['return_TestCase'], m)\r
857 self.assertIsInstance(suite, loader.suiteClass)\r
858\r
859 ref_suite = unittest.TestSuite([testcase_1])\r
860 self.assertEqual(list(suite), [ref_suite])\r
861\r
862 # "The specifier name is a ``dotted name'' that may resolve ... to\r
863 # ... a callable object which returns a TestCase or TestSuite instance"\r
864 #\r
865 # Are staticmethods handled correctly?\r
866 def test_loadTestsFromNames__callable__call_staticmethod(self):\r
867 m = types.ModuleType('m')\r
868 class Test1(unittest.TestCase):\r
869 def test(self):\r
870 pass\r
871\r
872 testcase_1 = Test1('test')\r
873 class Foo(unittest.TestCase):\r
874 @staticmethod\r
875 def foo():\r
876 return testcase_1\r
877 m.Foo = Foo\r
878\r
879 loader = unittest.TestLoader()\r
880 suite = loader.loadTestsFromNames(['Foo.foo'], m)\r
881 self.assertIsInstance(suite, loader.suiteClass)\r
882\r
883 ref_suite = unittest.TestSuite([testcase_1])\r
884 self.assertEqual(list(suite), [ref_suite])\r
885\r
886 # "The specifier name is a ``dotted name'' that may resolve ... to\r
887 # ... a callable object which returns a TestCase or TestSuite instance"\r
888 #\r
889 # What happens when the callable returns something else?\r
890 def test_loadTestsFromNames__callable__wrong_type(self):\r
891 m = types.ModuleType('m')\r
892 def return_wrong():\r
893 return 6\r
894 m.return_wrong = return_wrong\r
895\r
896 loader = unittest.TestLoader()\r
897 try:\r
898 loader.loadTestsFromNames(['return_wrong'], m)\r
899 except TypeError:\r
900 pass\r
901 else:\r
902 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")\r
903\r
904 # "The specifier can refer to modules and packages which have not been\r
905 # imported; they will be imported as a side-effect"\r
906 def test_loadTestsFromNames__module_not_loaded(self):\r
907 # We're going to try to load this module as a side-effect, so it\r
908 # better not be loaded before we try.\r
909 #\r
910 module_name = 'unittest.test.dummy'\r
911 sys.modules.pop(module_name, None)\r
912\r
913 loader = unittest.TestLoader()\r
914 try:\r
915 suite = loader.loadTestsFromNames([module_name])\r
916\r
917 self.assertIsInstance(suite, loader.suiteClass)\r
918 self.assertEqual(list(suite), [unittest.TestSuite()])\r
919\r
920 # module should now be loaded, thanks to loadTestsFromName()\r
921 self.assertIn(module_name, sys.modules)\r
922 finally:\r
923 if module_name in sys.modules:\r
924 del sys.modules[module_name]\r
925\r
926 ################################################################\r
927 ### /Tests for TestLoader.loadTestsFromNames()\r
928\r
929 ### Tests for TestLoader.getTestCaseNames()\r
930 ################################################################\r
931\r
932 # "Return a sorted sequence of method names found within testCaseClass"\r
933 #\r
934 # Test.foobar is defined to make sure getTestCaseNames() respects\r
935 # loader.testMethodPrefix\r
936 def test_getTestCaseNames(self):\r
937 class Test(unittest.TestCase):\r
938 def test_1(self): pass\r
939 def test_2(self): pass\r
940 def foobar(self): pass\r
941\r
942 loader = unittest.TestLoader()\r
943\r
944 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])\r
945\r
946 # "Return a sorted sequence of method names found within testCaseClass"\r
947 #\r
948 # Does getTestCaseNames() behave appropriately if no tests are found?\r
949 def test_getTestCaseNames__no_tests(self):\r
950 class Test(unittest.TestCase):\r
951 def foobar(self): pass\r
952\r
953 loader = unittest.TestLoader()\r
954\r
955 self.assertEqual(loader.getTestCaseNames(Test), [])\r
956\r
957 # "Return a sorted sequence of method names found within testCaseClass"\r
958 #\r
959 # Are not-TestCases handled gracefully?\r
960 #\r
961 # XXX This should raise a TypeError, not return a list\r
962 #\r
963 # XXX It's too late in the 2.5 release cycle to fix this, but it should\r
964 # probably be revisited for 2.6\r
965 def test_getTestCaseNames__not_a_TestCase(self):\r
966 class BadCase(int):\r
967 def test_foo(self):\r
968 pass\r
969\r
970 loader = unittest.TestLoader()\r
971 names = loader.getTestCaseNames(BadCase)\r
972\r
973 self.assertEqual(names, ['test_foo'])\r
974\r
975 # "Return a sorted sequence of method names found within testCaseClass"\r
976 #\r
977 # Make sure inherited names are handled.\r
978 #\r
979 # TestP.foobar is defined to make sure getTestCaseNames() respects\r
980 # loader.testMethodPrefix\r
981 def test_getTestCaseNames__inheritance(self):\r
982 class TestP(unittest.TestCase):\r
983 def test_1(self): pass\r
984 def test_2(self): pass\r
985 def foobar(self): pass\r
986\r
987 class TestC(TestP):\r
988 def test_1(self): pass\r
989 def test_3(self): pass\r
990\r
991 loader = unittest.TestLoader()\r
992\r
993 names = ['test_1', 'test_2', 'test_3']\r
994 self.assertEqual(loader.getTestCaseNames(TestC), names)\r
995\r
996 ################################################################\r
997 ### /Tests for TestLoader.getTestCaseNames()\r
998\r
999 ### Tests for TestLoader.testMethodPrefix\r
1000 ################################################################\r
1001\r
1002 # "String giving the prefix of method names which will be interpreted as\r
1003 # test methods"\r
1004 #\r
1005 # Implicit in the documentation is that testMethodPrefix is respected by\r
1006 # all loadTestsFrom* methods.\r
1007 def test_testMethodPrefix__loadTestsFromTestCase(self):\r
1008 class Foo(unittest.TestCase):\r
1009 def test_1(self): pass\r
1010 def test_2(self): pass\r
1011 def foo_bar(self): pass\r
1012\r
1013 tests_1 = unittest.TestSuite([Foo('foo_bar')])\r
1014 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])\r
1015\r
1016 loader = unittest.TestLoader()\r
1017 loader.testMethodPrefix = 'foo'\r
1018 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)\r
1019\r
1020 loader.testMethodPrefix = 'test'\r
1021 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)\r
1022\r
1023 # "String giving the prefix of method names which will be interpreted as\r
1024 # test methods"\r
1025 #\r
1026 # Implicit in the documentation is that testMethodPrefix is respected by\r
1027 # all loadTestsFrom* methods.\r
1028 def test_testMethodPrefix__loadTestsFromModule(self):\r
1029 m = types.ModuleType('m')\r
1030 class Foo(unittest.TestCase):\r
1031 def test_1(self): pass\r
1032 def test_2(self): pass\r
1033 def foo_bar(self): pass\r
1034 m.Foo = Foo\r
1035\r
1036 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]\r
1037 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]\r
1038\r
1039 loader = unittest.TestLoader()\r
1040 loader.testMethodPrefix = 'foo'\r
1041 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)\r
1042\r
1043 loader.testMethodPrefix = 'test'\r
1044 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)\r
1045\r
1046 # "String giving the prefix of method names which will be interpreted as\r
1047 # test methods"\r
1048 #\r
1049 # Implicit in the documentation is that testMethodPrefix is respected by\r
1050 # all loadTestsFrom* methods.\r
1051 def test_testMethodPrefix__loadTestsFromName(self):\r
1052 m = types.ModuleType('m')\r
1053 class Foo(unittest.TestCase):\r
1054 def test_1(self): pass\r
1055 def test_2(self): pass\r
1056 def foo_bar(self): pass\r
1057 m.Foo = Foo\r
1058\r
1059 tests_1 = unittest.TestSuite([Foo('foo_bar')])\r
1060 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])\r
1061\r
1062 loader = unittest.TestLoader()\r
1063 loader.testMethodPrefix = 'foo'\r
1064 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)\r
1065\r
1066 loader.testMethodPrefix = 'test'\r
1067 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)\r
1068\r
1069 # "String giving the prefix of method names which will be interpreted as\r
1070 # test methods"\r
1071 #\r
1072 # Implicit in the documentation is that testMethodPrefix is respected by\r
1073 # all loadTestsFrom* methods.\r
1074 def test_testMethodPrefix__loadTestsFromNames(self):\r
1075 m = types.ModuleType('m')\r
1076 class Foo(unittest.TestCase):\r
1077 def test_1(self): pass\r
1078 def test_2(self): pass\r
1079 def foo_bar(self): pass\r
1080 m.Foo = Foo\r
1081\r
1082 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])\r
1083 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])\r
1084 tests_2 = unittest.TestSuite([tests_2])\r
1085\r
1086 loader = unittest.TestLoader()\r
1087 loader.testMethodPrefix = 'foo'\r
1088 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)\r
1089\r
1090 loader.testMethodPrefix = 'test'\r
1091 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)\r
1092\r
1093 # "The default value is 'test'"\r
1094 def test_testMethodPrefix__default_value(self):\r
1095 loader = unittest.TestLoader()\r
1096 self.assertTrue(loader.testMethodPrefix == 'test')\r
1097\r
1098 ################################################################\r
1099 ### /Tests for TestLoader.testMethodPrefix\r
1100\r
1101 ### Tests for TestLoader.sortTestMethodsUsing\r
1102 ################################################################\r
1103\r
1104 # "Function to be used to compare method names when sorting them in\r
1105 # getTestCaseNames() and all the loadTestsFromX() methods"\r
1106 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):\r
1107 def reversed_cmp(x, y):\r
1108 return -cmp(x, y)\r
1109\r
1110 class Foo(unittest.TestCase):\r
1111 def test_1(self): pass\r
1112 def test_2(self): pass\r
1113\r
1114 loader = unittest.TestLoader()\r
1115 loader.sortTestMethodsUsing = reversed_cmp\r
1116\r
1117 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])\r
1118 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)\r
1119\r
1120 # "Function to be used to compare method names when sorting them in\r
1121 # getTestCaseNames() and all the loadTestsFromX() methods"\r
1122 def test_sortTestMethodsUsing__loadTestsFromModule(self):\r
1123 def reversed_cmp(x, y):\r
1124 return -cmp(x, y)\r
1125\r
1126 m = types.ModuleType('m')\r
1127 class Foo(unittest.TestCase):\r
1128 def test_1(self): pass\r
1129 def test_2(self): pass\r
1130 m.Foo = Foo\r
1131\r
1132 loader = unittest.TestLoader()\r
1133 loader.sortTestMethodsUsing = reversed_cmp\r
1134\r
1135 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]\r
1136 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)\r
1137\r
1138 # "Function to be used to compare method names when sorting them in\r
1139 # getTestCaseNames() and all the loadTestsFromX() methods"\r
1140 def test_sortTestMethodsUsing__loadTestsFromName(self):\r
1141 def reversed_cmp(x, y):\r
1142 return -cmp(x, y)\r
1143\r
1144 m = types.ModuleType('m')\r
1145 class Foo(unittest.TestCase):\r
1146 def test_1(self): pass\r
1147 def test_2(self): pass\r
1148 m.Foo = Foo\r
1149\r
1150 loader = unittest.TestLoader()\r
1151 loader.sortTestMethodsUsing = reversed_cmp\r
1152\r
1153 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])\r
1154 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)\r
1155\r
1156 # "Function to be used to compare method names when sorting them in\r
1157 # getTestCaseNames() and all the loadTestsFromX() methods"\r
1158 def test_sortTestMethodsUsing__loadTestsFromNames(self):\r
1159 def reversed_cmp(x, y):\r
1160 return -cmp(x, y)\r
1161\r
1162 m = types.ModuleType('m')\r
1163 class Foo(unittest.TestCase):\r
1164 def test_1(self): pass\r
1165 def test_2(self): pass\r
1166 m.Foo = Foo\r
1167\r
1168 loader = unittest.TestLoader()\r
1169 loader.sortTestMethodsUsing = reversed_cmp\r
1170\r
1171 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]\r
1172 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)\r
1173\r
1174 # "Function to be used to compare method names when sorting them in\r
1175 # getTestCaseNames()"\r
1176 #\r
1177 # Does it actually affect getTestCaseNames()?\r
1178 def test_sortTestMethodsUsing__getTestCaseNames(self):\r
1179 def reversed_cmp(x, y):\r
1180 return -cmp(x, y)\r
1181\r
1182 class Foo(unittest.TestCase):\r
1183 def test_1(self): pass\r
1184 def test_2(self): pass\r
1185\r
1186 loader = unittest.TestLoader()\r
1187 loader.sortTestMethodsUsing = reversed_cmp\r
1188\r
1189 test_names = ['test_2', 'test_1']\r
1190 self.assertEqual(loader.getTestCaseNames(Foo), test_names)\r
1191\r
1192 # "The default value is the built-in cmp() function"\r
1193 def test_sortTestMethodsUsing__default_value(self):\r
1194 loader = unittest.TestLoader()\r
1195 self.assertTrue(loader.sortTestMethodsUsing is cmp)\r
1196\r
1197 # "it can be set to None to disable the sort."\r
1198 #\r
1199 # XXX How is this different from reassigning cmp? Are the tests returned\r
1200 # in a random order or something? This behaviour should die\r
1201 def test_sortTestMethodsUsing__None(self):\r
1202 class Foo(unittest.TestCase):\r
1203 def test_1(self): pass\r
1204 def test_2(self): pass\r
1205\r
1206 loader = unittest.TestLoader()\r
1207 loader.sortTestMethodsUsing = None\r
1208\r
1209 test_names = ['test_2', 'test_1']\r
1210 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))\r
1211\r
1212 ################################################################\r
1213 ### /Tests for TestLoader.sortTestMethodsUsing\r
1214\r
1215 ### Tests for TestLoader.suiteClass\r
1216 ################################################################\r
1217\r
1218 # "Callable object that constructs a test suite from a list of tests."\r
1219 def test_suiteClass__loadTestsFromTestCase(self):\r
1220 class Foo(unittest.TestCase):\r
1221 def test_1(self): pass\r
1222 def test_2(self): pass\r
1223 def foo_bar(self): pass\r
1224\r
1225 tests = [Foo('test_1'), Foo('test_2')]\r
1226\r
1227 loader = unittest.TestLoader()\r
1228 loader.suiteClass = list\r
1229 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)\r
1230\r
1231 # It is implicit in the documentation for TestLoader.suiteClass that\r
1232 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure\r
1233 def test_suiteClass__loadTestsFromModule(self):\r
1234 m = types.ModuleType('m')\r
1235 class Foo(unittest.TestCase):\r
1236 def test_1(self): pass\r
1237 def test_2(self): pass\r
1238 def foo_bar(self): pass\r
1239 m.Foo = Foo\r
1240\r
1241 tests = [[Foo('test_1'), Foo('test_2')]]\r
1242\r
1243 loader = unittest.TestLoader()\r
1244 loader.suiteClass = list\r
1245 self.assertEqual(loader.loadTestsFromModule(m), tests)\r
1246\r
1247 # It is implicit in the documentation for TestLoader.suiteClass that\r
1248 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure\r
1249 def test_suiteClass__loadTestsFromName(self):\r
1250 m = types.ModuleType('m')\r
1251 class Foo(unittest.TestCase):\r
1252 def test_1(self): pass\r
1253 def test_2(self): pass\r
1254 def foo_bar(self): pass\r
1255 m.Foo = Foo\r
1256\r
1257 tests = [Foo('test_1'), Foo('test_2')]\r
1258\r
1259 loader = unittest.TestLoader()\r
1260 loader.suiteClass = list\r
1261 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)\r
1262\r
1263 # It is implicit in the documentation for TestLoader.suiteClass that\r
1264 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure\r
1265 def test_suiteClass__loadTestsFromNames(self):\r
1266 m = types.ModuleType('m')\r
1267 class Foo(unittest.TestCase):\r
1268 def test_1(self): pass\r
1269 def test_2(self): pass\r
1270 def foo_bar(self): pass\r
1271 m.Foo = Foo\r
1272\r
1273 tests = [[Foo('test_1'), Foo('test_2')]]\r
1274\r
1275 loader = unittest.TestLoader()\r
1276 loader.suiteClass = list\r
1277 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)\r
1278\r
1279 # "The default value is the TestSuite class"\r
1280 def test_suiteClass__default_value(self):\r
1281 loader = unittest.TestLoader()\r
1282 self.assertTrue(loader.suiteClass is unittest.TestSuite)\r
1283\r
1284\r
1285if __name__ == '__main__':\r
1286 unittest.main()\r