blob: 7e60548f1cc4a867699c13134997147beb4e7ea7 [file] [log] [blame]
Jim Fultonfafd8742004-08-28 15:22:12 +00001"""Test script for unittest.
2
Georg Brandl15c5ce92007-03-07 09:09:40 +00003By Collin Winter <collinw at gmail.com>
4
5Still need testing:
6 TestCase.{assert,fail}* methods (some are tested implicitly)
Jim Fultonfafd8742004-08-28 15:22:12 +00007"""
8
Georg Brandl15c5ce92007-03-07 09:09:40 +00009from test import test_support
Jim Fultonfafd8742004-08-28 15:22:12 +000010import unittest
Georg Brandl15c5ce92007-03-07 09:09:40 +000011from unittest import TestCase
Jim Fultonfafd8742004-08-28 15:22:12 +000012
Georg Brandl15c5ce92007-03-07 09:09:40 +000013### Support code
14################################################################
Jim Fultonfafd8742004-08-28 15:22:12 +000015
Georg Brandl15c5ce92007-03-07 09:09:40 +000016class LoggingResult(unittest.TestResult):
17 def __init__(self, log):
18 self._events = log
19 super(LoggingResult, self).__init__()
20
21 def startTest(self, test):
22 self._events.append('startTest')
23 super(LoggingResult, self).startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +000024
Georg Brandl15c5ce92007-03-07 09:09:40 +000025 def stopTest(self, test):
26 self._events.append('stopTest')
27 super(LoggingResult, self).stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +000028
Georg Brandl15c5ce92007-03-07 09:09:40 +000029 def addFailure(self, *args):
30 self._events.append('addFailure')
31 super(LoggingResult, self).addFailure(*args)
Tim Petersea5962f2007-03-12 18:07:52 +000032
Georg Brandl15c5ce92007-03-07 09:09:40 +000033 def addError(self, *args):
34 self._events.append('addError')
35 super(LoggingResult, self).addError(*args)
36
37class TestEquality(object):
Tim Petersea5962f2007-03-12 18:07:52 +000038 # Check for a valid __eq__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000039 def test_eq(self):
40 for obj_1, obj_2 in self.eq_pairs:
41 self.assertEqual(obj_1, obj_2)
42 self.assertEqual(obj_2, obj_1)
Tim Petersea5962f2007-03-12 18:07:52 +000043
44 # Check for a valid __ne__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000045 def test_ne(self):
46 for obj_1, obj_2 in self.ne_pairs:
47 self.failIfEqual(obj_1, obj_2)
48 self.failIfEqual(obj_2, obj_1)
Tim Petersea5962f2007-03-12 18:07:52 +000049
Georg Brandl15c5ce92007-03-07 09:09:40 +000050class TestHashing(object):
Tim Petersea5962f2007-03-12 18:07:52 +000051 # Check for a valid __hash__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000052 def test_hash(self):
53 for obj_1, obj_2 in self.eq_pairs:
54 try:
55 assert hash(obj_1) == hash(obj_2)
56 except KeyboardInterrupt:
57 raise
58 except AssertionError:
59 self.fail("%s and %s do not hash equal" % (obj_1, obj_2))
60 except Exception, e:
61 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
Tim Petersea5962f2007-03-12 18:07:52 +000062
Georg Brandl15c5ce92007-03-07 09:09:40 +000063 for obj_1, obj_2 in self.ne_pairs:
64 try:
65 assert hash(obj_1) != hash(obj_2)
66 except KeyboardInterrupt:
67 raise
68 except AssertionError:
69 self.fail("%s and %s hash equal, but shouldn't" % (obj_1, obj_2))
70 except Exception, e:
71 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
Tim Petersea5962f2007-03-12 18:07:52 +000072
Georg Brandl15c5ce92007-03-07 09:09:40 +000073
74################################################################
75### /Support code
76
77class Test_TestLoader(TestCase):
78
79 ### Tests for TestLoader.loadTestsFromTestCase
80 ################################################################
81
82 # "Return a suite of all tests cases contained in the TestCase-derived
83 # class testCaseClass"
84 def test_loadTestsFromTestCase(self):
85 class Foo(unittest.TestCase):
86 def test_1(self): pass
87 def test_2(self): pass
88 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +000089
Georg Brandl15c5ce92007-03-07 09:09:40 +000090 tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +000091
Georg Brandl15c5ce92007-03-07 09:09:40 +000092 loader = unittest.TestLoader()
93 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +000094
Georg Brandl15c5ce92007-03-07 09:09:40 +000095 # "Return a suite of all tests cases contained in the TestCase-derived
96 # class testCaseClass"
97 #
Tim Petersea5962f2007-03-12 18:07:52 +000098 # Make sure it does the right thing even if no tests were found
Georg Brandl15c5ce92007-03-07 09:09:40 +000099 def test_loadTestsFromTestCase__no_matches(self):
100 class Foo(unittest.TestCase):
101 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000102
Georg Brandl15c5ce92007-03-07 09:09:40 +0000103 empty_suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +0000104
Georg Brandl15c5ce92007-03-07 09:09:40 +0000105 loader = unittest.TestLoader()
106 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
Tim Petersea5962f2007-03-12 18:07:52 +0000107
Georg Brandl15c5ce92007-03-07 09:09:40 +0000108 # "Return a suite of all tests cases contained in the TestCase-derived
109 # class testCaseClass"
110 #
111 # What happens if loadTestsFromTestCase() is given an object
112 # that isn't a subclass of TestCase? Specifically, what happens
113 # if testCaseClass is a subclass of TestSuite?
114 #
115 # This is checked for specifically in the code, so we better add a
116 # test for it.
117 def test_loadTestsFromTestCase__TestSuite_subclass(self):
118 class NotATestCase(unittest.TestSuite):
119 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000120
Georg Brandl15c5ce92007-03-07 09:09:40 +0000121 loader = unittest.TestLoader()
122 try:
123 loader.loadTestsFromTestCase(NotATestCase)
124 except TypeError:
125 pass
126 else:
127 self.fail('Should raise TypeError')
Tim Petersea5962f2007-03-12 18:07:52 +0000128
Georg Brandl15c5ce92007-03-07 09:09:40 +0000129 # "Return a suite of all tests cases contained in the TestCase-derived
130 # class testCaseClass"
131 #
132 # Make sure loadTestsFromTestCase() picks up the default test method
133 # name (as specified by TestCase), even though the method name does
134 # not match the default TestLoader.testMethodPrefix string
135 def test_loadTestsFromTestCase__default_method_name(self):
136 class Foo(unittest.TestCase):
137 def runTest(self):
138 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000139
Georg Brandl15c5ce92007-03-07 09:09:40 +0000140 loader = unittest.TestLoader()
141 # This has to be false for the test to succeed
142 self.failIf('runTest'.startswith(loader.testMethodPrefix))
Tim Petersea5962f2007-03-12 18:07:52 +0000143
Georg Brandl15c5ce92007-03-07 09:09:40 +0000144 suite = loader.loadTestsFromTestCase(Foo)
145 self.failUnless(isinstance(suite, loader.suiteClass))
146 self.assertEqual(list(suite), [Foo('runTest')])
147
148 ################################################################
149 ### /Tests for TestLoader.loadTestsFromTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000150
Georg Brandl15c5ce92007-03-07 09:09:40 +0000151 ### Tests for TestLoader.loadTestsFromModule
152 ################################################################
153
154 # "This method searches `module` for classes derived from TestCase"
155 def test_loadTestsFromModule__TestCase_subclass(self):
156 import new
157 m = new.module('m')
158 class MyTestCase(unittest.TestCase):
159 def test(self):
160 pass
161 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000162
Georg Brandl15c5ce92007-03-07 09:09:40 +0000163 loader = unittest.TestLoader()
164 suite = loader.loadTestsFromModule(m)
165 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000166
Georg Brandl15c5ce92007-03-07 09:09:40 +0000167 expected = [loader.suiteClass([MyTestCase('test')])]
168 self.assertEqual(list(suite), expected)
Tim Petersea5962f2007-03-12 18:07:52 +0000169
Georg Brandl15c5ce92007-03-07 09:09:40 +0000170 # "This method searches `module` for classes derived from TestCase"
Tim Petersea5962f2007-03-12 18:07:52 +0000171 #
Georg Brandl15c5ce92007-03-07 09:09:40 +0000172 # What happens if no tests are found (no TestCase instances)?
173 def test_loadTestsFromModule__no_TestCase_instances(self):
174 import new
175 m = new.module('m')
Tim Petersea5962f2007-03-12 18:07:52 +0000176
Georg Brandl15c5ce92007-03-07 09:09:40 +0000177 loader = unittest.TestLoader()
178 suite = loader.loadTestsFromModule(m)
179 self.failUnless(isinstance(suite, loader.suiteClass))
180 self.assertEqual(list(suite), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000181
Georg Brandl15c5ce92007-03-07 09:09:40 +0000182 # "This method searches `module` for classes derived from TestCase"
183 #
Tim Petersea5962f2007-03-12 18:07:52 +0000184 # What happens if no tests are found (TestCases instances, but no tests)?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000185 def test_loadTestsFromModule__no_TestCase_tests(self):
186 import new
187 m = new.module('m')
188 class MyTestCase(unittest.TestCase):
189 pass
190 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000191
Georg Brandl15c5ce92007-03-07 09:09:40 +0000192 loader = unittest.TestLoader()
193 suite = loader.loadTestsFromModule(m)
194 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000195
Georg Brandl15c5ce92007-03-07 09:09:40 +0000196 self.assertEqual(list(suite), [loader.suiteClass()])
Tim Petersea5962f2007-03-12 18:07:52 +0000197
Georg Brandl15c5ce92007-03-07 09:09:40 +0000198 # "This method searches `module` for classes derived from TestCase"s
199 #
200 # What happens if loadTestsFromModule() is given something other
201 # than a module?
202 #
203 # XXX Currently, it succeeds anyway. This flexibility
204 # should either be documented or loadTestsFromModule() should
205 # raise a TypeError
206 #
207 # XXX Certain people are using this behaviour. We'll add a test for it
208 def test_loadTestsFromModule__not_a_module(self):
209 class MyTestCase(unittest.TestCase):
210 def test(self):
211 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000212
Georg Brandl15c5ce92007-03-07 09:09:40 +0000213 class NotAModule(object):
214 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000215
Georg Brandl15c5ce92007-03-07 09:09:40 +0000216 loader = unittest.TestLoader()
217 suite = loader.loadTestsFromModule(NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000218
Georg Brandl15c5ce92007-03-07 09:09:40 +0000219 reference = [unittest.TestSuite([MyTestCase('test')])]
220 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000221
Georg Brandl15c5ce92007-03-07 09:09:40 +0000222 ################################################################
223 ### /Tests for TestLoader.loadTestsFromModule()
Tim Petersea5962f2007-03-12 18:07:52 +0000224
Georg Brandl15c5ce92007-03-07 09:09:40 +0000225 ### Tests for TestLoader.loadTestsFromName()
226 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000227
Georg Brandl15c5ce92007-03-07 09:09:40 +0000228 # "The specifier name is a ``dotted name'' that may resolve either to
229 # a module, a test case class, a TestSuite instance, a test method
230 # within a test case class, or a callable object which returns a
231 # TestCase or TestSuite instance."
232 #
233 # Is ValueError raised in response to an empty name?
234 def test_loadTestsFromName__empty_name(self):
235 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000236
Georg Brandl15c5ce92007-03-07 09:09:40 +0000237 try:
238 loader.loadTestsFromName('')
239 except ValueError, e:
240 self.assertEqual(str(e), "Empty module name")
241 else:
242 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000243
Georg Brandl15c5ce92007-03-07 09:09:40 +0000244 # "The specifier name is a ``dotted name'' that may resolve either to
245 # a module, a test case class, a TestSuite instance, a test method
246 # within a test case class, or a callable object which returns a
247 # TestCase or TestSuite instance."
248 #
Tim Petersea5962f2007-03-12 18:07:52 +0000249 # What happens when the name contains invalid characters?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000250 def test_loadTestsFromName__malformed_name(self):
251 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000252
Georg Brandl15c5ce92007-03-07 09:09:40 +0000253 # XXX Should this raise ValueError or ImportError?
254 try:
255 loader.loadTestsFromName('abc () //')
256 except ValueError:
257 pass
258 except ImportError:
259 pass
260 else:
261 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000262
Georg Brandl15c5ce92007-03-07 09:09:40 +0000263 # "The specifier name is a ``dotted name'' that may resolve ... to a
264 # module"
265 #
Tim Petersea5962f2007-03-12 18:07:52 +0000266 # What happens when a module by that name can't be found?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000267 def test_loadTestsFromName__unknown_module_name(self):
268 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000269
Georg Brandl15c5ce92007-03-07 09:09:40 +0000270 try:
271 loader.loadTestsFromName('sdasfasfasdf')
272 except ImportError, e:
273 self.assertEqual(str(e), "No module named sdasfasfasdf")
274 else:
275 self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
Tim Petersea5962f2007-03-12 18:07:52 +0000276
Georg Brandl15c5ce92007-03-07 09:09:40 +0000277 # "The specifier name is a ``dotted name'' that may resolve either to
278 # a module, a test case class, a TestSuite instance, a test method
279 # within a test case class, or a callable object which returns a
280 # TestCase or TestSuite instance."
281 #
Tim Petersea5962f2007-03-12 18:07:52 +0000282 # What happens when the module is found, but the attribute can't?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000283 def test_loadTestsFromName__unknown_attr_name(self):
284 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000285
Georg Brandl15c5ce92007-03-07 09:09:40 +0000286 try:
287 loader.loadTestsFromName('unittest.sdasfasfasdf')
288 except AttributeError, e:
289 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
290 else:
291 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000292
Georg Brandl15c5ce92007-03-07 09:09:40 +0000293 # "The specifier name is a ``dotted name'' that may resolve either to
294 # a module, a test case class, a TestSuite instance, a test method
295 # within a test case class, or a callable object which returns a
296 # TestCase or TestSuite instance."
297 #
298 # What happens when we provide the module, but the attribute can't be
299 # found?
300 def test_loadTestsFromName__relative_unknown_name(self):
301 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000302
Georg Brandl15c5ce92007-03-07 09:09:40 +0000303 try:
304 loader.loadTestsFromName('sdasfasfasdf', unittest)
305 except AttributeError, e:
306 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
307 else:
308 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000309
Georg Brandl15c5ce92007-03-07 09:09:40 +0000310 # "The specifier name is a ``dotted name'' that may resolve either to
311 # a module, a test case class, a TestSuite instance, a test method
312 # within a test case class, or a callable object which returns a
313 # TestCase or TestSuite instance."
314 # ...
315 # "The method optionally resolves name relative to the given module"
316 #
317 # Does loadTestsFromName raise ValueError when passed an empty
318 # name relative to a provided module?
319 #
320 # XXX Should probably raise a ValueError instead of an AttributeError
321 def test_loadTestsFromName__relative_empty_name(self):
322 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000323
Georg Brandl15c5ce92007-03-07 09:09:40 +0000324 try:
325 loader.loadTestsFromName('', unittest)
326 except AttributeError, e:
327 pass
328 else:
329 self.fail("Failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000330
Georg Brandl15c5ce92007-03-07 09:09:40 +0000331 # "The specifier name is a ``dotted name'' that may resolve either to
332 # a module, a test case class, a TestSuite instance, a test method
333 # within a test case class, or a callable object which returns a
334 # TestCase or TestSuite instance."
335 # ...
336 # "The method optionally resolves name relative to the given module"
337 #
338 # What happens when an impossible name is given, relative to the provided
339 # `module`?
340 def test_loadTestsFromName__relative_malformed_name(self):
341 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000342
Georg Brandl15c5ce92007-03-07 09:09:40 +0000343 # XXX Should this raise AttributeError or ValueError?
344 try:
345 loader.loadTestsFromName('abc () //', unittest)
346 except ValueError:
347 pass
348 except AttributeError:
349 pass
350 else:
351 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
352
353 # "The method optionally resolves name relative to the given module"
354 #
355 # Does loadTestsFromName raise TypeError when the `module` argument
356 # isn't a module object?
357 #
358 # XXX Accepts the not-a-module object, ignorning the object's type
359 # This should raise an exception or the method name should be changed
360 #
361 # XXX Some people are relying on this, so keep it for now
362 def test_loadTestsFromName__relative_not_a_module(self):
363 class MyTestCase(unittest.TestCase):
364 def test(self):
365 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000366
Georg Brandl15c5ce92007-03-07 09:09:40 +0000367 class NotAModule(object):
368 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000369
Georg Brandl15c5ce92007-03-07 09:09:40 +0000370 loader = unittest.TestLoader()
371 suite = loader.loadTestsFromName('test_2', NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000372
Georg Brandl15c5ce92007-03-07 09:09:40 +0000373 reference = [MyTestCase('test')]
374 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000375
Georg Brandl15c5ce92007-03-07 09:09:40 +0000376 # "The specifier name is a ``dotted name'' that may resolve either to
377 # a module, a test case class, a TestSuite instance, a test method
378 # within a test case class, or a callable object which returns a
379 # TestCase or TestSuite instance."
380 #
381 # Does it raise an exception if the name resolves to an invalid
382 # object?
383 def test_loadTestsFromName__relative_bad_object(self):
384 import new
385 m = new.module('m')
386 m.testcase_1 = object()
Tim Petersea5962f2007-03-12 18:07:52 +0000387
Georg Brandl15c5ce92007-03-07 09:09:40 +0000388 loader = unittest.TestLoader()
389 try:
390 loader.loadTestsFromName('testcase_1', m)
391 except TypeError:
392 pass
393 else:
394 self.fail("Should have raised TypeError")
395
396 # "The specifier name is a ``dotted name'' that may
397 # resolve either to ... a test case class"
398 def test_loadTestsFromName__relative_TestCase_subclass(self):
399 import new
400 m = new.module('m')
401 class MyTestCase(unittest.TestCase):
402 def test(self):
403 pass
404 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000405
Georg Brandl15c5ce92007-03-07 09:09:40 +0000406 loader = unittest.TestLoader()
407 suite = loader.loadTestsFromName('testcase_1', m)
408 self.failUnless(isinstance(suite, loader.suiteClass))
409 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000410
Georg Brandl15c5ce92007-03-07 09:09:40 +0000411 # "The specifier name is a ``dotted name'' that may resolve either to
412 # a module, a test case class, a TestSuite instance, a test method
413 # within a test case class, or a callable object which returns a
414 # TestCase or TestSuite instance."
415 def test_loadTestsFromName__relative_TestSuite(self):
416 import new
417 m = new.module('m')
418 class MyTestCase(unittest.TestCase):
419 def test(self):
420 pass
421 m.testsuite = unittest.TestSuite([MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000422
Georg Brandl15c5ce92007-03-07 09:09:40 +0000423 loader = unittest.TestLoader()
424 suite = loader.loadTestsFromName('testsuite', m)
425 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000426
Georg Brandl15c5ce92007-03-07 09:09:40 +0000427 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000428
Georg Brandl15c5ce92007-03-07 09:09:40 +0000429 # "The specifier name is a ``dotted name'' that may resolve ... to
430 # ... a test method within a test case class"
431 def test_loadTestsFromName__relative_testmethod(self):
432 import new
433 m = new.module('m')
434 class MyTestCase(unittest.TestCase):
435 def test(self):
436 pass
437 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000438
Georg Brandl15c5ce92007-03-07 09:09:40 +0000439 loader = unittest.TestLoader()
440 suite = loader.loadTestsFromName('testcase_1.test', m)
441 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000442
Georg Brandl15c5ce92007-03-07 09:09:40 +0000443 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000444
Georg Brandl15c5ce92007-03-07 09:09:40 +0000445 # "The specifier name is a ``dotted name'' that may resolve either to
446 # a module, a test case class, a TestSuite instance, a test method
447 # within a test case class, or a callable object which returns a
448 # TestCase or TestSuite instance."
449 #
450 # Does loadTestsFromName() raise the proper exception when trying to
451 # resolve "a test method within a test case class" that doesn't exist
452 # for the given name (relative to a provided module)?
453 def test_loadTestsFromName__relative_invalid_testmethod(self):
454 import new
455 m = new.module('m')
456 class MyTestCase(unittest.TestCase):
457 def test(self):
458 pass
459 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000460
Georg Brandl15c5ce92007-03-07 09:09:40 +0000461 loader = unittest.TestLoader()
462 try:
463 loader.loadTestsFromName('testcase_1.testfoo', m)
464 except AttributeError, e:
465 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
466 else:
467 self.fail("Failed to raise AttributeError")
468
469 # "The specifier name is a ``dotted name'' that may resolve ... to
470 # ... a callable object which returns a ... TestSuite instance"
471 def test_loadTestsFromName__callable__TestSuite(self):
472 import new
473 m = new.module('m')
474 testcase_1 = unittest.FunctionTestCase(lambda: None)
475 testcase_2 = unittest.FunctionTestCase(lambda: None)
476 def return_TestSuite():
477 return unittest.TestSuite([testcase_1, testcase_2])
478 m.return_TestSuite = return_TestSuite
Tim Petersea5962f2007-03-12 18:07:52 +0000479
Georg Brandl15c5ce92007-03-07 09:09:40 +0000480 loader = unittest.TestLoader()
481 suite = loader.loadTestsFromName('return_TestSuite', m)
482 self.failUnless(isinstance(suite, loader.suiteClass))
483 self.assertEqual(list(suite), [testcase_1, testcase_2])
Tim Petersea5962f2007-03-12 18:07:52 +0000484
Georg Brandl15c5ce92007-03-07 09:09:40 +0000485 # "The specifier name is a ``dotted name'' that may resolve ... to
486 # ... a callable object which returns a TestCase ... instance"
487 def test_loadTestsFromName__callable__TestCase_instance(self):
488 import new
489 m = new.module('m')
490 testcase_1 = unittest.FunctionTestCase(lambda: None)
491 def return_TestCase():
492 return testcase_1
493 m.return_TestCase = return_TestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000494
Georg Brandl15c5ce92007-03-07 09:09:40 +0000495 loader = unittest.TestLoader()
496 suite = loader.loadTestsFromName('return_TestCase', m)
497 self.failUnless(isinstance(suite, loader.suiteClass))
498 self.assertEqual(list(suite), [testcase_1])
Tim Petersea5962f2007-03-12 18:07:52 +0000499
Georg Brandl15c5ce92007-03-07 09:09:40 +0000500 # "The specifier name is a ``dotted name'' that may resolve ... to
501 # ... a callable object which returns a TestCase or TestSuite instance"
502 #
503 # What happens if the callable returns something else?
504 def test_loadTestsFromName__callable__wrong_type(self):
505 import new
506 m = new.module('m')
507 def return_wrong():
508 return 6
509 m.return_wrong = return_wrong
Tim Petersea5962f2007-03-12 18:07:52 +0000510
Georg Brandl15c5ce92007-03-07 09:09:40 +0000511 loader = unittest.TestLoader()
512 try:
513 suite = loader.loadTestsFromName('return_wrong', m)
514 except TypeError:
515 pass
516 else:
517 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000518
Georg Brandl15c5ce92007-03-07 09:09:40 +0000519 # "The specifier can refer to modules and packages which have not been
Tim Petersea5962f2007-03-12 18:07:52 +0000520 # imported; they will be imported as a side-effect"
Georg Brandl15c5ce92007-03-07 09:09:40 +0000521 def test_loadTestsFromName__module_not_loaded(self):
522 # We're going to try to load this module as a side-effect, so it
523 # better not be loaded before we try.
524 #
525 # Why pick audioop? Google shows it isn't used very often, so there's
526 # a good chance that it won't be imported when this test is run
527 module_name = 'audioop'
Tim Petersea5962f2007-03-12 18:07:52 +0000528
Georg Brandl15c5ce92007-03-07 09:09:40 +0000529 import sys
530 if module_name in sys.modules:
531 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000532
Georg Brandl15c5ce92007-03-07 09:09:40 +0000533 loader = unittest.TestLoader()
534 try:
535 suite = loader.loadTestsFromName(module_name)
536
537 self.failUnless(isinstance(suite, loader.suiteClass))
538 self.assertEqual(list(suite), [])
539
540 # audioop should now be loaded, thanks to loadTestsFromName()
541 self.failUnless(module_name in sys.modules)
542 finally:
543 del sys.modules[module_name]
544
545 ################################################################
546 ### Tests for TestLoader.loadTestsFromName()
547
548 ### Tests for TestLoader.loadTestsFromNames()
549 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000550
Georg Brandl15c5ce92007-03-07 09:09:40 +0000551 # "Similar to loadTestsFromName(), but takes a sequence of names rather
552 # than a single name."
553 #
554 # What happens if that sequence of names is empty?
555 def test_loadTestsFromNames__empty_name_list(self):
556 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000557
Georg Brandl15c5ce92007-03-07 09:09:40 +0000558 suite = loader.loadTestsFromNames([])
559 self.failUnless(isinstance(suite, loader.suiteClass))
560 self.assertEqual(list(suite), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000561
Georg Brandl15c5ce92007-03-07 09:09:40 +0000562 # "Similar to loadTestsFromName(), but takes a sequence of names rather
563 # than a single name."
564 # ...
565 # "The method optionally resolves name relative to the given module"
566 #
567 # What happens if that sequence of names is empty?
568 #
569 # XXX Should this raise a ValueError or just return an empty TestSuite?
570 def test_loadTestsFromNames__relative_empty_name_list(self):
571 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000572
Georg Brandl15c5ce92007-03-07 09:09:40 +0000573 suite = loader.loadTestsFromNames([], unittest)
574 self.failUnless(isinstance(suite, loader.suiteClass))
575 self.assertEqual(list(suite), [])
576
577 # "The specifier name is a ``dotted name'' that may resolve either to
578 # a module, a test case class, a TestSuite instance, a test method
579 # within a test case class, or a callable object which returns a
580 # TestCase or TestSuite instance."
581 #
582 # Is ValueError raised in response to an empty name?
583 def test_loadTestsFromNames__empty_name(self):
584 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000585
Georg Brandl15c5ce92007-03-07 09:09:40 +0000586 try:
587 loader.loadTestsFromNames([''])
588 except ValueError, e:
589 self.assertEqual(str(e), "Empty module name")
590 else:
591 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000592
Georg Brandl15c5ce92007-03-07 09:09:40 +0000593 # "The specifier name is a ``dotted name'' that may resolve either to
594 # a module, a test case class, a TestSuite instance, a test method
595 # within a test case class, or a callable object which returns a
596 # TestCase or TestSuite instance."
597 #
Tim Petersea5962f2007-03-12 18:07:52 +0000598 # What happens when presented with an impossible module name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000599 def test_loadTestsFromNames__malformed_name(self):
600 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000601
Georg Brandl15c5ce92007-03-07 09:09:40 +0000602 # XXX Should this raise ValueError or ImportError?
603 try:
604 loader.loadTestsFromNames(['abc () //'])
605 except ValueError:
606 pass
607 except ImportError:
608 pass
609 else:
610 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000611
Georg Brandl15c5ce92007-03-07 09:09:40 +0000612 # "The specifier name is a ``dotted name'' that may resolve either to
613 # a module, a test case class, a TestSuite instance, a test method
614 # within a test case class, or a callable object which returns a
615 # TestCase or TestSuite instance."
616 #
Tim Petersea5962f2007-03-12 18:07:52 +0000617 # What happens when no module can be found for the given name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000618 def test_loadTestsFromNames__unknown_module_name(self):
619 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000620
Georg Brandl15c5ce92007-03-07 09:09:40 +0000621 try:
622 loader.loadTestsFromNames(['sdasfasfasdf'])
623 except ImportError, e:
624 self.assertEqual(str(e), "No module named sdasfasfasdf")
625 else:
626 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
Tim Petersea5962f2007-03-12 18:07:52 +0000627
Georg Brandl15c5ce92007-03-07 09:09:40 +0000628 # "The specifier name is a ``dotted name'' that may resolve either to
629 # a module, a test case class, a TestSuite instance, a test method
630 # within a test case class, or a callable object which returns a
631 # TestCase or TestSuite instance."
632 #
Tim Petersea5962f2007-03-12 18:07:52 +0000633 # What happens when the module can be found, but not the attribute?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000634 def test_loadTestsFromNames__unknown_attr_name(self):
635 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000636
Georg Brandl15c5ce92007-03-07 09:09:40 +0000637 try:
638 loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
639 except AttributeError, e:
640 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
641 else:
642 self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000643
Georg Brandl15c5ce92007-03-07 09:09:40 +0000644 # "The specifier name is a ``dotted name'' that may resolve either to
645 # a module, a test case class, a TestSuite instance, a test method
646 # within a test case class, or a callable object which returns a
647 # TestCase or TestSuite instance."
648 # ...
649 # "The method optionally resolves name relative to the given module"
650 #
651 # What happens when given an unknown attribute on a specified `module`
652 # argument?
653 def test_loadTestsFromNames__unknown_name_relative_1(self):
654 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000655
Georg Brandl15c5ce92007-03-07 09:09:40 +0000656 try:
657 loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
658 except AttributeError, e:
659 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
660 else:
661 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000662
Georg Brandl15c5ce92007-03-07 09:09:40 +0000663 # "The specifier name is a ``dotted name'' that may resolve either to
664 # a module, a test case class, a TestSuite instance, a test method
665 # within a test case class, or a callable object which returns a
666 # TestCase or TestSuite instance."
667 # ...
668 # "The method optionally resolves name relative to the given module"
669 #
670 # Do unknown attributes (relative to a provided module) still raise an
Tim Petersea5962f2007-03-12 18:07:52 +0000671 # exception even in the presence of valid attribute names?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000672 def test_loadTestsFromNames__unknown_name_relative_2(self):
673 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000674
Georg Brandl15c5ce92007-03-07 09:09:40 +0000675 try:
676 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
677 except AttributeError, e:
678 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
679 else:
680 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
681
682 # "The specifier name is a ``dotted name'' that may resolve either to
683 # a module, a test case class, a TestSuite instance, a test method
684 # within a test case class, or a callable object which returns a
685 # TestCase or TestSuite instance."
686 # ...
687 # "The method optionally resolves name relative to the given module"
688 #
689 # What happens when faced with the empty string?
690 #
691 # XXX This currently raises AttributeError, though ValueError is probably
692 # more appropriate
693 def test_loadTestsFromNames__relative_empty_name(self):
694 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000695
Georg Brandl15c5ce92007-03-07 09:09:40 +0000696 try:
697 loader.loadTestsFromNames([''], unittest)
698 except AttributeError:
699 pass
700 else:
701 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000702
Georg Brandl15c5ce92007-03-07 09:09:40 +0000703 # "The specifier name is a ``dotted name'' that may resolve either to
704 # a module, a test case class, a TestSuite instance, a test method
705 # within a test case class, or a callable object which returns a
706 # TestCase or TestSuite instance."
707 # ...
708 # "The method optionally resolves name relative to the given module"
709 #
Tim Petersea5962f2007-03-12 18:07:52 +0000710 # What happens when presented with an impossible attribute name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000711 def test_loadTestsFromNames__relative_malformed_name(self):
712 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000713
Georg Brandl15c5ce92007-03-07 09:09:40 +0000714 # XXX Should this raise AttributeError or ValueError?
715 try:
716 loader.loadTestsFromNames(['abc () //'], unittest)
717 except AttributeError:
718 pass
719 except ValueError:
720 pass
721 else:
722 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
723
724 # "The method optionally resolves name relative to the given module"
725 #
726 # Does loadTestsFromNames() make sure the provided `module` is in fact
727 # a module?
728 #
729 # XXX This validation is currently not done. This flexibility should
730 # either be documented or a TypeError should be raised.
731 def test_loadTestsFromNames__relative_not_a_module(self):
732 class MyTestCase(unittest.TestCase):
733 def test(self):
734 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000735
Georg Brandl15c5ce92007-03-07 09:09:40 +0000736 class NotAModule(object):
737 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000738
Georg Brandl15c5ce92007-03-07 09:09:40 +0000739 loader = unittest.TestLoader()
740 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000741
Georg Brandl15c5ce92007-03-07 09:09:40 +0000742 reference = [unittest.TestSuite([MyTestCase('test')])]
743 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000744
Georg Brandl15c5ce92007-03-07 09:09:40 +0000745 # "The specifier name is a ``dotted name'' that may resolve either to
746 # a module, a test case class, a TestSuite instance, a test method
747 # within a test case class, or a callable object which returns a
748 # TestCase or TestSuite instance."
749 #
750 # Does it raise an exception if the name resolves to an invalid
751 # object?
752 def test_loadTestsFromNames__relative_bad_object(self):
753 import new
754 m = new.module('m')
755 m.testcase_1 = object()
Tim Petersea5962f2007-03-12 18:07:52 +0000756
Georg Brandl15c5ce92007-03-07 09:09:40 +0000757 loader = unittest.TestLoader()
758 try:
759 loader.loadTestsFromNames(['testcase_1'], m)
760 except TypeError:
761 pass
762 else:
763 self.fail("Should have raised TypeError")
764
765 # "The specifier name is a ``dotted name'' that may resolve ... to
766 # ... a test case class"
767 def test_loadTestsFromNames__relative_TestCase_subclass(self):
768 import new
769 m = new.module('m')
770 class MyTestCase(unittest.TestCase):
771 def test(self):
772 pass
773 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000774
Georg Brandl15c5ce92007-03-07 09:09:40 +0000775 loader = unittest.TestLoader()
776 suite = loader.loadTestsFromNames(['testcase_1'], m)
777 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000778
Georg Brandl15c5ce92007-03-07 09:09:40 +0000779 expected = loader.suiteClass([MyTestCase('test')])
780 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000781
Georg Brandl15c5ce92007-03-07 09:09:40 +0000782 # "The specifier name is a ``dotted name'' that may resolve ... to
783 # ... a TestSuite instance"
784 def test_loadTestsFromNames__relative_TestSuite(self):
785 import new
786 m = new.module('m')
787 class MyTestCase(unittest.TestCase):
788 def test(self):
789 pass
790 m.testsuite = unittest.TestSuite([MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000791
Georg Brandl15c5ce92007-03-07 09:09:40 +0000792 loader = unittest.TestLoader()
793 suite = loader.loadTestsFromNames(['testsuite'], m)
794 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000795
Georg Brandl15c5ce92007-03-07 09:09:40 +0000796 self.assertEqual(list(suite), [m.testsuite])
Tim Petersea5962f2007-03-12 18:07:52 +0000797
Georg Brandl15c5ce92007-03-07 09:09:40 +0000798 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
799 # test method within a test case class"
800 def test_loadTestsFromNames__relative_testmethod(self):
801 import new
802 m = new.module('m')
803 class MyTestCase(unittest.TestCase):
804 def test(self):
805 pass
806 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000807
Georg Brandl15c5ce92007-03-07 09:09:40 +0000808 loader = unittest.TestLoader()
809 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
810 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000811
Georg Brandl15c5ce92007-03-07 09:09:40 +0000812 ref_suite = unittest.TestSuite([MyTestCase('test')])
813 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000814
Georg Brandl15c5ce92007-03-07 09:09:40 +0000815 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
816 # test method within a test case class"
817 #
818 # Does the method gracefully handle names that initially look like they
Tim Petersea5962f2007-03-12 18:07:52 +0000819 # resolve to "a test method within a test case class" but don't?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000820 def test_loadTestsFromNames__relative_invalid_testmethod(self):
821 import new
822 m = new.module('m')
823 class MyTestCase(unittest.TestCase):
824 def test(self):
825 pass
826 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000827
Georg Brandl15c5ce92007-03-07 09:09:40 +0000828 loader = unittest.TestLoader()
829 try:
830 loader.loadTestsFromNames(['testcase_1.testfoo'], m)
831 except AttributeError, e:
832 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
833 else:
834 self.fail("Failed to raise AttributeError")
835
836 # "The specifier name is a ``dotted name'' that may resolve ... to
837 # ... a callable object which returns a ... TestSuite instance"
838 def test_loadTestsFromNames__callable__TestSuite(self):
839 import new
840 m = new.module('m')
841 testcase_1 = unittest.FunctionTestCase(lambda: None)
842 testcase_2 = unittest.FunctionTestCase(lambda: None)
843 def return_TestSuite():
844 return unittest.TestSuite([testcase_1, testcase_2])
845 m.return_TestSuite = return_TestSuite
Tim Petersea5962f2007-03-12 18:07:52 +0000846
Georg Brandl15c5ce92007-03-07 09:09:40 +0000847 loader = unittest.TestLoader()
848 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
849 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000850
Georg Brandl15c5ce92007-03-07 09:09:40 +0000851 expected = unittest.TestSuite([testcase_1, testcase_2])
852 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000853
Georg Brandl15c5ce92007-03-07 09:09:40 +0000854 # "The specifier name is a ``dotted name'' that may resolve ... to
855 # ... a callable object which returns a TestCase ... instance"
856 def test_loadTestsFromNames__callable__TestCase_instance(self):
857 import new
858 m = new.module('m')
859 testcase_1 = unittest.FunctionTestCase(lambda: None)
860 def return_TestCase():
861 return testcase_1
862 m.return_TestCase = return_TestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000863
Georg Brandl15c5ce92007-03-07 09:09:40 +0000864 loader = unittest.TestLoader()
865 suite = loader.loadTestsFromNames(['return_TestCase'], m)
866 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000867
Georg Brandl15c5ce92007-03-07 09:09:40 +0000868 ref_suite = unittest.TestSuite([testcase_1])
869 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000870
Georg Brandl15c5ce92007-03-07 09:09:40 +0000871 # "The specifier name is a ``dotted name'' that may resolve ... to
872 # ... a callable object which returns a TestCase or TestSuite instance"
873 #
Tim Petersea5962f2007-03-12 18:07:52 +0000874 # Are staticmethods handled correctly?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000875 def test_loadTestsFromNames__callable__call_staticmethod(self):
876 import new
877 m = new.module('m')
878 class Test1(unittest.TestCase):
879 def test(self):
880 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000881
Georg Brandl15c5ce92007-03-07 09:09:40 +0000882 testcase_1 = Test1('test')
883 class Foo(unittest.TestCase):
884 @staticmethod
885 def foo():
886 return testcase_1
887 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +0000888
Georg Brandl15c5ce92007-03-07 09:09:40 +0000889 loader = unittest.TestLoader()
890 suite = loader.loadTestsFromNames(['Foo.foo'], m)
891 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000892
Georg Brandl15c5ce92007-03-07 09:09:40 +0000893 ref_suite = unittest.TestSuite([testcase_1])
894 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000895
Georg Brandl15c5ce92007-03-07 09:09:40 +0000896 # "The specifier name is a ``dotted name'' that may resolve ... to
897 # ... a callable object which returns a TestCase or TestSuite instance"
898 #
899 # What happens when the callable returns something else?
900 def test_loadTestsFromNames__callable__wrong_type(self):
901 import new
902 m = new.module('m')
903 def return_wrong():
904 return 6
905 m.return_wrong = return_wrong
Tim Petersea5962f2007-03-12 18:07:52 +0000906
Georg Brandl15c5ce92007-03-07 09:09:40 +0000907 loader = unittest.TestLoader()
908 try:
909 suite = loader.loadTestsFromNames(['return_wrong'], m)
910 except TypeError:
911 pass
912 else:
913 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000914
Georg Brandl15c5ce92007-03-07 09:09:40 +0000915 # "The specifier can refer to modules and packages which have not been
Tim Petersea5962f2007-03-12 18:07:52 +0000916 # imported; they will be imported as a side-effect"
Georg Brandl15c5ce92007-03-07 09:09:40 +0000917 def test_loadTestsFromNames__module_not_loaded(self):
918 # We're going to try to load this module as a side-effect, so it
919 # better not be loaded before we try.
920 #
921 # Why pick audioop? Google shows it isn't used very often, so there's
922 # a good chance that it won't be imported when this test is run
923 module_name = 'audioop'
Tim Petersea5962f2007-03-12 18:07:52 +0000924
Georg Brandl15c5ce92007-03-07 09:09:40 +0000925 import sys
926 if module_name in sys.modules:
927 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000928
Georg Brandl15c5ce92007-03-07 09:09:40 +0000929 loader = unittest.TestLoader()
930 try:
931 suite = loader.loadTestsFromNames([module_name])
932
933 self.failUnless(isinstance(suite, loader.suiteClass))
934 self.assertEqual(list(suite), [unittest.TestSuite()])
935
936 # audioop should now be loaded, thanks to loadTestsFromName()
937 self.failUnless(module_name in sys.modules)
938 finally:
939 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000940
Georg Brandl15c5ce92007-03-07 09:09:40 +0000941 ################################################################
942 ### /Tests for TestLoader.loadTestsFromNames()
943
944 ### Tests for TestLoader.getTestCaseNames()
945 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000946
Georg Brandl15c5ce92007-03-07 09:09:40 +0000947 # "Return a sorted sequence of method names found within testCaseClass"
948 #
949 # Test.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +0000950 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +0000951 def test_getTestCaseNames(self):
952 class Test(unittest.TestCase):
953 def test_1(self): pass
954 def test_2(self): pass
955 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000956
Georg Brandl15c5ce92007-03-07 09:09:40 +0000957 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000958
Georg Brandl15c5ce92007-03-07 09:09:40 +0000959 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
Tim Petersea5962f2007-03-12 18:07:52 +0000960
Georg Brandl15c5ce92007-03-07 09:09:40 +0000961 # "Return a sorted sequence of method names found within testCaseClass"
962 #
Tim Petersea5962f2007-03-12 18:07:52 +0000963 # Does getTestCaseNames() behave appropriately if no tests are found?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000964 def test_getTestCaseNames__no_tests(self):
965 class Test(unittest.TestCase):
966 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000967
Georg Brandl15c5ce92007-03-07 09:09:40 +0000968 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000969
Georg Brandl15c5ce92007-03-07 09:09:40 +0000970 self.assertEqual(loader.getTestCaseNames(Test), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000971
Georg Brandl15c5ce92007-03-07 09:09:40 +0000972 # "Return a sorted sequence of method names found within testCaseClass"
973 #
974 # Are not-TestCases handled gracefully?
975 #
976 # XXX This should raise a TypeError, not return a list
977 #
978 # XXX It's too late in the 2.5 release cycle to fix this, but it should
979 # probably be revisited for 2.6
980 def test_getTestCaseNames__not_a_TestCase(self):
981 class BadCase(int):
982 def test_foo(self):
983 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000984
Georg Brandl15c5ce92007-03-07 09:09:40 +0000985 loader = unittest.TestLoader()
986 names = loader.getTestCaseNames(BadCase)
Tim Petersea5962f2007-03-12 18:07:52 +0000987
Georg Brandl15c5ce92007-03-07 09:09:40 +0000988 self.assertEqual(names, ['test_foo'])
Tim Petersea5962f2007-03-12 18:07:52 +0000989
Georg Brandl15c5ce92007-03-07 09:09:40 +0000990 # "Return a sorted sequence of method names found within testCaseClass"
991 #
992 # Make sure inherited names are handled.
993 #
994 # TestP.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +0000995 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +0000996 def test_getTestCaseNames__inheritance(self):
997 class TestP(unittest.TestCase):
998 def test_1(self): pass
999 def test_2(self): pass
1000 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001001
Georg Brandl15c5ce92007-03-07 09:09:40 +00001002 class TestC(TestP):
1003 def test_1(self): pass
1004 def test_3(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001005
Georg Brandl15c5ce92007-03-07 09:09:40 +00001006 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +00001007
Georg Brandl15c5ce92007-03-07 09:09:40 +00001008 names = ['test_1', 'test_2', 'test_3']
1009 self.assertEqual(loader.getTestCaseNames(TestC), names)
Tim Petersea5962f2007-03-12 18:07:52 +00001010
1011 ################################################################
Georg Brandl15c5ce92007-03-07 09:09:40 +00001012 ### /Tests for TestLoader.getTestCaseNames()
1013
1014 ### Tests for TestLoader.testMethodPrefix
1015 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001016
Georg Brandl15c5ce92007-03-07 09:09:40 +00001017 # "String giving the prefix of method names which will be interpreted as
1018 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001019 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001020 # Implicit in the documentation is that testMethodPrefix is respected by
1021 # all loadTestsFrom* methods.
1022 def test_testMethodPrefix__loadTestsFromTestCase(self):
1023 class Foo(unittest.TestCase):
1024 def test_1(self): pass
1025 def test_2(self): pass
1026 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001027
Georg Brandl15c5ce92007-03-07 09:09:40 +00001028 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1029 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001030
Georg Brandl15c5ce92007-03-07 09:09:40 +00001031 loader = unittest.TestLoader()
1032 loader.testMethodPrefix = 'foo'
1033 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1034
1035 loader.testMethodPrefix = 'test'
1036 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001037
Georg Brandl15c5ce92007-03-07 09:09:40 +00001038 # "String giving the prefix of method names which will be interpreted as
1039 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001040 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001041 # Implicit in the documentation is that testMethodPrefix is respected by
1042 # all loadTestsFrom* methods.
1043 def test_testMethodPrefix__loadTestsFromModule(self):
1044 import new
1045 m = new.module('m')
1046 class Foo(unittest.TestCase):
1047 def test_1(self): pass
1048 def test_2(self): pass
1049 def foo_bar(self): pass
1050 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001051
Georg Brandl15c5ce92007-03-07 09:09:40 +00001052 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
1053 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
Tim Petersea5962f2007-03-12 18:07:52 +00001054
Georg Brandl15c5ce92007-03-07 09:09:40 +00001055 loader = unittest.TestLoader()
1056 loader.testMethodPrefix = 'foo'
1057 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1058
1059 loader.testMethodPrefix = 'test'
1060 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001061
Georg Brandl15c5ce92007-03-07 09:09:40 +00001062 # "String giving the prefix of method names which will be interpreted as
1063 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001064 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001065 # Implicit in the documentation is that testMethodPrefix is respected by
1066 # all loadTestsFrom* methods.
1067 def test_testMethodPrefix__loadTestsFromName(self):
1068 import new
1069 m = new.module('m')
1070 class Foo(unittest.TestCase):
1071 def test_1(self): pass
1072 def test_2(self): pass
1073 def foo_bar(self): pass
1074 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001075
Georg Brandl15c5ce92007-03-07 09:09:40 +00001076 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1077 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001078
Georg Brandl15c5ce92007-03-07 09:09:40 +00001079 loader = unittest.TestLoader()
1080 loader.testMethodPrefix = 'foo'
1081 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1082
1083 loader.testMethodPrefix = 'test'
1084 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001085
Georg Brandl15c5ce92007-03-07 09:09:40 +00001086 # "String giving the prefix of method names which will be interpreted as
1087 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001088 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001089 # Implicit in the documentation is that testMethodPrefix is respected by
1090 # all loadTestsFrom* methods.
1091 def test_testMethodPrefix__loadTestsFromNames(self):
1092 import new
1093 m = new.module('m')
1094 class Foo(unittest.TestCase):
1095 def test_1(self): pass
1096 def test_2(self): pass
1097 def foo_bar(self): pass
1098 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001099
Georg Brandl15c5ce92007-03-07 09:09:40 +00001100 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
1101 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1102 tests_2 = unittest.TestSuite([tests_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001103
Georg Brandl15c5ce92007-03-07 09:09:40 +00001104 loader = unittest.TestLoader()
1105 loader.testMethodPrefix = 'foo'
1106 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1107
1108 loader.testMethodPrefix = 'test'
1109 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001110
Georg Brandl15c5ce92007-03-07 09:09:40 +00001111 # "The default value is 'test'"
1112 def test_testMethodPrefix__default_value(self):
1113 loader = unittest.TestLoader()
1114 self.failUnless(loader.testMethodPrefix == 'test')
Tim Petersea5962f2007-03-12 18:07:52 +00001115
Georg Brandl15c5ce92007-03-07 09:09:40 +00001116 ################################################################
1117 ### /Tests for TestLoader.testMethodPrefix
1118
Tim Petersea5962f2007-03-12 18:07:52 +00001119 ### Tests for TestLoader.sortTestMethodsUsing
Georg Brandl15c5ce92007-03-07 09:09:40 +00001120 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001121
Georg Brandl15c5ce92007-03-07 09:09:40 +00001122 # "Function to be used to compare method names when sorting them in
1123 # getTestCaseNames() and all the loadTestsFromX() methods"
1124 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1125 def reversed_cmp(x, y):
1126 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001127
Georg Brandl15c5ce92007-03-07 09:09:40 +00001128 class Foo(unittest.TestCase):
1129 def test_1(self): pass
1130 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001131
Georg Brandl15c5ce92007-03-07 09:09:40 +00001132 loader = unittest.TestLoader()
1133 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001134
Georg Brandl15c5ce92007-03-07 09:09:40 +00001135 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1136 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001137
Georg Brandl15c5ce92007-03-07 09:09:40 +00001138 # "Function to be used to compare method names when sorting them in
1139 # getTestCaseNames() and all the loadTestsFromX() methods"
1140 def test_sortTestMethodsUsing__loadTestsFromModule(self):
1141 def reversed_cmp(x, y):
1142 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001143
Georg Brandl15c5ce92007-03-07 09:09:40 +00001144 import new
Tim Petersea5962f2007-03-12 18:07:52 +00001145 m = new.module('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001146 class Foo(unittest.TestCase):
1147 def test_1(self): pass
1148 def test_2(self): pass
1149 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001150
Georg Brandl15c5ce92007-03-07 09:09:40 +00001151 loader = unittest.TestLoader()
1152 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001153
Georg Brandl15c5ce92007-03-07 09:09:40 +00001154 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1155 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001156
Georg Brandl15c5ce92007-03-07 09:09:40 +00001157 # "Function to be used to compare method names when sorting them in
1158 # getTestCaseNames() and all the loadTestsFromX() methods"
1159 def test_sortTestMethodsUsing__loadTestsFromName(self):
1160 def reversed_cmp(x, y):
1161 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001162
Georg Brandl15c5ce92007-03-07 09:09:40 +00001163 import new
Tim Petersea5962f2007-03-12 18:07:52 +00001164 m = new.module('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001165 class Foo(unittest.TestCase):
1166 def test_1(self): pass
1167 def test_2(self): pass
1168 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001169
Georg Brandl15c5ce92007-03-07 09:09:40 +00001170 loader = unittest.TestLoader()
1171 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001172
Georg Brandl15c5ce92007-03-07 09:09:40 +00001173 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1174 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001175
Georg Brandl15c5ce92007-03-07 09:09:40 +00001176 # "Function to be used to compare method names when sorting them in
1177 # getTestCaseNames() and all the loadTestsFromX() methods"
1178 def test_sortTestMethodsUsing__loadTestsFromNames(self):
1179 def reversed_cmp(x, y):
1180 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001181
Georg Brandl15c5ce92007-03-07 09:09:40 +00001182 import new
Tim Petersea5962f2007-03-12 18:07:52 +00001183 m = new.module('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001184 class Foo(unittest.TestCase):
1185 def test_1(self): pass
1186 def test_2(self): pass
1187 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001188
Georg Brandl15c5ce92007-03-07 09:09:40 +00001189 loader = unittest.TestLoader()
1190 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001191
Georg Brandl15c5ce92007-03-07 09:09:40 +00001192 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1193 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001194
Georg Brandl15c5ce92007-03-07 09:09:40 +00001195 # "Function to be used to compare method names when sorting them in
1196 # getTestCaseNames()"
1197 #
1198 # Does it actually affect getTestCaseNames()?
1199 def test_sortTestMethodsUsing__getTestCaseNames(self):
1200 def reversed_cmp(x, y):
1201 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001202
Georg Brandl15c5ce92007-03-07 09:09:40 +00001203 class Foo(unittest.TestCase):
1204 def test_1(self): pass
1205 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001206
Georg Brandl15c5ce92007-03-07 09:09:40 +00001207 loader = unittest.TestLoader()
1208 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001209
Georg Brandl15c5ce92007-03-07 09:09:40 +00001210 test_names = ['test_2', 'test_1']
1211 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
Tim Petersea5962f2007-03-12 18:07:52 +00001212
Georg Brandl15c5ce92007-03-07 09:09:40 +00001213 # "The default value is the built-in cmp() function"
1214 def test_sortTestMethodsUsing__default_value(self):
1215 loader = unittest.TestLoader()
1216 self.failUnless(loader.sortTestMethodsUsing is cmp)
Tim Petersea5962f2007-03-12 18:07:52 +00001217
Georg Brandl15c5ce92007-03-07 09:09:40 +00001218 # "it can be set to None to disable the sort."
1219 #
1220 # XXX How is this different from reassigning cmp? Are the tests returned
1221 # in a random order or something? This behaviour should die
1222 def test_sortTestMethodsUsing__None(self):
1223 class Foo(unittest.TestCase):
1224 def test_1(self): pass
1225 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001226
Georg Brandl15c5ce92007-03-07 09:09:40 +00001227 loader = unittest.TestLoader()
1228 loader.sortTestMethodsUsing = None
Tim Petersea5962f2007-03-12 18:07:52 +00001229
Georg Brandl15c5ce92007-03-07 09:09:40 +00001230 test_names = ['test_2', 'test_1']
1231 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
Tim Petersea5962f2007-03-12 18:07:52 +00001232
Georg Brandl15c5ce92007-03-07 09:09:40 +00001233 ################################################################
1234 ### /Tests for TestLoader.sortTestMethodsUsing
Tim Petersea5962f2007-03-12 18:07:52 +00001235
Georg Brandl15c5ce92007-03-07 09:09:40 +00001236 ### Tests for TestLoader.suiteClass
1237 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001238
Georg Brandl15c5ce92007-03-07 09:09:40 +00001239 # "Callable object that constructs a test suite from a list of tests."
1240 def test_suiteClass__loadTestsFromTestCase(self):
1241 class Foo(unittest.TestCase):
1242 def test_1(self): pass
1243 def test_2(self): pass
1244 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001245
Georg Brandl15c5ce92007-03-07 09:09:40 +00001246 tests = [Foo('test_1'), Foo('test_2')]
1247
1248 loader = unittest.TestLoader()
1249 loader.suiteClass = list
1250 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001251
Georg Brandl15c5ce92007-03-07 09:09:40 +00001252 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001253 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001254 def test_suiteClass__loadTestsFromModule(self):
1255 import new
1256 m = new.module('m')
1257 class Foo(unittest.TestCase):
1258 def test_1(self): pass
1259 def test_2(self): pass
1260 def foo_bar(self): pass
1261 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001262
Georg Brandl15c5ce92007-03-07 09:09:40 +00001263 tests = [[Foo('test_1'), Foo('test_2')]]
1264
1265 loader = unittest.TestLoader()
1266 loader.suiteClass = list
1267 self.assertEqual(loader.loadTestsFromModule(m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001268
Georg Brandl15c5ce92007-03-07 09:09:40 +00001269 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001270 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001271 def test_suiteClass__loadTestsFromName(self):
1272 import new
1273 m = new.module('m')
1274 class Foo(unittest.TestCase):
1275 def test_1(self): pass
1276 def test_2(self): pass
1277 def foo_bar(self): pass
1278 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001279
Georg Brandl15c5ce92007-03-07 09:09:40 +00001280 tests = [Foo('test_1'), Foo('test_2')]
1281
1282 loader = unittest.TestLoader()
1283 loader.suiteClass = list
1284 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001285
Georg Brandl15c5ce92007-03-07 09:09:40 +00001286 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001287 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001288 def test_suiteClass__loadTestsFromNames(self):
1289 import new
1290 m = new.module('m')
1291 class Foo(unittest.TestCase):
1292 def test_1(self): pass
1293 def test_2(self): pass
1294 def foo_bar(self): pass
1295 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001296
Georg Brandl15c5ce92007-03-07 09:09:40 +00001297 tests = [[Foo('test_1'), Foo('test_2')]]
1298
1299 loader = unittest.TestLoader()
1300 loader.suiteClass = list
1301 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001302
Georg Brandl15c5ce92007-03-07 09:09:40 +00001303 # "The default value is the TestSuite class"
1304 def test_suiteClass__default_value(self):
1305 loader = unittest.TestLoader()
1306 self.failUnless(loader.suiteClass is unittest.TestSuite)
Tim Petersea5962f2007-03-12 18:07:52 +00001307
Georg Brandl15c5ce92007-03-07 09:09:40 +00001308 ################################################################
1309 ### /Tests for TestLoader.suiteClass
1310
1311### Support code for Test_TestSuite
1312################################################################
1313
1314class Foo(unittest.TestCase):
1315 def test_1(self): pass
1316 def test_2(self): pass
1317 def test_3(self): pass
1318 def runTest(self): pass
1319
1320def _mk_TestSuite(*names):
1321 return unittest.TestSuite(Foo(n) for n in names)
Tim Petersea5962f2007-03-12 18:07:52 +00001322
Georg Brandl15c5ce92007-03-07 09:09:40 +00001323################################################################
1324### /Support code for Test_TestSuite
1325
1326class Test_TestSuite(TestCase, TestEquality):
1327
1328 ### Set up attributes needed by inherited tests
1329 ################################################################
1330
1331 # Used by TestEquality.test_eq
1332 eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
1333 ,(unittest.TestSuite(), unittest.TestSuite([]))
1334 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001335
1336 # Used by TestEquality.test_ne
Georg Brandl15c5ce92007-03-07 09:09:40 +00001337 ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
1338 ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
1339 ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
1340 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001341
Georg Brandl15c5ce92007-03-07 09:09:40 +00001342 ################################################################
1343 ### /Set up attributes needed by inherited tests
1344
1345 ### Tests for TestSuite.__init__
1346 ################################################################
1347
1348 # "class TestSuite([tests])"
1349 #
1350 # The tests iterable should be optional
1351 def test_init__tests_optional(self):
1352 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001353
Georg Brandl15c5ce92007-03-07 09:09:40 +00001354 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001355
Georg Brandl15c5ce92007-03-07 09:09:40 +00001356 # "class TestSuite([tests])"
1357 # ...
1358 # "If tests is given, it must be an iterable of individual test cases
1359 # or other test suites that will be used to build the suite initially"
1360 #
1361 # TestSuite should deal with empty tests iterables by allowing the
Tim Petersea5962f2007-03-12 18:07:52 +00001362 # creation of an empty suite
Georg Brandl15c5ce92007-03-07 09:09:40 +00001363 def test_init__empty_tests(self):
1364 suite = unittest.TestSuite([])
Tim Petersea5962f2007-03-12 18:07:52 +00001365
Georg Brandl15c5ce92007-03-07 09:09:40 +00001366 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001367
Georg Brandl15c5ce92007-03-07 09:09:40 +00001368 # "class TestSuite([tests])"
1369 # ...
1370 # "If tests is given, it must be an iterable of individual test cases
1371 # or other test suites that will be used to build the suite initially"
1372 #
Tim Petersea5962f2007-03-12 18:07:52 +00001373 # TestSuite should allow any iterable to provide tests
Georg Brandl15c5ce92007-03-07 09:09:40 +00001374 def test_init__tests_from_any_iterable(self):
1375 def tests():
1376 yield unittest.FunctionTestCase(lambda: None)
1377 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001378
Georg Brandl15c5ce92007-03-07 09:09:40 +00001379 suite_1 = unittest.TestSuite(tests())
1380 self.assertEqual(suite_1.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001381
Georg Brandl15c5ce92007-03-07 09:09:40 +00001382 suite_2 = unittest.TestSuite(suite_1)
1383 self.assertEqual(suite_2.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001384
Georg Brandl15c5ce92007-03-07 09:09:40 +00001385 suite_3 = unittest.TestSuite(set(suite_1))
1386 self.assertEqual(suite_3.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001387
Georg Brandl15c5ce92007-03-07 09:09:40 +00001388 # "class TestSuite([tests])"
1389 # ...
1390 # "If tests is given, it must be an iterable of individual test cases
1391 # or other test suites that will be used to build the suite initially"
1392 #
1393 # Does TestSuite() also allow other TestSuite() instances to be present
1394 # in the tests iterable?
1395 def test_init__TestSuite_instances_in_tests(self):
1396 def tests():
1397 ftc = unittest.FunctionTestCase(lambda: None)
1398 yield unittest.TestSuite([ftc])
1399 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001400
Georg Brandl15c5ce92007-03-07 09:09:40 +00001401 suite = unittest.TestSuite(tests())
1402 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001403
Georg Brandl15c5ce92007-03-07 09:09:40 +00001404 ################################################################
1405 ### /Tests for TestSuite.__init__
1406
1407 # Container types should support the iter protocol
1408 def test_iter(self):
1409 test1 = unittest.FunctionTestCase(lambda: None)
1410 test2 = unittest.FunctionTestCase(lambda: None)
1411 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001412
Georg Brandl15c5ce92007-03-07 09:09:40 +00001413 self.assertEqual(list(suite), [test1, test2])
Tim Petersea5962f2007-03-12 18:07:52 +00001414
Georg Brandl15c5ce92007-03-07 09:09:40 +00001415 # "Return the number of tests represented by the this test object.
1416 # ...this method is also implemented by the TestSuite class, which can
1417 # return larger [greater than 1] values"
1418 #
Tim Petersea5962f2007-03-12 18:07:52 +00001419 # Presumably an empty TestSuite returns 0?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001420 def test_countTestCases_zero_simple(self):
1421 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001422
Georg Brandl15c5ce92007-03-07 09:09:40 +00001423 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001424
Georg Brandl15c5ce92007-03-07 09:09:40 +00001425 # "Return the number of tests represented by the this test object.
1426 # ...this method is also implemented by the TestSuite class, which can
1427 # return larger [greater than 1] values"
1428 #
1429 # Presumably an empty TestSuite (even if it contains other empty
1430 # TestSuite instances) returns 0?
1431 def test_countTestCases_zero_nested(self):
1432 class Test1(unittest.TestCase):
1433 def test(self):
1434 pass
1435
1436 suite = unittest.TestSuite([unittest.TestSuite()])
Tim Petersea5962f2007-03-12 18:07:52 +00001437
Georg Brandl15c5ce92007-03-07 09:09:40 +00001438 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001439
Georg Brandl15c5ce92007-03-07 09:09:40 +00001440 # "Return the number of tests represented by the this test object.
1441 # ...this method is also implemented by the TestSuite class, which can
1442 # return larger [greater than 1] values"
1443 def test_countTestCases_simple(self):
1444 test1 = unittest.FunctionTestCase(lambda: None)
1445 test2 = unittest.FunctionTestCase(lambda: None)
1446 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001447
Georg Brandl15c5ce92007-03-07 09:09:40 +00001448 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001449
Georg Brandl15c5ce92007-03-07 09:09:40 +00001450 # "Return the number of tests represented by the this test object.
1451 # ...this method is also implemented by the TestSuite class, which can
1452 # return larger [greater than 1] values"
1453 #
1454 # Make sure this holds for nested TestSuite instances, too
1455 def test_countTestCases_nested(self):
1456 class Test1(unittest.TestCase):
1457 def test1(self): pass
1458 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001459
Georg Brandl15c5ce92007-03-07 09:09:40 +00001460 test2 = unittest.FunctionTestCase(lambda: None)
1461 test3 = unittest.FunctionTestCase(lambda: None)
1462 child = unittest.TestSuite((Test1('test2'), test2))
1463 parent = unittest.TestSuite((test3, child, Test1('test1')))
Tim Petersea5962f2007-03-12 18:07:52 +00001464
Georg Brandl15c5ce92007-03-07 09:09:40 +00001465 self.assertEqual(parent.countTestCases(), 4)
Tim Petersea5962f2007-03-12 18:07:52 +00001466
Georg Brandl15c5ce92007-03-07 09:09:40 +00001467 # "Run the tests associated with this suite, collecting the result into
1468 # the test result object passed as result."
1469 #
1470 # And if there are no tests? What then?
1471 def test_run__empty_suite(self):
1472 events = []
1473 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001474
Georg Brandl15c5ce92007-03-07 09:09:40 +00001475 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001476
Georg Brandl15c5ce92007-03-07 09:09:40 +00001477 suite.run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001478
Georg Brandl15c5ce92007-03-07 09:09:40 +00001479 self.assertEqual(events, [])
Tim Petersea5962f2007-03-12 18:07:52 +00001480
Georg Brandl15c5ce92007-03-07 09:09:40 +00001481 # "Note that unlike TestCase.run(), TestSuite.run() requires the
1482 # "result object to be passed in."
1483 def test_run__requires_result(self):
1484 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001485
Georg Brandl15c5ce92007-03-07 09:09:40 +00001486 try:
1487 suite.run()
1488 except TypeError:
1489 pass
1490 else:
1491 self.fail("Failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +00001492
Georg Brandl15c5ce92007-03-07 09:09:40 +00001493 # "Run the tests associated with this suite, collecting the result into
Tim Petersea5962f2007-03-12 18:07:52 +00001494 # the test result object passed as result."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001495 def test_run(self):
1496 events = []
1497 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001498
Georg Brandl15c5ce92007-03-07 09:09:40 +00001499 class LoggingCase(unittest.TestCase):
1500 def run(self, result):
1501 events.append('run %s' % self._testMethodName)
Tim Petersea5962f2007-03-12 18:07:52 +00001502
Georg Brandl15c5ce92007-03-07 09:09:40 +00001503 def test1(self): pass
1504 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001505
1506 tests = [LoggingCase('test1'), LoggingCase('test2')]
1507
Georg Brandl15c5ce92007-03-07 09:09:40 +00001508 unittest.TestSuite(tests).run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001509
Georg Brandl15c5ce92007-03-07 09:09:40 +00001510 self.assertEqual(events, ['run test1', 'run test2'])
Tim Petersea5962f2007-03-12 18:07:52 +00001511
1512 # "Add a TestCase ... to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001513 def test_addTest__TestCase(self):
1514 class Foo(unittest.TestCase):
1515 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001516
Georg Brandl15c5ce92007-03-07 09:09:40 +00001517 test = Foo('test')
1518 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001519
Georg Brandl15c5ce92007-03-07 09:09:40 +00001520 suite.addTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001521
Georg Brandl15c5ce92007-03-07 09:09:40 +00001522 self.assertEqual(suite.countTestCases(), 1)
1523 self.assertEqual(list(suite), [test])
Tim Petersea5962f2007-03-12 18:07:52 +00001524
1525 # "Add a ... TestSuite to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001526 def test_addTest__TestSuite(self):
1527 class Foo(unittest.TestCase):
1528 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001529
Georg Brandl15c5ce92007-03-07 09:09:40 +00001530 suite_2 = unittest.TestSuite([Foo('test')])
Tim Petersea5962f2007-03-12 18:07:52 +00001531
Georg Brandl15c5ce92007-03-07 09:09:40 +00001532 suite = unittest.TestSuite()
1533 suite.addTest(suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001534
Georg Brandl15c5ce92007-03-07 09:09:40 +00001535 self.assertEqual(suite.countTestCases(), 1)
1536 self.assertEqual(list(suite), [suite_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001537
Georg Brandl15c5ce92007-03-07 09:09:40 +00001538 # "Add all the tests from an iterable of TestCase and TestSuite
1539 # instances to this test suite."
Tim Petersea5962f2007-03-12 18:07:52 +00001540 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001541 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001542 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001543 def test_addTests(self):
1544 class Foo(unittest.TestCase):
1545 def test_1(self): pass
1546 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001547
Georg Brandl15c5ce92007-03-07 09:09:40 +00001548 test_1 = Foo('test_1')
1549 test_2 = Foo('test_2')
1550 inner_suite = unittest.TestSuite([test_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001551
Georg Brandl15c5ce92007-03-07 09:09:40 +00001552 def gen():
1553 yield test_1
1554 yield test_2
1555 yield inner_suite
Tim Petersea5962f2007-03-12 18:07:52 +00001556
Georg Brandl15c5ce92007-03-07 09:09:40 +00001557 suite_1 = unittest.TestSuite()
1558 suite_1.addTests(gen())
Tim Petersea5962f2007-03-12 18:07:52 +00001559
Georg Brandl15c5ce92007-03-07 09:09:40 +00001560 self.assertEqual(list(suite_1), list(gen()))
Tim Petersea5962f2007-03-12 18:07:52 +00001561
Georg Brandl15c5ce92007-03-07 09:09:40 +00001562 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001563 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001564 suite_2 = unittest.TestSuite()
1565 for t in gen():
1566 suite_2.addTest(t)
Tim Petersea5962f2007-03-12 18:07:52 +00001567
Georg Brandl15c5ce92007-03-07 09:09:40 +00001568 self.assertEqual(suite_1, suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001569
Georg Brandl15c5ce92007-03-07 09:09:40 +00001570 # "Add all the tests from an iterable of TestCase and TestSuite
1571 # instances to this test suite."
1572 #
Tim Petersea5962f2007-03-12 18:07:52 +00001573 # What happens if it doesn't get an iterable?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001574 def test_addTest__noniterable(self):
1575 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001576
Georg Brandl15c5ce92007-03-07 09:09:40 +00001577 try:
1578 suite.addTests(5)
1579 except TypeError:
1580 pass
1581 else:
1582 self.fail("Failed to raise TypeError")
Georg Brandld9e50262007-03-07 11:54:49 +00001583
1584 def test_addTest__noncallable(self):
1585 suite = unittest.TestSuite()
1586 self.assertRaises(TypeError, suite.addTest, 5)
1587
1588 def test_addTest__casesuiteclass(self):
1589 suite = unittest.TestSuite()
1590 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
1591 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
1592
1593 def test_addTests__string(self):
1594 suite = unittest.TestSuite()
1595 self.assertRaises(TypeError, suite.addTests, "foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001596
1597
Georg Brandl15c5ce92007-03-07 09:09:40 +00001598class Test_FunctionTestCase(TestCase):
Tim Petersea5962f2007-03-12 18:07:52 +00001599
Georg Brandl15c5ce92007-03-07 09:09:40 +00001600 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00001601 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001602 def test_countTestCases(self):
1603 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001604
Georg Brandl15c5ce92007-03-07 09:09:40 +00001605 self.assertEqual(test.countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00001606
Georg Brandl15c5ce92007-03-07 09:09:40 +00001607 # "When a setUp() method is defined, the test runner will run that method
1608 # prior to each test. Likewise, if a tearDown() method is defined, the
1609 # test runner will invoke that method after each test. In the example,
1610 # setUp() was used to create a fresh sequence for each test."
1611 #
1612 # Make sure the proper call order is maintained, even if setUp() raises
1613 # an exception.
1614 def test_run_call_order__error_in_setUp(self):
1615 events = []
1616 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001617
Georg Brandl15c5ce92007-03-07 09:09:40 +00001618 def setUp():
1619 events.append('setUp')
1620 raise RuntimeError('raised by setUp')
1621
1622 def test():
1623 events.append('test')
1624
1625 def tearDown():
1626 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001627
1628 expected = ['startTest', 'setUp', 'addError', 'stopTest']
Georg Brandl15c5ce92007-03-07 09:09:40 +00001629 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1630 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001631
Georg Brandl15c5ce92007-03-07 09:09:40 +00001632 # "When a setUp() method is defined, the test runner will run that method
1633 # prior to each test. Likewise, if a tearDown() method is defined, the
1634 # test runner will invoke that method after each test. In the example,
1635 # setUp() was used to create a fresh sequence for each test."
1636 #
1637 # Make sure the proper call order is maintained, even if the test raises
1638 # an error (as opposed to a failure).
1639 def test_run_call_order__error_in_test(self):
1640 events = []
1641 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001642
Georg Brandl15c5ce92007-03-07 09:09:40 +00001643 def setUp():
1644 events.append('setUp')
1645
1646 def test():
1647 events.append('test')
1648 raise RuntimeError('raised by test')
1649
1650 def tearDown():
1651 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001652
Georg Brandl15c5ce92007-03-07 09:09:40 +00001653 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
1654 'stopTest']
1655 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1656 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001657
Georg Brandl15c5ce92007-03-07 09:09:40 +00001658 # "When a setUp() method is defined, the test runner will run that method
1659 # prior to each test. Likewise, if a tearDown() method is defined, the
1660 # test runner will invoke that method after each test. In the example,
1661 # setUp() was used to create a fresh sequence for each test."
1662 #
1663 # Make sure the proper call order is maintained, even if the test signals
1664 # a failure (as opposed to an error).
1665 def test_run_call_order__failure_in_test(self):
1666 events = []
1667 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001668
Georg Brandl15c5ce92007-03-07 09:09:40 +00001669 def setUp():
1670 events.append('setUp')
1671
1672 def test():
1673 events.append('test')
1674 self.fail('raised by test')
1675
1676 def tearDown():
1677 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001678
Georg Brandl15c5ce92007-03-07 09:09:40 +00001679 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
1680 'stopTest']
1681 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1682 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001683
Georg Brandl15c5ce92007-03-07 09:09:40 +00001684 # "When a setUp() method is defined, the test runner will run that method
1685 # prior to each test. Likewise, if a tearDown() method is defined, the
1686 # test runner will invoke that method after each test. In the example,
1687 # setUp() was used to create a fresh sequence for each test."
1688 #
1689 # Make sure the proper call order is maintained, even if tearDown() raises
1690 # an exception.
1691 def test_run_call_order__error_in_tearDown(self):
1692 events = []
1693 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001694
Georg Brandl15c5ce92007-03-07 09:09:40 +00001695 def setUp():
1696 events.append('setUp')
1697
1698 def test():
1699 events.append('test')
1700
1701 def tearDown():
1702 events.append('tearDown')
1703 raise RuntimeError('raised by tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001704
Georg Brandl15c5ce92007-03-07 09:09:40 +00001705 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
1706 'stopTest']
1707 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1708 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001709
Georg Brandl15c5ce92007-03-07 09:09:40 +00001710 # "Return a string identifying the specific test case."
1711 #
1712 # Because of the vague nature of the docs, I'm not going to lock this
1713 # test down too much. Really all that can be asserted is that the id()
1714 # will be a string (either 8-byte or unicode -- again, because the docs
1715 # just say "string")
1716 def test_id(self):
1717 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001718
Georg Brandl15c5ce92007-03-07 09:09:40 +00001719 self.failUnless(isinstance(test.id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00001720
Georg Brandl15c5ce92007-03-07 09:09:40 +00001721 # "Returns a one-line description of the test, or None if no description
1722 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001723 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001724 def test_shortDescription__no_docstring(self):
1725 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001726
Georg Brandl15c5ce92007-03-07 09:09:40 +00001727 self.assertEqual(test.shortDescription(), None)
Tim Petersea5962f2007-03-12 18:07:52 +00001728
Georg Brandl15c5ce92007-03-07 09:09:40 +00001729 # "Returns a one-line description of the test, or None if no description
1730 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001731 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001732 def test_shortDescription__singleline_docstring(self):
1733 desc = "this tests foo"
1734 test = unittest.FunctionTestCase(lambda: None, description=desc)
Tim Petersea5962f2007-03-12 18:07:52 +00001735
Georg Brandl15c5ce92007-03-07 09:09:40 +00001736 self.assertEqual(test.shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001737
Georg Brandl15c5ce92007-03-07 09:09:40 +00001738class Test_TestResult(TestCase):
1739 # Note: there are not separate tests for TestResult.wasSuccessful(),
1740 # TestResult.errors, TestResult.failures, TestResult.testsRun or
1741 # TestResult.shouldStop because these only have meaning in terms of
1742 # other TestResult methods.
1743 #
1744 # Accordingly, tests for the aforenamed attributes are incorporated
1745 # in with the tests for the defining methods.
1746 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001747
Georg Brandl15c5ce92007-03-07 09:09:40 +00001748 def test_init(self):
1749 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001750
Georg Brandl15c5ce92007-03-07 09:09:40 +00001751 self.failUnless(result.wasSuccessful())
1752 self.assertEqual(len(result.errors), 0)
1753 self.assertEqual(len(result.failures), 0)
1754 self.assertEqual(result.testsRun, 0)
1755 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001756
Georg Brandl15c5ce92007-03-07 09:09:40 +00001757 # "This method can be called to signal that the set of tests being
1758 # run should be aborted by setting the TestResult's shouldStop
Tim Petersea5962f2007-03-12 18:07:52 +00001759 # attribute to True."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001760 def test_stop(self):
1761 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001762
Georg Brandl15c5ce92007-03-07 09:09:40 +00001763 result.stop()
Tim Petersea5962f2007-03-12 18:07:52 +00001764
Georg Brandl15c5ce92007-03-07 09:09:40 +00001765 self.assertEqual(result.shouldStop, True)
Tim Petersea5962f2007-03-12 18:07:52 +00001766
Georg Brandl15c5ce92007-03-07 09:09:40 +00001767 # "Called when the test case test is about to be run. The default
1768 # implementation simply increments the instance's testsRun counter."
1769 def test_startTest(self):
1770 class Foo(unittest.TestCase):
1771 def test_1(self):
1772 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001773
Georg Brandl15c5ce92007-03-07 09:09:40 +00001774 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001775
Georg Brandl15c5ce92007-03-07 09:09:40 +00001776 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001777
Georg Brandl15c5ce92007-03-07 09:09:40 +00001778 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001779
Georg Brandl15c5ce92007-03-07 09:09:40 +00001780 self.failUnless(result.wasSuccessful())
1781 self.assertEqual(len(result.errors), 0)
1782 self.assertEqual(len(result.failures), 0)
1783 self.assertEqual(result.testsRun, 1)
1784 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001785
Georg Brandl15c5ce92007-03-07 09:09:40 +00001786 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001787
Georg Brandl15c5ce92007-03-07 09:09:40 +00001788 # "Called after the test case test has been executed, regardless of
1789 # the outcome. The default implementation does nothing."
1790 def test_stopTest(self):
1791 class Foo(unittest.TestCase):
1792 def test_1(self):
1793 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001794
Georg Brandl15c5ce92007-03-07 09:09:40 +00001795 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001796
Georg Brandl15c5ce92007-03-07 09:09:40 +00001797 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001798
Georg Brandl15c5ce92007-03-07 09:09:40 +00001799 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001800
Georg Brandl15c5ce92007-03-07 09:09:40 +00001801 self.failUnless(result.wasSuccessful())
1802 self.assertEqual(len(result.errors), 0)
1803 self.assertEqual(len(result.failures), 0)
1804 self.assertEqual(result.testsRun, 1)
1805 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001806
Georg Brandl15c5ce92007-03-07 09:09:40 +00001807 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001808
Georg Brandl15c5ce92007-03-07 09:09:40 +00001809 # Same tests as above; make sure nothing has changed
1810 self.failUnless(result.wasSuccessful())
1811 self.assertEqual(len(result.errors), 0)
1812 self.assertEqual(len(result.failures), 0)
1813 self.assertEqual(result.testsRun, 1)
1814 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001815
Georg Brandl15c5ce92007-03-07 09:09:40 +00001816 # "addSuccess(test)"
1817 # ...
1818 # "Called when the test case test succeeds"
1819 # ...
1820 # "wasSuccessful() - Returns True if all tests run so far have passed,
1821 # otherwise returns False"
1822 # ...
1823 # "testsRun - The total number of tests run so far."
1824 # ...
1825 # "errors - A list containing 2-tuples of TestCase instances and
1826 # formatted tracebacks. Each tuple represents a test which raised an
1827 # unexpected exception. Contains formatted
1828 # tracebacks instead of sys.exc_info() results."
1829 # ...
1830 # "failures - A list containing 2-tuples of TestCase instances and
1831 # formatted tracebacks. Each tuple represents a test where a failure was
1832 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1833 # methods. Contains formatted tracebacks instead
1834 # of sys.exc_info() results."
1835 def test_addSuccess(self):
1836 class Foo(unittest.TestCase):
1837 def test_1(self):
1838 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001839
Georg Brandl15c5ce92007-03-07 09:09:40 +00001840 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001841
Georg Brandl15c5ce92007-03-07 09:09:40 +00001842 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001843
Georg Brandl15c5ce92007-03-07 09:09:40 +00001844 result.startTest(test)
1845 result.addSuccess(test)
1846 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001847
Georg Brandl15c5ce92007-03-07 09:09:40 +00001848 self.failUnless(result.wasSuccessful())
1849 self.assertEqual(len(result.errors), 0)
1850 self.assertEqual(len(result.failures), 0)
1851 self.assertEqual(result.testsRun, 1)
1852 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001853
Georg Brandl15c5ce92007-03-07 09:09:40 +00001854 # "addFailure(test, err)"
1855 # ...
1856 # "Called when the test case test signals a failure. err is a tuple of
1857 # the form returned by sys.exc_info(): (type, value, traceback)"
1858 # ...
1859 # "wasSuccessful() - Returns True if all tests run so far have passed,
1860 # otherwise returns False"
1861 # ...
1862 # "testsRun - The total number of tests run so far."
1863 # ...
1864 # "errors - A list containing 2-tuples of TestCase instances and
1865 # formatted tracebacks. Each tuple represents a test which raised an
1866 # unexpected exception. Contains formatted
1867 # tracebacks instead of sys.exc_info() results."
1868 # ...
1869 # "failures - A list containing 2-tuples of TestCase instances and
1870 # formatted tracebacks. Each tuple represents a test where a failure was
1871 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1872 # methods. Contains formatted tracebacks instead
1873 # of sys.exc_info() results."
1874 def test_addFailure(self):
1875 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001876
Georg Brandl15c5ce92007-03-07 09:09:40 +00001877 class Foo(unittest.TestCase):
1878 def test_1(self):
1879 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001880
Georg Brandl15c5ce92007-03-07 09:09:40 +00001881 test = Foo('test_1')
1882 try:
1883 test.fail("foo")
1884 except:
1885 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001886
Georg Brandl15c5ce92007-03-07 09:09:40 +00001887 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001888
Georg Brandl15c5ce92007-03-07 09:09:40 +00001889 result.startTest(test)
1890 result.addFailure(test, exc_info_tuple)
1891 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001892
Georg Brandl15c5ce92007-03-07 09:09:40 +00001893 self.failIf(result.wasSuccessful())
1894 self.assertEqual(len(result.errors), 0)
1895 self.assertEqual(len(result.failures), 1)
1896 self.assertEqual(result.testsRun, 1)
1897 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001898
Georg Brandl15c5ce92007-03-07 09:09:40 +00001899 test_case, formatted_exc = result.failures[0]
1900 self.failUnless(test_case is test)
1901 self.failUnless(isinstance(formatted_exc, str))
Tim Petersea5962f2007-03-12 18:07:52 +00001902
Georg Brandl15c5ce92007-03-07 09:09:40 +00001903 # "addError(test, err)"
1904 # ...
1905 # "Called when the test case test raises an unexpected exception err
1906 # is a tuple of the form returned by sys.exc_info():
1907 # (type, value, traceback)"
1908 # ...
1909 # "wasSuccessful() - Returns True if all tests run so far have passed,
1910 # otherwise returns False"
1911 # ...
1912 # "testsRun - The total number of tests run so far."
1913 # ...
1914 # "errors - A list containing 2-tuples of TestCase instances and
1915 # formatted tracebacks. Each tuple represents a test which raised an
1916 # unexpected exception. Contains formatted
1917 # tracebacks instead of sys.exc_info() results."
1918 # ...
1919 # "failures - A list containing 2-tuples of TestCase instances and
1920 # formatted tracebacks. Each tuple represents a test where a failure was
1921 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1922 # methods. Contains formatted tracebacks instead
1923 # of sys.exc_info() results."
1924 def test_addError(self):
1925 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001926
Georg Brandl15c5ce92007-03-07 09:09:40 +00001927 class Foo(unittest.TestCase):
1928 def test_1(self):
1929 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001930
Georg Brandl15c5ce92007-03-07 09:09:40 +00001931 test = Foo('test_1')
1932 try:
1933 raise TypeError()
1934 except:
1935 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001936
Georg Brandl15c5ce92007-03-07 09:09:40 +00001937 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001938
Georg Brandl15c5ce92007-03-07 09:09:40 +00001939 result.startTest(test)
1940 result.addError(test, exc_info_tuple)
1941 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001942
Georg Brandl15c5ce92007-03-07 09:09:40 +00001943 self.failIf(result.wasSuccessful())
1944 self.assertEqual(len(result.errors), 1)
1945 self.assertEqual(len(result.failures), 0)
1946 self.assertEqual(result.testsRun, 1)
1947 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001948
Georg Brandl15c5ce92007-03-07 09:09:40 +00001949 test_case, formatted_exc = result.errors[0]
1950 self.failUnless(test_case is test)
1951 self.failUnless(isinstance(formatted_exc, str))
1952
1953### Support code for Test_TestCase
1954################################################################
1955
1956class Foo(unittest.TestCase):
1957 def runTest(self): pass
1958 def test1(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001959
Georg Brandl15c5ce92007-03-07 09:09:40 +00001960class Bar(Foo):
1961 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001962
Georg Brandl15c5ce92007-03-07 09:09:40 +00001963################################################################
1964### /Support code for Test_TestCase
1965
1966class Test_TestCase(TestCase, TestEquality, TestHashing):
1967
1968 ### Set up attributes used by inherited tests
1969 ################################################################
1970
1971 # Used by TestHashing.test_hash and TestEquality.test_eq
1972 eq_pairs = [(Foo('test1'), Foo('test1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001973
Georg Brandl15c5ce92007-03-07 09:09:40 +00001974 # Used by TestEquality.test_ne
1975 ne_pairs = [(Foo('test1'), Foo('runTest'))
1976 ,(Foo('test1'), Bar('test1'))
1977 ,(Foo('test1'), Bar('test2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001978
Georg Brandl15c5ce92007-03-07 09:09:40 +00001979 ################################################################
1980 ### /Set up attributes used by inherited tests
Tim Petersea5962f2007-03-12 18:07:52 +00001981
Georg Brandl15c5ce92007-03-07 09:09:40 +00001982
1983 # "class TestCase([methodName])"
1984 # ...
1985 # "Each instance of TestCase will run a single test method: the
1986 # method named methodName."
1987 # ...
1988 # "methodName defaults to "runTest"."
1989 #
1990 # Make sure it really is optional, and that it defaults to the proper
1991 # thing.
1992 def test_init__no_test_name(self):
1993 class Test(unittest.TestCase):
1994 def runTest(self): raise MyException()
1995 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001996
Georg Brandl15c5ce92007-03-07 09:09:40 +00001997 self.assertEqual(Test().id()[-13:], '.Test.runTest')
Tim Petersea5962f2007-03-12 18:07:52 +00001998
Georg Brandl15c5ce92007-03-07 09:09:40 +00001999 # "class TestCase([methodName])"
2000 # ...
2001 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00002002 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002003 def test_init__test_name__valid(self):
2004 class Test(unittest.TestCase):
2005 def runTest(self): raise MyException()
2006 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002007
Georg Brandl15c5ce92007-03-07 09:09:40 +00002008 self.assertEqual(Test('test').id()[-10:], '.Test.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002009
Georg Brandl15c5ce92007-03-07 09:09:40 +00002010 # "class TestCase([methodName])"
2011 # ...
2012 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00002013 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002014 def test_init__test_name__invalid(self):
2015 class Test(unittest.TestCase):
2016 def runTest(self): raise MyException()
2017 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002018
Georg Brandl15c5ce92007-03-07 09:09:40 +00002019 try:
2020 Test('testfoo')
2021 except ValueError:
2022 pass
2023 else:
2024 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +00002025
Georg Brandl15c5ce92007-03-07 09:09:40 +00002026 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00002027 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002028 def test_countTestCases(self):
2029 class Foo(unittest.TestCase):
2030 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002031
Georg Brandl15c5ce92007-03-07 09:09:40 +00002032 self.assertEqual(Foo('test').countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00002033
Georg Brandl15c5ce92007-03-07 09:09:40 +00002034 # "Return the default type of test result object to be used to run this
2035 # test. For TestCase instances, this will always be
2036 # unittest.TestResult; subclasses of TestCase should
2037 # override this as necessary."
2038 def test_defaultTestResult(self):
2039 class Foo(unittest.TestCase):
2040 def runTest(self):
2041 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002042
Georg Brandl15c5ce92007-03-07 09:09:40 +00002043 result = Foo().defaultTestResult()
2044 self.assertEqual(type(result), unittest.TestResult)
2045
2046 # "When a setUp() method is defined, the test runner will run that method
2047 # prior to each test. Likewise, if a tearDown() method is defined, the
2048 # test runner will invoke that method after each test. In the example,
2049 # setUp() was used to create a fresh sequence for each test."
2050 #
2051 # Make sure the proper call order is maintained, even if setUp() raises
2052 # an exception.
2053 def test_run_call_order__error_in_setUp(self):
2054 events = []
2055 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002056
Georg Brandl15c5ce92007-03-07 09:09:40 +00002057 class Foo(unittest.TestCase):
2058 def setUp(self):
2059 events.append('setUp')
2060 raise RuntimeError('raised by Foo.setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002061
Georg Brandl15c5ce92007-03-07 09:09:40 +00002062 def test(self):
2063 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002064
Georg Brandl15c5ce92007-03-07 09:09:40 +00002065 def tearDown(self):
2066 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002067
Georg Brandl15c5ce92007-03-07 09:09:40 +00002068 Foo('test').run(result)
2069 expected = ['startTest', 'setUp', 'addError', 'stopTest']
2070 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002071
Georg Brandl15c5ce92007-03-07 09:09:40 +00002072 # "When a setUp() method is defined, the test runner will run that method
2073 # prior to each test. Likewise, if a tearDown() method is defined, the
2074 # test runner will invoke that method after each test. In the example,
2075 # setUp() was used to create a fresh sequence for each test."
2076 #
2077 # Make sure the proper call order is maintained, even if the test raises
2078 # an error (as opposed to a failure).
2079 def test_run_call_order__error_in_test(self):
2080 events = []
2081 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002082
Georg Brandl15c5ce92007-03-07 09:09:40 +00002083 class Foo(unittest.TestCase):
2084 def setUp(self):
2085 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002086
Georg Brandl15c5ce92007-03-07 09:09:40 +00002087 def test(self):
2088 events.append('test')
2089 raise RuntimeError('raised by Foo.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002090
Georg Brandl15c5ce92007-03-07 09:09:40 +00002091 def tearDown(self):
2092 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002093
Georg Brandl15c5ce92007-03-07 09:09:40 +00002094 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
2095 'stopTest']
2096 Foo('test').run(result)
2097 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002098
Georg Brandl15c5ce92007-03-07 09:09:40 +00002099 # "When a setUp() method is defined, the test runner will run that method
2100 # prior to each test. Likewise, if a tearDown() method is defined, the
2101 # test runner will invoke that method after each test. In the example,
2102 # setUp() was used to create a fresh sequence for each test."
2103 #
2104 # Make sure the proper call order is maintained, even if the test signals
2105 # a failure (as opposed to an error).
2106 def test_run_call_order__failure_in_test(self):
2107 events = []
2108 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002109
Georg Brandl15c5ce92007-03-07 09:09:40 +00002110 class Foo(unittest.TestCase):
2111 def setUp(self):
2112 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002113
Georg Brandl15c5ce92007-03-07 09:09:40 +00002114 def test(self):
2115 events.append('test')
2116 self.fail('raised by Foo.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002117
Georg Brandl15c5ce92007-03-07 09:09:40 +00002118 def tearDown(self):
2119 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002120
Georg Brandl15c5ce92007-03-07 09:09:40 +00002121 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
2122 'stopTest']
2123 Foo('test').run(result)
2124 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002125
Georg Brandl15c5ce92007-03-07 09:09:40 +00002126 # "When a setUp() method is defined, the test runner will run that method
2127 # prior to each test. Likewise, if a tearDown() method is defined, the
2128 # test runner will invoke that method after each test. In the example,
2129 # setUp() was used to create a fresh sequence for each test."
2130 #
2131 # Make sure the proper call order is maintained, even if tearDown() raises
2132 # an exception.
2133 def test_run_call_order__error_in_tearDown(self):
2134 events = []
2135 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002136
Georg Brandl15c5ce92007-03-07 09:09:40 +00002137 class Foo(unittest.TestCase):
2138 def setUp(self):
2139 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002140
Georg Brandl15c5ce92007-03-07 09:09:40 +00002141 def test(self):
2142 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002143
Georg Brandl15c5ce92007-03-07 09:09:40 +00002144 def tearDown(self):
2145 events.append('tearDown')
2146 raise RuntimeError('raised by Foo.tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002147
Georg Brandl15c5ce92007-03-07 09:09:40 +00002148 Foo('test').run(result)
2149 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
2150 'stopTest']
2151 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002152
Georg Brandl15c5ce92007-03-07 09:09:40 +00002153 # "This class attribute gives the exception raised by the test() method.
2154 # If a test framework needs to use a specialized exception, possibly to
2155 # carry additional information, it must subclass this exception in
2156 # order to ``play fair'' with the framework. The initial value of this
2157 # attribute is AssertionError"
2158 def test_failureException__default(self):
2159 class Foo(unittest.TestCase):
2160 def test(self):
2161 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002162
Georg Brandl15c5ce92007-03-07 09:09:40 +00002163 self.failUnless(Foo('test').failureException is AssertionError)
Tim Petersea5962f2007-03-12 18:07:52 +00002164
Georg Brandl15c5ce92007-03-07 09:09:40 +00002165 # "This class attribute gives the exception raised by the test() method.
2166 # If a test framework needs to use a specialized exception, possibly to
2167 # carry additional information, it must subclass this exception in
2168 # order to ``play fair'' with the framework."
2169 #
2170 # Make sure TestCase.run() respects the designated failureException
2171 def test_failureException__subclassing__explicit_raise(self):
2172 events = []
2173 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002174
Georg Brandl15c5ce92007-03-07 09:09:40 +00002175 class Foo(unittest.TestCase):
2176 def test(self):
2177 raise RuntimeError()
Tim Petersea5962f2007-03-12 18:07:52 +00002178
Georg Brandl15c5ce92007-03-07 09:09:40 +00002179 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002180
Georg Brandl15c5ce92007-03-07 09:09:40 +00002181 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002182
2183
Georg Brandl15c5ce92007-03-07 09:09:40 +00002184 Foo('test').run(result)
2185 expected = ['startTest', 'addFailure', 'stopTest']
2186 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002187
Georg Brandl15c5ce92007-03-07 09:09:40 +00002188 # "This class attribute gives the exception raised by the test() method.
2189 # If a test framework needs to use a specialized exception, possibly to
2190 # carry additional information, it must subclass this exception in
2191 # order to ``play fair'' with the framework."
2192 #
2193 # Make sure TestCase.run() respects the designated failureException
2194 def test_failureException__subclassing__implicit_raise(self):
2195 events = []
2196 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002197
Georg Brandl15c5ce92007-03-07 09:09:40 +00002198 class Foo(unittest.TestCase):
2199 def test(self):
2200 self.fail("foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002201
Georg Brandl15c5ce92007-03-07 09:09:40 +00002202 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002203
Georg Brandl15c5ce92007-03-07 09:09:40 +00002204 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002205
2206
Georg Brandl15c5ce92007-03-07 09:09:40 +00002207 Foo('test').run(result)
2208 expected = ['startTest', 'addFailure', 'stopTest']
2209 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002210
2211 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002212 def test_setUp(self):
2213 class Foo(unittest.TestCase):
2214 def runTest(self):
2215 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002216
Georg Brandl15c5ce92007-03-07 09:09:40 +00002217 # ... and nothing should happen
2218 Foo().setUp()
Tim Petersea5962f2007-03-12 18:07:52 +00002219
2220 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002221 def test_tearDown(self):
2222 class Foo(unittest.TestCase):
2223 def runTest(self):
2224 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002225
Georg Brandl15c5ce92007-03-07 09:09:40 +00002226 # ... and nothing should happen
2227 Foo().tearDown()
Tim Petersea5962f2007-03-12 18:07:52 +00002228
Georg Brandl15c5ce92007-03-07 09:09:40 +00002229 # "Return a string identifying the specific test case."
2230 #
2231 # Because of the vague nature of the docs, I'm not going to lock this
2232 # test down too much. Really all that can be asserted is that the id()
2233 # will be a string (either 8-byte or unicode -- again, because the docs
2234 # just say "string")
2235 def test_id(self):
2236 class Foo(unittest.TestCase):
2237 def runTest(self):
2238 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002239
Georg Brandl15c5ce92007-03-07 09:09:40 +00002240 self.failUnless(isinstance(Foo().id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00002241
Georg Brandl15c5ce92007-03-07 09:09:40 +00002242 # "Returns a one-line description of the test, or None if no description
2243 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002244 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002245 def test_shortDescription__no_docstring(self):
2246 class Foo(unittest.TestCase):
2247 def runTest(self):
2248 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002249
Georg Brandl15c5ce92007-03-07 09:09:40 +00002250 self.assertEqual(Foo().shortDescription(), None)
Tim Petersea5962f2007-03-12 18:07:52 +00002251
Georg Brandl15c5ce92007-03-07 09:09:40 +00002252 # "Returns a one-line description of the test, or None if no description
2253 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002254 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002255 def test_shortDescription__singleline_docstring(self):
2256 class Foo(unittest.TestCase):
2257 def runTest(self):
2258 "this tests foo"
2259 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002260
Georg Brandl15c5ce92007-03-07 09:09:40 +00002261 self.assertEqual(Foo().shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002262
Georg Brandl15c5ce92007-03-07 09:09:40 +00002263 # "Returns a one-line description of the test, or None if no description
2264 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002265 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002266 def test_shortDescription__multiline_docstring(self):
2267 class Foo(unittest.TestCase):
2268 def runTest(self):
2269 """this tests foo
2270 blah, bar and baz are also tested"""
2271 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002272
Georg Brandl15c5ce92007-03-07 09:09:40 +00002273 self.assertEqual(Foo().shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002274
Georg Brandl15c5ce92007-03-07 09:09:40 +00002275 # "If result is omitted or None, a temporary result object is created
Tim Petersea5962f2007-03-12 18:07:52 +00002276 # and used, but is not made available to the caller"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002277 def test_run__uses_defaultTestResult(self):
2278 events = []
Tim Petersea5962f2007-03-12 18:07:52 +00002279
Georg Brandl15c5ce92007-03-07 09:09:40 +00002280 class Foo(unittest.TestCase):
2281 def test(self):
2282 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002283
Georg Brandl15c5ce92007-03-07 09:09:40 +00002284 def defaultTestResult(self):
2285 return LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002286
2287 # Make run() find a result object on its own
Georg Brandl15c5ce92007-03-07 09:09:40 +00002288 Foo('test').run()
Tim Petersea5962f2007-03-12 18:07:52 +00002289
Georg Brandl15c5ce92007-03-07 09:09:40 +00002290 expected = ['startTest', 'test', 'stopTest']
2291 self.assertEqual(events, expected)
Jim Fultonfafd8742004-08-28 15:22:12 +00002292
2293######################################################################
2294## Main
2295######################################################################
2296
2297def test_main():
Georg Brandl15c5ce92007-03-07 09:09:40 +00002298 test_support.run_unittest(Test_TestCase, Test_TestLoader,
2299 Test_TestSuite, Test_TestResult, Test_FunctionTestCase)
Jim Fultonfafd8742004-08-28 15:22:12 +00002300
Georg Brandl15c5ce92007-03-07 09:09:40 +00002301if __name__ == "__main__":
Tim Petersea5962f2007-03-12 18:07:52 +00002302 test_main()