blob: 8d5bb6366b7ca1fe2cc89c3dc786ad690c9ea989 [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:
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +0000543 if module_name in sys.modules:
544 del sys.modules[module_name]
Georg Brandl15c5ce92007-03-07 09:09:40 +0000545
546 ################################################################
547 ### Tests for TestLoader.loadTestsFromName()
548
549 ### Tests for TestLoader.loadTestsFromNames()
550 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000551
Georg Brandl15c5ce92007-03-07 09:09:40 +0000552 # "Similar to loadTestsFromName(), but takes a sequence of names rather
553 # than a single name."
554 #
555 # What happens if that sequence of names is empty?
556 def test_loadTestsFromNames__empty_name_list(self):
557 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000558
Georg Brandl15c5ce92007-03-07 09:09:40 +0000559 suite = loader.loadTestsFromNames([])
560 self.failUnless(isinstance(suite, loader.suiteClass))
561 self.assertEqual(list(suite), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000562
Georg Brandl15c5ce92007-03-07 09:09:40 +0000563 # "Similar to loadTestsFromName(), but takes a sequence of names rather
564 # than a single name."
565 # ...
566 # "The method optionally resolves name relative to the given module"
567 #
568 # What happens if that sequence of names is empty?
569 #
570 # XXX Should this raise a ValueError or just return an empty TestSuite?
571 def test_loadTestsFromNames__relative_empty_name_list(self):
572 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000573
Georg Brandl15c5ce92007-03-07 09:09:40 +0000574 suite = loader.loadTestsFromNames([], unittest)
575 self.failUnless(isinstance(suite, loader.suiteClass))
576 self.assertEqual(list(suite), [])
577
578 # "The specifier name is a ``dotted name'' that may resolve either to
579 # a module, a test case class, a TestSuite instance, a test method
580 # within a test case class, or a callable object which returns a
581 # TestCase or TestSuite instance."
582 #
583 # Is ValueError raised in response to an empty name?
584 def test_loadTestsFromNames__empty_name(self):
585 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000586
Georg Brandl15c5ce92007-03-07 09:09:40 +0000587 try:
588 loader.loadTestsFromNames([''])
589 except ValueError, e:
590 self.assertEqual(str(e), "Empty module name")
591 else:
592 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000593
Georg Brandl15c5ce92007-03-07 09:09:40 +0000594 # "The specifier name is a ``dotted name'' that may resolve either to
595 # a module, a test case class, a TestSuite instance, a test method
596 # within a test case class, or a callable object which returns a
597 # TestCase or TestSuite instance."
598 #
Tim Petersea5962f2007-03-12 18:07:52 +0000599 # What happens when presented with an impossible module name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000600 def test_loadTestsFromNames__malformed_name(self):
601 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000602
Georg Brandl15c5ce92007-03-07 09:09:40 +0000603 # XXX Should this raise ValueError or ImportError?
604 try:
605 loader.loadTestsFromNames(['abc () //'])
606 except ValueError:
607 pass
608 except ImportError:
609 pass
610 else:
611 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000612
Georg Brandl15c5ce92007-03-07 09:09:40 +0000613 # "The specifier name is a ``dotted name'' that may resolve either to
614 # a module, a test case class, a TestSuite instance, a test method
615 # within a test case class, or a callable object which returns a
616 # TestCase or TestSuite instance."
617 #
Tim Petersea5962f2007-03-12 18:07:52 +0000618 # What happens when no module can be found for the given name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000619 def test_loadTestsFromNames__unknown_module_name(self):
620 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000621
Georg Brandl15c5ce92007-03-07 09:09:40 +0000622 try:
623 loader.loadTestsFromNames(['sdasfasfasdf'])
624 except ImportError, e:
625 self.assertEqual(str(e), "No module named sdasfasfasdf")
626 else:
627 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
Tim Petersea5962f2007-03-12 18:07:52 +0000628
Georg Brandl15c5ce92007-03-07 09:09:40 +0000629 # "The specifier name is a ``dotted name'' that may resolve either to
630 # a module, a test case class, a TestSuite instance, a test method
631 # within a test case class, or a callable object which returns a
632 # TestCase or TestSuite instance."
633 #
Tim Petersea5962f2007-03-12 18:07:52 +0000634 # What happens when the module can be found, but not the attribute?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000635 def test_loadTestsFromNames__unknown_attr_name(self):
636 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000637
Georg Brandl15c5ce92007-03-07 09:09:40 +0000638 try:
639 loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
640 except AttributeError, e:
641 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
642 else:
643 self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000644
Georg Brandl15c5ce92007-03-07 09:09:40 +0000645 # "The specifier name is a ``dotted name'' that may resolve either to
646 # a module, a test case class, a TestSuite instance, a test method
647 # within a test case class, or a callable object which returns a
648 # TestCase or TestSuite instance."
649 # ...
650 # "The method optionally resolves name relative to the given module"
651 #
652 # What happens when given an unknown attribute on a specified `module`
653 # argument?
654 def test_loadTestsFromNames__unknown_name_relative_1(self):
655 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000656
Georg Brandl15c5ce92007-03-07 09:09:40 +0000657 try:
658 loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
659 except AttributeError, e:
660 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
661 else:
662 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000663
Georg Brandl15c5ce92007-03-07 09:09:40 +0000664 # "The specifier name is a ``dotted name'' that may resolve either to
665 # a module, a test case class, a TestSuite instance, a test method
666 # within a test case class, or a callable object which returns a
667 # TestCase or TestSuite instance."
668 # ...
669 # "The method optionally resolves name relative to the given module"
670 #
671 # Do unknown attributes (relative to a provided module) still raise an
Tim Petersea5962f2007-03-12 18:07:52 +0000672 # exception even in the presence of valid attribute names?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000673 def test_loadTestsFromNames__unknown_name_relative_2(self):
674 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000675
Georg Brandl15c5ce92007-03-07 09:09:40 +0000676 try:
677 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
678 except AttributeError, e:
679 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
680 else:
681 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
682
683 # "The specifier name is a ``dotted name'' that may resolve either to
684 # a module, a test case class, a TestSuite instance, a test method
685 # within a test case class, or a callable object which returns a
686 # TestCase or TestSuite instance."
687 # ...
688 # "The method optionally resolves name relative to the given module"
689 #
690 # What happens when faced with the empty string?
691 #
692 # XXX This currently raises AttributeError, though ValueError is probably
693 # more appropriate
694 def test_loadTestsFromNames__relative_empty_name(self):
695 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000696
Georg Brandl15c5ce92007-03-07 09:09:40 +0000697 try:
698 loader.loadTestsFromNames([''], unittest)
699 except AttributeError:
700 pass
701 else:
702 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000703
Georg Brandl15c5ce92007-03-07 09:09:40 +0000704 # "The specifier name is a ``dotted name'' that may resolve either to
705 # a module, a test case class, a TestSuite instance, a test method
706 # within a test case class, or a callable object which returns a
707 # TestCase or TestSuite instance."
708 # ...
709 # "The method optionally resolves name relative to the given module"
710 #
Tim Petersea5962f2007-03-12 18:07:52 +0000711 # What happens when presented with an impossible attribute name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000712 def test_loadTestsFromNames__relative_malformed_name(self):
713 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000714
Georg Brandl15c5ce92007-03-07 09:09:40 +0000715 # XXX Should this raise AttributeError or ValueError?
716 try:
717 loader.loadTestsFromNames(['abc () //'], unittest)
718 except AttributeError:
719 pass
720 except ValueError:
721 pass
722 else:
723 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
724
725 # "The method optionally resolves name relative to the given module"
726 #
727 # Does loadTestsFromNames() make sure the provided `module` is in fact
728 # a module?
729 #
730 # XXX This validation is currently not done. This flexibility should
731 # either be documented or a TypeError should be raised.
732 def test_loadTestsFromNames__relative_not_a_module(self):
733 class MyTestCase(unittest.TestCase):
734 def test(self):
735 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000736
Georg Brandl15c5ce92007-03-07 09:09:40 +0000737 class NotAModule(object):
738 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000739
Georg Brandl15c5ce92007-03-07 09:09:40 +0000740 loader = unittest.TestLoader()
741 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000742
Georg Brandl15c5ce92007-03-07 09:09:40 +0000743 reference = [unittest.TestSuite([MyTestCase('test')])]
744 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000745
Georg Brandl15c5ce92007-03-07 09:09:40 +0000746 # "The specifier name is a ``dotted name'' that may resolve either to
747 # a module, a test case class, a TestSuite instance, a test method
748 # within a test case class, or a callable object which returns a
749 # TestCase or TestSuite instance."
750 #
751 # Does it raise an exception if the name resolves to an invalid
752 # object?
753 def test_loadTestsFromNames__relative_bad_object(self):
754 import new
755 m = new.module('m')
756 m.testcase_1 = object()
Tim Petersea5962f2007-03-12 18:07:52 +0000757
Georg Brandl15c5ce92007-03-07 09:09:40 +0000758 loader = unittest.TestLoader()
759 try:
760 loader.loadTestsFromNames(['testcase_1'], m)
761 except TypeError:
762 pass
763 else:
764 self.fail("Should have raised TypeError")
765
766 # "The specifier name is a ``dotted name'' that may resolve ... to
767 # ... a test case class"
768 def test_loadTestsFromNames__relative_TestCase_subclass(self):
769 import new
770 m = new.module('m')
771 class MyTestCase(unittest.TestCase):
772 def test(self):
773 pass
774 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000775
Georg Brandl15c5ce92007-03-07 09:09:40 +0000776 loader = unittest.TestLoader()
777 suite = loader.loadTestsFromNames(['testcase_1'], m)
778 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000779
Georg Brandl15c5ce92007-03-07 09:09:40 +0000780 expected = loader.suiteClass([MyTestCase('test')])
781 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000782
Georg Brandl15c5ce92007-03-07 09:09:40 +0000783 # "The specifier name is a ``dotted name'' that may resolve ... to
784 # ... a TestSuite instance"
785 def test_loadTestsFromNames__relative_TestSuite(self):
786 import new
787 m = new.module('m')
788 class MyTestCase(unittest.TestCase):
789 def test(self):
790 pass
791 m.testsuite = unittest.TestSuite([MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000792
Georg Brandl15c5ce92007-03-07 09:09:40 +0000793 loader = unittest.TestLoader()
794 suite = loader.loadTestsFromNames(['testsuite'], m)
795 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000796
Georg Brandl15c5ce92007-03-07 09:09:40 +0000797 self.assertEqual(list(suite), [m.testsuite])
Tim Petersea5962f2007-03-12 18:07:52 +0000798
Georg Brandl15c5ce92007-03-07 09:09:40 +0000799 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
800 # test method within a test case class"
801 def test_loadTestsFromNames__relative_testmethod(self):
802 import new
803 m = new.module('m')
804 class MyTestCase(unittest.TestCase):
805 def test(self):
806 pass
807 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000808
Georg Brandl15c5ce92007-03-07 09:09:40 +0000809 loader = unittest.TestLoader()
810 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
811 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000812
Georg Brandl15c5ce92007-03-07 09:09:40 +0000813 ref_suite = unittest.TestSuite([MyTestCase('test')])
814 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000815
Georg Brandl15c5ce92007-03-07 09:09:40 +0000816 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
817 # test method within a test case class"
818 #
819 # Does the method gracefully handle names that initially look like they
Tim Petersea5962f2007-03-12 18:07:52 +0000820 # resolve to "a test method within a test case class" but don't?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000821 def test_loadTestsFromNames__relative_invalid_testmethod(self):
822 import new
823 m = new.module('m')
824 class MyTestCase(unittest.TestCase):
825 def test(self):
826 pass
827 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000828
Georg Brandl15c5ce92007-03-07 09:09:40 +0000829 loader = unittest.TestLoader()
830 try:
831 loader.loadTestsFromNames(['testcase_1.testfoo'], m)
832 except AttributeError, e:
833 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
834 else:
835 self.fail("Failed to raise AttributeError")
836
837 # "The specifier name is a ``dotted name'' that may resolve ... to
838 # ... a callable object which returns a ... TestSuite instance"
839 def test_loadTestsFromNames__callable__TestSuite(self):
840 import new
841 m = new.module('m')
842 testcase_1 = unittest.FunctionTestCase(lambda: None)
843 testcase_2 = unittest.FunctionTestCase(lambda: None)
844 def return_TestSuite():
845 return unittest.TestSuite([testcase_1, testcase_2])
846 m.return_TestSuite = return_TestSuite
Tim Petersea5962f2007-03-12 18:07:52 +0000847
Georg Brandl15c5ce92007-03-07 09:09:40 +0000848 loader = unittest.TestLoader()
849 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
850 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000851
Georg Brandl15c5ce92007-03-07 09:09:40 +0000852 expected = unittest.TestSuite([testcase_1, testcase_2])
853 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000854
Georg Brandl15c5ce92007-03-07 09:09:40 +0000855 # "The specifier name is a ``dotted name'' that may resolve ... to
856 # ... a callable object which returns a TestCase ... instance"
857 def test_loadTestsFromNames__callable__TestCase_instance(self):
858 import new
859 m = new.module('m')
860 testcase_1 = unittest.FunctionTestCase(lambda: None)
861 def return_TestCase():
862 return testcase_1
863 m.return_TestCase = return_TestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000864
Georg Brandl15c5ce92007-03-07 09:09:40 +0000865 loader = unittest.TestLoader()
866 suite = loader.loadTestsFromNames(['return_TestCase'], m)
867 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000868
Georg Brandl15c5ce92007-03-07 09:09:40 +0000869 ref_suite = unittest.TestSuite([testcase_1])
870 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000871
Georg Brandl15c5ce92007-03-07 09:09:40 +0000872 # "The specifier name is a ``dotted name'' that may resolve ... to
873 # ... a callable object which returns a TestCase or TestSuite instance"
874 #
Tim Petersea5962f2007-03-12 18:07:52 +0000875 # Are staticmethods handled correctly?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000876 def test_loadTestsFromNames__callable__call_staticmethod(self):
877 import new
878 m = new.module('m')
879 class Test1(unittest.TestCase):
880 def test(self):
881 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000882
Georg Brandl15c5ce92007-03-07 09:09:40 +0000883 testcase_1 = Test1('test')
884 class Foo(unittest.TestCase):
885 @staticmethod
886 def foo():
887 return testcase_1
888 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +0000889
Georg Brandl15c5ce92007-03-07 09:09:40 +0000890 loader = unittest.TestLoader()
891 suite = loader.loadTestsFromNames(['Foo.foo'], m)
892 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000893
Georg Brandl15c5ce92007-03-07 09:09:40 +0000894 ref_suite = unittest.TestSuite([testcase_1])
895 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000896
Georg Brandl15c5ce92007-03-07 09:09:40 +0000897 # "The specifier name is a ``dotted name'' that may resolve ... to
898 # ... a callable object which returns a TestCase or TestSuite instance"
899 #
900 # What happens when the callable returns something else?
901 def test_loadTestsFromNames__callable__wrong_type(self):
902 import new
903 m = new.module('m')
904 def return_wrong():
905 return 6
906 m.return_wrong = return_wrong
Tim Petersea5962f2007-03-12 18:07:52 +0000907
Georg Brandl15c5ce92007-03-07 09:09:40 +0000908 loader = unittest.TestLoader()
909 try:
910 suite = loader.loadTestsFromNames(['return_wrong'], m)
911 except TypeError:
912 pass
913 else:
914 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000915
Georg Brandl15c5ce92007-03-07 09:09:40 +0000916 # "The specifier can refer to modules and packages which have not been
Tim Petersea5962f2007-03-12 18:07:52 +0000917 # imported; they will be imported as a side-effect"
Georg Brandl15c5ce92007-03-07 09:09:40 +0000918 def test_loadTestsFromNames__module_not_loaded(self):
919 # We're going to try to load this module as a side-effect, so it
920 # better not be loaded before we try.
921 #
922 # Why pick audioop? Google shows it isn't used very often, so there's
923 # a good chance that it won't be imported when this test is run
924 module_name = 'audioop'
Tim Petersea5962f2007-03-12 18:07:52 +0000925
Georg Brandl15c5ce92007-03-07 09:09:40 +0000926 import sys
927 if module_name in sys.modules:
928 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000929
Georg Brandl15c5ce92007-03-07 09:09:40 +0000930 loader = unittest.TestLoader()
931 try:
932 suite = loader.loadTestsFromNames([module_name])
933
934 self.failUnless(isinstance(suite, loader.suiteClass))
935 self.assertEqual(list(suite), [unittest.TestSuite()])
936
937 # audioop should now be loaded, thanks to loadTestsFromName()
938 self.failUnless(module_name in sys.modules)
939 finally:
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +0000940 if module_name in sys.modules:
941 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000942
Georg Brandl15c5ce92007-03-07 09:09:40 +0000943 ################################################################
944 ### /Tests for TestLoader.loadTestsFromNames()
945
946 ### Tests for TestLoader.getTestCaseNames()
947 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000948
Georg Brandl15c5ce92007-03-07 09:09:40 +0000949 # "Return a sorted sequence of method names found within testCaseClass"
950 #
951 # Test.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +0000952 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +0000953 def test_getTestCaseNames(self):
954 class Test(unittest.TestCase):
955 def test_1(self): pass
956 def test_2(self): pass
957 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000958
Georg Brandl15c5ce92007-03-07 09:09:40 +0000959 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000960
Georg Brandl15c5ce92007-03-07 09:09:40 +0000961 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
Tim Petersea5962f2007-03-12 18:07:52 +0000962
Georg Brandl15c5ce92007-03-07 09:09:40 +0000963 # "Return a sorted sequence of method names found within testCaseClass"
964 #
Tim Petersea5962f2007-03-12 18:07:52 +0000965 # Does getTestCaseNames() behave appropriately if no tests are found?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000966 def test_getTestCaseNames__no_tests(self):
967 class Test(unittest.TestCase):
968 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000969
Georg Brandl15c5ce92007-03-07 09:09:40 +0000970 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000971
Georg Brandl15c5ce92007-03-07 09:09:40 +0000972 self.assertEqual(loader.getTestCaseNames(Test), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000973
Georg Brandl15c5ce92007-03-07 09:09:40 +0000974 # "Return a sorted sequence of method names found within testCaseClass"
975 #
976 # Are not-TestCases handled gracefully?
977 #
978 # XXX This should raise a TypeError, not return a list
979 #
980 # XXX It's too late in the 2.5 release cycle to fix this, but it should
981 # probably be revisited for 2.6
982 def test_getTestCaseNames__not_a_TestCase(self):
983 class BadCase(int):
984 def test_foo(self):
985 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000986
Georg Brandl15c5ce92007-03-07 09:09:40 +0000987 loader = unittest.TestLoader()
988 names = loader.getTestCaseNames(BadCase)
Tim Petersea5962f2007-03-12 18:07:52 +0000989
Georg Brandl15c5ce92007-03-07 09:09:40 +0000990 self.assertEqual(names, ['test_foo'])
Tim Petersea5962f2007-03-12 18:07:52 +0000991
Georg Brandl15c5ce92007-03-07 09:09:40 +0000992 # "Return a sorted sequence of method names found within testCaseClass"
993 #
994 # Make sure inherited names are handled.
995 #
996 # TestP.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +0000997 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +0000998 def test_getTestCaseNames__inheritance(self):
999 class TestP(unittest.TestCase):
1000 def test_1(self): pass
1001 def test_2(self): pass
1002 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001003
Georg Brandl15c5ce92007-03-07 09:09:40 +00001004 class TestC(TestP):
1005 def test_1(self): pass
1006 def test_3(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001007
Georg Brandl15c5ce92007-03-07 09:09:40 +00001008 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +00001009
Georg Brandl15c5ce92007-03-07 09:09:40 +00001010 names = ['test_1', 'test_2', 'test_3']
1011 self.assertEqual(loader.getTestCaseNames(TestC), names)
Tim Petersea5962f2007-03-12 18:07:52 +00001012
1013 ################################################################
Georg Brandl15c5ce92007-03-07 09:09:40 +00001014 ### /Tests for TestLoader.getTestCaseNames()
1015
1016 ### Tests for TestLoader.testMethodPrefix
1017 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001018
Georg Brandl15c5ce92007-03-07 09:09:40 +00001019 # "String giving the prefix of method names which will be interpreted as
1020 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001021 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001022 # Implicit in the documentation is that testMethodPrefix is respected by
1023 # all loadTestsFrom* methods.
1024 def test_testMethodPrefix__loadTestsFromTestCase(self):
1025 class Foo(unittest.TestCase):
1026 def test_1(self): pass
1027 def test_2(self): pass
1028 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001029
Georg Brandl15c5ce92007-03-07 09:09:40 +00001030 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1031 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001032
Georg Brandl15c5ce92007-03-07 09:09:40 +00001033 loader = unittest.TestLoader()
1034 loader.testMethodPrefix = 'foo'
1035 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1036
1037 loader.testMethodPrefix = 'test'
1038 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001039
Georg Brandl15c5ce92007-03-07 09:09:40 +00001040 # "String giving the prefix of method names which will be interpreted as
1041 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001042 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001043 # Implicit in the documentation is that testMethodPrefix is respected by
1044 # all loadTestsFrom* methods.
1045 def test_testMethodPrefix__loadTestsFromModule(self):
1046 import new
1047 m = new.module('m')
1048 class Foo(unittest.TestCase):
1049 def test_1(self): pass
1050 def test_2(self): pass
1051 def foo_bar(self): pass
1052 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001053
Georg Brandl15c5ce92007-03-07 09:09:40 +00001054 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
1055 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
Tim Petersea5962f2007-03-12 18:07:52 +00001056
Georg Brandl15c5ce92007-03-07 09:09:40 +00001057 loader = unittest.TestLoader()
1058 loader.testMethodPrefix = 'foo'
1059 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1060
1061 loader.testMethodPrefix = 'test'
1062 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001063
Georg Brandl15c5ce92007-03-07 09:09:40 +00001064 # "String giving the prefix of method names which will be interpreted as
1065 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001066 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001067 # Implicit in the documentation is that testMethodPrefix is respected by
1068 # all loadTestsFrom* methods.
1069 def test_testMethodPrefix__loadTestsFromName(self):
1070 import new
1071 m = new.module('m')
1072 class Foo(unittest.TestCase):
1073 def test_1(self): pass
1074 def test_2(self): pass
1075 def foo_bar(self): pass
1076 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001077
Georg Brandl15c5ce92007-03-07 09:09:40 +00001078 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1079 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001080
Georg Brandl15c5ce92007-03-07 09:09:40 +00001081 loader = unittest.TestLoader()
1082 loader.testMethodPrefix = 'foo'
1083 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1084
1085 loader.testMethodPrefix = 'test'
1086 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001087
Georg Brandl15c5ce92007-03-07 09:09:40 +00001088 # "String giving the prefix of method names which will be interpreted as
1089 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001090 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001091 # Implicit in the documentation is that testMethodPrefix is respected by
1092 # all loadTestsFrom* methods.
1093 def test_testMethodPrefix__loadTestsFromNames(self):
1094 import new
1095 m = new.module('m')
1096 class Foo(unittest.TestCase):
1097 def test_1(self): pass
1098 def test_2(self): pass
1099 def foo_bar(self): pass
1100 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001101
Georg Brandl15c5ce92007-03-07 09:09:40 +00001102 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
1103 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1104 tests_2 = unittest.TestSuite([tests_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001105
Georg Brandl15c5ce92007-03-07 09:09:40 +00001106 loader = unittest.TestLoader()
1107 loader.testMethodPrefix = 'foo'
1108 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1109
1110 loader.testMethodPrefix = 'test'
1111 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001112
Georg Brandl15c5ce92007-03-07 09:09:40 +00001113 # "The default value is 'test'"
1114 def test_testMethodPrefix__default_value(self):
1115 loader = unittest.TestLoader()
1116 self.failUnless(loader.testMethodPrefix == 'test')
Tim Petersea5962f2007-03-12 18:07:52 +00001117
Georg Brandl15c5ce92007-03-07 09:09:40 +00001118 ################################################################
1119 ### /Tests for TestLoader.testMethodPrefix
1120
Tim Petersea5962f2007-03-12 18:07:52 +00001121 ### Tests for TestLoader.sortTestMethodsUsing
Georg Brandl15c5ce92007-03-07 09:09:40 +00001122 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001123
Georg Brandl15c5ce92007-03-07 09:09:40 +00001124 # "Function to be used to compare method names when sorting them in
1125 # getTestCaseNames() and all the loadTestsFromX() methods"
1126 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1127 def reversed_cmp(x, y):
1128 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001129
Georg Brandl15c5ce92007-03-07 09:09:40 +00001130 class Foo(unittest.TestCase):
1131 def test_1(self): pass
1132 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001133
Georg Brandl15c5ce92007-03-07 09:09:40 +00001134 loader = unittest.TestLoader()
1135 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001136
Georg Brandl15c5ce92007-03-07 09:09:40 +00001137 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1138 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001139
Georg Brandl15c5ce92007-03-07 09:09:40 +00001140 # "Function to be used to compare method names when sorting them in
1141 # getTestCaseNames() and all the loadTestsFromX() methods"
1142 def test_sortTestMethodsUsing__loadTestsFromModule(self):
1143 def reversed_cmp(x, y):
1144 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001145
Georg Brandl15c5ce92007-03-07 09:09:40 +00001146 import new
Tim Petersea5962f2007-03-12 18:07:52 +00001147 m = new.module('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001148 class Foo(unittest.TestCase):
1149 def test_1(self): pass
1150 def test_2(self): pass
1151 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001152
Georg Brandl15c5ce92007-03-07 09:09:40 +00001153 loader = unittest.TestLoader()
1154 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001155
Georg Brandl15c5ce92007-03-07 09:09:40 +00001156 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1157 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001158
Georg Brandl15c5ce92007-03-07 09:09:40 +00001159 # "Function to be used to compare method names when sorting them in
1160 # getTestCaseNames() and all the loadTestsFromX() methods"
1161 def test_sortTestMethodsUsing__loadTestsFromName(self):
1162 def reversed_cmp(x, y):
1163 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001164
Georg Brandl15c5ce92007-03-07 09:09:40 +00001165 import new
Tim Petersea5962f2007-03-12 18:07:52 +00001166 m = new.module('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001167 class Foo(unittest.TestCase):
1168 def test_1(self): pass
1169 def test_2(self): pass
1170 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001171
Georg Brandl15c5ce92007-03-07 09:09:40 +00001172 loader = unittest.TestLoader()
1173 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001174
Georg Brandl15c5ce92007-03-07 09:09:40 +00001175 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1176 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001177
Georg Brandl15c5ce92007-03-07 09:09:40 +00001178 # "Function to be used to compare method names when sorting them in
1179 # getTestCaseNames() and all the loadTestsFromX() methods"
1180 def test_sortTestMethodsUsing__loadTestsFromNames(self):
1181 def reversed_cmp(x, y):
1182 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001183
Georg Brandl15c5ce92007-03-07 09:09:40 +00001184 import new
Tim Petersea5962f2007-03-12 18:07:52 +00001185 m = new.module('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001186 class Foo(unittest.TestCase):
1187 def test_1(self): pass
1188 def test_2(self): pass
1189 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001190
Georg Brandl15c5ce92007-03-07 09:09:40 +00001191 loader = unittest.TestLoader()
1192 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001193
Georg Brandl15c5ce92007-03-07 09:09:40 +00001194 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1195 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001196
Georg Brandl15c5ce92007-03-07 09:09:40 +00001197 # "Function to be used to compare method names when sorting them in
1198 # getTestCaseNames()"
1199 #
1200 # Does it actually affect getTestCaseNames()?
1201 def test_sortTestMethodsUsing__getTestCaseNames(self):
1202 def reversed_cmp(x, y):
1203 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001204
Georg Brandl15c5ce92007-03-07 09:09:40 +00001205 class Foo(unittest.TestCase):
1206 def test_1(self): pass
1207 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001208
Georg Brandl15c5ce92007-03-07 09:09:40 +00001209 loader = unittest.TestLoader()
1210 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001211
Georg Brandl15c5ce92007-03-07 09:09:40 +00001212 test_names = ['test_2', 'test_1']
1213 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
Tim Petersea5962f2007-03-12 18:07:52 +00001214
Georg Brandl15c5ce92007-03-07 09:09:40 +00001215 # "The default value is the built-in cmp() function"
1216 def test_sortTestMethodsUsing__default_value(self):
1217 loader = unittest.TestLoader()
1218 self.failUnless(loader.sortTestMethodsUsing is cmp)
Tim Petersea5962f2007-03-12 18:07:52 +00001219
Georg Brandl15c5ce92007-03-07 09:09:40 +00001220 # "it can be set to None to disable the sort."
1221 #
1222 # XXX How is this different from reassigning cmp? Are the tests returned
1223 # in a random order or something? This behaviour should die
1224 def test_sortTestMethodsUsing__None(self):
1225 class Foo(unittest.TestCase):
1226 def test_1(self): pass
1227 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001228
Georg Brandl15c5ce92007-03-07 09:09:40 +00001229 loader = unittest.TestLoader()
1230 loader.sortTestMethodsUsing = None
Tim Petersea5962f2007-03-12 18:07:52 +00001231
Georg Brandl15c5ce92007-03-07 09:09:40 +00001232 test_names = ['test_2', 'test_1']
1233 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
Tim Petersea5962f2007-03-12 18:07:52 +00001234
Georg Brandl15c5ce92007-03-07 09:09:40 +00001235 ################################################################
1236 ### /Tests for TestLoader.sortTestMethodsUsing
Tim Petersea5962f2007-03-12 18:07:52 +00001237
Georg Brandl15c5ce92007-03-07 09:09:40 +00001238 ### Tests for TestLoader.suiteClass
1239 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001240
Georg Brandl15c5ce92007-03-07 09:09:40 +00001241 # "Callable object that constructs a test suite from a list of tests."
1242 def test_suiteClass__loadTestsFromTestCase(self):
1243 class Foo(unittest.TestCase):
1244 def test_1(self): pass
1245 def test_2(self): pass
1246 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001247
Georg Brandl15c5ce92007-03-07 09:09:40 +00001248 tests = [Foo('test_1'), Foo('test_2')]
1249
1250 loader = unittest.TestLoader()
1251 loader.suiteClass = list
1252 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001253
Georg Brandl15c5ce92007-03-07 09:09:40 +00001254 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001255 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001256 def test_suiteClass__loadTestsFromModule(self):
1257 import new
1258 m = new.module('m')
1259 class Foo(unittest.TestCase):
1260 def test_1(self): pass
1261 def test_2(self): pass
1262 def foo_bar(self): pass
1263 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001264
Georg Brandl15c5ce92007-03-07 09:09:40 +00001265 tests = [[Foo('test_1'), Foo('test_2')]]
1266
1267 loader = unittest.TestLoader()
1268 loader.suiteClass = list
1269 self.assertEqual(loader.loadTestsFromModule(m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001270
Georg Brandl15c5ce92007-03-07 09:09:40 +00001271 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001272 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001273 def test_suiteClass__loadTestsFromName(self):
1274 import new
1275 m = new.module('m')
1276 class Foo(unittest.TestCase):
1277 def test_1(self): pass
1278 def test_2(self): pass
1279 def foo_bar(self): pass
1280 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001281
Georg Brandl15c5ce92007-03-07 09:09:40 +00001282 tests = [Foo('test_1'), Foo('test_2')]
1283
1284 loader = unittest.TestLoader()
1285 loader.suiteClass = list
1286 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001287
Georg Brandl15c5ce92007-03-07 09:09:40 +00001288 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001289 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001290 def test_suiteClass__loadTestsFromNames(self):
1291 import new
1292 m = new.module('m')
1293 class Foo(unittest.TestCase):
1294 def test_1(self): pass
1295 def test_2(self): pass
1296 def foo_bar(self): pass
1297 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001298
Georg Brandl15c5ce92007-03-07 09:09:40 +00001299 tests = [[Foo('test_1'), Foo('test_2')]]
1300
1301 loader = unittest.TestLoader()
1302 loader.suiteClass = list
1303 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001304
Georg Brandl15c5ce92007-03-07 09:09:40 +00001305 # "The default value is the TestSuite class"
1306 def test_suiteClass__default_value(self):
1307 loader = unittest.TestLoader()
1308 self.failUnless(loader.suiteClass is unittest.TestSuite)
Tim Petersea5962f2007-03-12 18:07:52 +00001309
Georg Brandl15c5ce92007-03-07 09:09:40 +00001310 ################################################################
1311 ### /Tests for TestLoader.suiteClass
1312
1313### Support code for Test_TestSuite
1314################################################################
1315
1316class Foo(unittest.TestCase):
1317 def test_1(self): pass
1318 def test_2(self): pass
1319 def test_3(self): pass
1320 def runTest(self): pass
1321
1322def _mk_TestSuite(*names):
1323 return unittest.TestSuite(Foo(n) for n in names)
Tim Petersea5962f2007-03-12 18:07:52 +00001324
Georg Brandl15c5ce92007-03-07 09:09:40 +00001325################################################################
1326### /Support code for Test_TestSuite
1327
1328class Test_TestSuite(TestCase, TestEquality):
1329
1330 ### Set up attributes needed by inherited tests
1331 ################################################################
1332
1333 # Used by TestEquality.test_eq
1334 eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
1335 ,(unittest.TestSuite(), unittest.TestSuite([]))
1336 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001337
1338 # Used by TestEquality.test_ne
Georg Brandl15c5ce92007-03-07 09:09:40 +00001339 ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
1340 ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
1341 ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
1342 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001343
Georg Brandl15c5ce92007-03-07 09:09:40 +00001344 ################################################################
1345 ### /Set up attributes needed by inherited tests
1346
1347 ### Tests for TestSuite.__init__
1348 ################################################################
1349
1350 # "class TestSuite([tests])"
1351 #
1352 # The tests iterable should be optional
1353 def test_init__tests_optional(self):
1354 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001355
Georg Brandl15c5ce92007-03-07 09:09:40 +00001356 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001357
Georg Brandl15c5ce92007-03-07 09:09:40 +00001358 # "class TestSuite([tests])"
1359 # ...
1360 # "If tests is given, it must be an iterable of individual test cases
1361 # or other test suites that will be used to build the suite initially"
1362 #
1363 # TestSuite should deal with empty tests iterables by allowing the
Tim Petersea5962f2007-03-12 18:07:52 +00001364 # creation of an empty suite
Georg Brandl15c5ce92007-03-07 09:09:40 +00001365 def test_init__empty_tests(self):
1366 suite = unittest.TestSuite([])
Tim Petersea5962f2007-03-12 18:07:52 +00001367
Georg Brandl15c5ce92007-03-07 09:09:40 +00001368 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001369
Georg Brandl15c5ce92007-03-07 09:09:40 +00001370 # "class TestSuite([tests])"
1371 # ...
1372 # "If tests is given, it must be an iterable of individual test cases
1373 # or other test suites that will be used to build the suite initially"
1374 #
Tim Petersea5962f2007-03-12 18:07:52 +00001375 # TestSuite should allow any iterable to provide tests
Georg Brandl15c5ce92007-03-07 09:09:40 +00001376 def test_init__tests_from_any_iterable(self):
1377 def tests():
1378 yield unittest.FunctionTestCase(lambda: None)
1379 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001380
Georg Brandl15c5ce92007-03-07 09:09:40 +00001381 suite_1 = unittest.TestSuite(tests())
1382 self.assertEqual(suite_1.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001383
Georg Brandl15c5ce92007-03-07 09:09:40 +00001384 suite_2 = unittest.TestSuite(suite_1)
1385 self.assertEqual(suite_2.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001386
Georg Brandl15c5ce92007-03-07 09:09:40 +00001387 suite_3 = unittest.TestSuite(set(suite_1))
1388 self.assertEqual(suite_3.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001389
Georg Brandl15c5ce92007-03-07 09:09:40 +00001390 # "class TestSuite([tests])"
1391 # ...
1392 # "If tests is given, it must be an iterable of individual test cases
1393 # or other test suites that will be used to build the suite initially"
1394 #
1395 # Does TestSuite() also allow other TestSuite() instances to be present
1396 # in the tests iterable?
1397 def test_init__TestSuite_instances_in_tests(self):
1398 def tests():
1399 ftc = unittest.FunctionTestCase(lambda: None)
1400 yield unittest.TestSuite([ftc])
1401 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001402
Georg Brandl15c5ce92007-03-07 09:09:40 +00001403 suite = unittest.TestSuite(tests())
1404 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001405
Georg Brandl15c5ce92007-03-07 09:09:40 +00001406 ################################################################
1407 ### /Tests for TestSuite.__init__
1408
1409 # Container types should support the iter protocol
1410 def test_iter(self):
1411 test1 = unittest.FunctionTestCase(lambda: None)
1412 test2 = unittest.FunctionTestCase(lambda: None)
1413 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001414
Georg Brandl15c5ce92007-03-07 09:09:40 +00001415 self.assertEqual(list(suite), [test1, test2])
Tim Petersea5962f2007-03-12 18:07:52 +00001416
Georg Brandl15c5ce92007-03-07 09:09:40 +00001417 # "Return the number of tests represented by the this test object.
1418 # ...this method is also implemented by the TestSuite class, which can
1419 # return larger [greater than 1] values"
1420 #
Tim Petersea5962f2007-03-12 18:07:52 +00001421 # Presumably an empty TestSuite returns 0?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001422 def test_countTestCases_zero_simple(self):
1423 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001424
Georg Brandl15c5ce92007-03-07 09:09:40 +00001425 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001426
Georg Brandl15c5ce92007-03-07 09:09:40 +00001427 # "Return the number of tests represented by the this test object.
1428 # ...this method is also implemented by the TestSuite class, which can
1429 # return larger [greater than 1] values"
1430 #
1431 # Presumably an empty TestSuite (even if it contains other empty
1432 # TestSuite instances) returns 0?
1433 def test_countTestCases_zero_nested(self):
1434 class Test1(unittest.TestCase):
1435 def test(self):
1436 pass
1437
1438 suite = unittest.TestSuite([unittest.TestSuite()])
Tim Petersea5962f2007-03-12 18:07:52 +00001439
Georg Brandl15c5ce92007-03-07 09:09:40 +00001440 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001441
Georg Brandl15c5ce92007-03-07 09:09:40 +00001442 # "Return the number of tests represented by the this test object.
1443 # ...this method is also implemented by the TestSuite class, which can
1444 # return larger [greater than 1] values"
1445 def test_countTestCases_simple(self):
1446 test1 = unittest.FunctionTestCase(lambda: None)
1447 test2 = unittest.FunctionTestCase(lambda: None)
1448 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001449
Georg Brandl15c5ce92007-03-07 09:09:40 +00001450 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001451
Georg Brandl15c5ce92007-03-07 09:09:40 +00001452 # "Return the number of tests represented by the this test object.
1453 # ...this method is also implemented by the TestSuite class, which can
1454 # return larger [greater than 1] values"
1455 #
1456 # Make sure this holds for nested TestSuite instances, too
1457 def test_countTestCases_nested(self):
1458 class Test1(unittest.TestCase):
1459 def test1(self): pass
1460 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001461
Georg Brandl15c5ce92007-03-07 09:09:40 +00001462 test2 = unittest.FunctionTestCase(lambda: None)
1463 test3 = unittest.FunctionTestCase(lambda: None)
1464 child = unittest.TestSuite((Test1('test2'), test2))
1465 parent = unittest.TestSuite((test3, child, Test1('test1')))
Tim Petersea5962f2007-03-12 18:07:52 +00001466
Georg Brandl15c5ce92007-03-07 09:09:40 +00001467 self.assertEqual(parent.countTestCases(), 4)
Tim Petersea5962f2007-03-12 18:07:52 +00001468
Georg Brandl15c5ce92007-03-07 09:09:40 +00001469 # "Run the tests associated with this suite, collecting the result into
1470 # the test result object passed as result."
1471 #
1472 # And if there are no tests? What then?
1473 def test_run__empty_suite(self):
1474 events = []
1475 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001476
Georg Brandl15c5ce92007-03-07 09:09:40 +00001477 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001478
Georg Brandl15c5ce92007-03-07 09:09:40 +00001479 suite.run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001480
Georg Brandl15c5ce92007-03-07 09:09:40 +00001481 self.assertEqual(events, [])
Tim Petersea5962f2007-03-12 18:07:52 +00001482
Georg Brandl15c5ce92007-03-07 09:09:40 +00001483 # "Note that unlike TestCase.run(), TestSuite.run() requires the
1484 # "result object to be passed in."
1485 def test_run__requires_result(self):
1486 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001487
Georg Brandl15c5ce92007-03-07 09:09:40 +00001488 try:
1489 suite.run()
1490 except TypeError:
1491 pass
1492 else:
1493 self.fail("Failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +00001494
Georg Brandl15c5ce92007-03-07 09:09:40 +00001495 # "Run the tests associated with this suite, collecting the result into
Tim Petersea5962f2007-03-12 18:07:52 +00001496 # the test result object passed as result."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001497 def test_run(self):
1498 events = []
1499 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001500
Georg Brandl15c5ce92007-03-07 09:09:40 +00001501 class LoggingCase(unittest.TestCase):
1502 def run(self, result):
1503 events.append('run %s' % self._testMethodName)
Tim Petersea5962f2007-03-12 18:07:52 +00001504
Georg Brandl15c5ce92007-03-07 09:09:40 +00001505 def test1(self): pass
1506 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001507
1508 tests = [LoggingCase('test1'), LoggingCase('test2')]
1509
Georg Brandl15c5ce92007-03-07 09:09:40 +00001510 unittest.TestSuite(tests).run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001511
Georg Brandl15c5ce92007-03-07 09:09:40 +00001512 self.assertEqual(events, ['run test1', 'run test2'])
Tim Petersea5962f2007-03-12 18:07:52 +00001513
1514 # "Add a TestCase ... to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001515 def test_addTest__TestCase(self):
1516 class Foo(unittest.TestCase):
1517 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001518
Georg Brandl15c5ce92007-03-07 09:09:40 +00001519 test = Foo('test')
1520 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001521
Georg Brandl15c5ce92007-03-07 09:09:40 +00001522 suite.addTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001523
Georg Brandl15c5ce92007-03-07 09:09:40 +00001524 self.assertEqual(suite.countTestCases(), 1)
1525 self.assertEqual(list(suite), [test])
Tim Petersea5962f2007-03-12 18:07:52 +00001526
1527 # "Add a ... TestSuite to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001528 def test_addTest__TestSuite(self):
1529 class Foo(unittest.TestCase):
1530 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001531
Georg Brandl15c5ce92007-03-07 09:09:40 +00001532 suite_2 = unittest.TestSuite([Foo('test')])
Tim Petersea5962f2007-03-12 18:07:52 +00001533
Georg Brandl15c5ce92007-03-07 09:09:40 +00001534 suite = unittest.TestSuite()
1535 suite.addTest(suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001536
Georg Brandl15c5ce92007-03-07 09:09:40 +00001537 self.assertEqual(suite.countTestCases(), 1)
1538 self.assertEqual(list(suite), [suite_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001539
Georg Brandl15c5ce92007-03-07 09:09:40 +00001540 # "Add all the tests from an iterable of TestCase and TestSuite
1541 # instances to this test suite."
Tim Petersea5962f2007-03-12 18:07:52 +00001542 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001543 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001544 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001545 def test_addTests(self):
1546 class Foo(unittest.TestCase):
1547 def test_1(self): pass
1548 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001549
Georg Brandl15c5ce92007-03-07 09:09:40 +00001550 test_1 = Foo('test_1')
1551 test_2 = Foo('test_2')
1552 inner_suite = unittest.TestSuite([test_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001553
Georg Brandl15c5ce92007-03-07 09:09:40 +00001554 def gen():
1555 yield test_1
1556 yield test_2
1557 yield inner_suite
Tim Petersea5962f2007-03-12 18:07:52 +00001558
Georg Brandl15c5ce92007-03-07 09:09:40 +00001559 suite_1 = unittest.TestSuite()
1560 suite_1.addTests(gen())
Tim Petersea5962f2007-03-12 18:07:52 +00001561
Georg Brandl15c5ce92007-03-07 09:09:40 +00001562 self.assertEqual(list(suite_1), list(gen()))
Tim Petersea5962f2007-03-12 18:07:52 +00001563
Georg Brandl15c5ce92007-03-07 09:09:40 +00001564 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001565 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001566 suite_2 = unittest.TestSuite()
1567 for t in gen():
1568 suite_2.addTest(t)
Tim Petersea5962f2007-03-12 18:07:52 +00001569
Georg Brandl15c5ce92007-03-07 09:09:40 +00001570 self.assertEqual(suite_1, suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001571
Georg Brandl15c5ce92007-03-07 09:09:40 +00001572 # "Add all the tests from an iterable of TestCase and TestSuite
1573 # instances to this test suite."
1574 #
Tim Petersea5962f2007-03-12 18:07:52 +00001575 # What happens if it doesn't get an iterable?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001576 def test_addTest__noniterable(self):
1577 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001578
Georg Brandl15c5ce92007-03-07 09:09:40 +00001579 try:
1580 suite.addTests(5)
1581 except TypeError:
1582 pass
1583 else:
1584 self.fail("Failed to raise TypeError")
Georg Brandld9e50262007-03-07 11:54:49 +00001585
1586 def test_addTest__noncallable(self):
1587 suite = unittest.TestSuite()
1588 self.assertRaises(TypeError, suite.addTest, 5)
1589
1590 def test_addTest__casesuiteclass(self):
1591 suite = unittest.TestSuite()
1592 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
1593 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
1594
1595 def test_addTests__string(self):
1596 suite = unittest.TestSuite()
1597 self.assertRaises(TypeError, suite.addTests, "foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001598
1599
Georg Brandl15c5ce92007-03-07 09:09:40 +00001600class Test_FunctionTestCase(TestCase):
Tim Petersea5962f2007-03-12 18:07:52 +00001601
Georg Brandl15c5ce92007-03-07 09:09:40 +00001602 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00001603 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001604 def test_countTestCases(self):
1605 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001606
Georg Brandl15c5ce92007-03-07 09:09:40 +00001607 self.assertEqual(test.countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00001608
Georg Brandl15c5ce92007-03-07 09:09:40 +00001609 # "When a setUp() method is defined, the test runner will run that method
1610 # prior to each test. Likewise, if a tearDown() method is defined, the
1611 # test runner will invoke that method after each test. In the example,
1612 # setUp() was used to create a fresh sequence for each test."
1613 #
1614 # Make sure the proper call order is maintained, even if setUp() raises
1615 # an exception.
1616 def test_run_call_order__error_in_setUp(self):
1617 events = []
1618 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001619
Georg Brandl15c5ce92007-03-07 09:09:40 +00001620 def setUp():
1621 events.append('setUp')
1622 raise RuntimeError('raised by setUp')
1623
1624 def test():
1625 events.append('test')
1626
1627 def tearDown():
1628 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001629
1630 expected = ['startTest', 'setUp', 'addError', 'stopTest']
Georg Brandl15c5ce92007-03-07 09:09:40 +00001631 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1632 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001633
Georg Brandl15c5ce92007-03-07 09:09:40 +00001634 # "When a setUp() method is defined, the test runner will run that method
1635 # prior to each test. Likewise, if a tearDown() method is defined, the
1636 # test runner will invoke that method after each test. In the example,
1637 # setUp() was used to create a fresh sequence for each test."
1638 #
1639 # Make sure the proper call order is maintained, even if the test raises
1640 # an error (as opposed to a failure).
1641 def test_run_call_order__error_in_test(self):
1642 events = []
1643 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001644
Georg Brandl15c5ce92007-03-07 09:09:40 +00001645 def setUp():
1646 events.append('setUp')
1647
1648 def test():
1649 events.append('test')
1650 raise RuntimeError('raised by test')
1651
1652 def tearDown():
1653 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001654
Georg Brandl15c5ce92007-03-07 09:09:40 +00001655 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
1656 'stopTest']
1657 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1658 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001659
Georg Brandl15c5ce92007-03-07 09:09:40 +00001660 # "When a setUp() method is defined, the test runner will run that method
1661 # prior to each test. Likewise, if a tearDown() method is defined, the
1662 # test runner will invoke that method after each test. In the example,
1663 # setUp() was used to create a fresh sequence for each test."
1664 #
1665 # Make sure the proper call order is maintained, even if the test signals
1666 # a failure (as opposed to an error).
1667 def test_run_call_order__failure_in_test(self):
1668 events = []
1669 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001670
Georg Brandl15c5ce92007-03-07 09:09:40 +00001671 def setUp():
1672 events.append('setUp')
1673
1674 def test():
1675 events.append('test')
1676 self.fail('raised by test')
1677
1678 def tearDown():
1679 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001680
Georg Brandl15c5ce92007-03-07 09:09:40 +00001681 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
1682 'stopTest']
1683 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1684 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001685
Georg Brandl15c5ce92007-03-07 09:09:40 +00001686 # "When a setUp() method is defined, the test runner will run that method
1687 # prior to each test. Likewise, if a tearDown() method is defined, the
1688 # test runner will invoke that method after each test. In the example,
1689 # setUp() was used to create a fresh sequence for each test."
1690 #
1691 # Make sure the proper call order is maintained, even if tearDown() raises
1692 # an exception.
1693 def test_run_call_order__error_in_tearDown(self):
1694 events = []
1695 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001696
Georg Brandl15c5ce92007-03-07 09:09:40 +00001697 def setUp():
1698 events.append('setUp')
1699
1700 def test():
1701 events.append('test')
1702
1703 def tearDown():
1704 events.append('tearDown')
1705 raise RuntimeError('raised by tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001706
Georg Brandl15c5ce92007-03-07 09:09:40 +00001707 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
1708 'stopTest']
1709 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1710 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001711
Georg Brandl15c5ce92007-03-07 09:09:40 +00001712 # "Return a string identifying the specific test case."
1713 #
1714 # Because of the vague nature of the docs, I'm not going to lock this
1715 # test down too much. Really all that can be asserted is that the id()
1716 # will be a string (either 8-byte or unicode -- again, because the docs
1717 # just say "string")
1718 def test_id(self):
1719 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001720
Georg Brandl15c5ce92007-03-07 09:09:40 +00001721 self.failUnless(isinstance(test.id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00001722
Georg Brandl15c5ce92007-03-07 09:09:40 +00001723 # "Returns a one-line description of the test, or None if no description
1724 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001725 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001726 def test_shortDescription__no_docstring(self):
1727 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001728
Georg Brandl15c5ce92007-03-07 09:09:40 +00001729 self.assertEqual(test.shortDescription(), None)
Tim Petersea5962f2007-03-12 18:07:52 +00001730
Georg Brandl15c5ce92007-03-07 09:09:40 +00001731 # "Returns a one-line description of the test, or None if no description
1732 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001733 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001734 def test_shortDescription__singleline_docstring(self):
1735 desc = "this tests foo"
1736 test = unittest.FunctionTestCase(lambda: None, description=desc)
Tim Petersea5962f2007-03-12 18:07:52 +00001737
Georg Brandl15c5ce92007-03-07 09:09:40 +00001738 self.assertEqual(test.shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001739
Georg Brandl15c5ce92007-03-07 09:09:40 +00001740class Test_TestResult(TestCase):
1741 # Note: there are not separate tests for TestResult.wasSuccessful(),
1742 # TestResult.errors, TestResult.failures, TestResult.testsRun or
1743 # TestResult.shouldStop because these only have meaning in terms of
1744 # other TestResult methods.
1745 #
1746 # Accordingly, tests for the aforenamed attributes are incorporated
1747 # in with the tests for the defining methods.
1748 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001749
Georg Brandl15c5ce92007-03-07 09:09:40 +00001750 def test_init(self):
1751 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001752
Georg Brandl15c5ce92007-03-07 09:09:40 +00001753 self.failUnless(result.wasSuccessful())
1754 self.assertEqual(len(result.errors), 0)
1755 self.assertEqual(len(result.failures), 0)
1756 self.assertEqual(result.testsRun, 0)
1757 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001758
Georg Brandl15c5ce92007-03-07 09:09:40 +00001759 # "This method can be called to signal that the set of tests being
1760 # run should be aborted by setting the TestResult's shouldStop
Tim Petersea5962f2007-03-12 18:07:52 +00001761 # attribute to True."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001762 def test_stop(self):
1763 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001764
Georg Brandl15c5ce92007-03-07 09:09:40 +00001765 result.stop()
Tim Petersea5962f2007-03-12 18:07:52 +00001766
Georg Brandl15c5ce92007-03-07 09:09:40 +00001767 self.assertEqual(result.shouldStop, True)
Tim Petersea5962f2007-03-12 18:07:52 +00001768
Georg Brandl15c5ce92007-03-07 09:09:40 +00001769 # "Called when the test case test is about to be run. The default
1770 # implementation simply increments the instance's testsRun counter."
1771 def test_startTest(self):
1772 class Foo(unittest.TestCase):
1773 def test_1(self):
1774 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001775
Georg Brandl15c5ce92007-03-07 09:09:40 +00001776 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001777
Georg Brandl15c5ce92007-03-07 09:09:40 +00001778 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001779
Georg Brandl15c5ce92007-03-07 09:09:40 +00001780 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001781
Georg Brandl15c5ce92007-03-07 09:09:40 +00001782 self.failUnless(result.wasSuccessful())
1783 self.assertEqual(len(result.errors), 0)
1784 self.assertEqual(len(result.failures), 0)
1785 self.assertEqual(result.testsRun, 1)
1786 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001787
Georg Brandl15c5ce92007-03-07 09:09:40 +00001788 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001789
Georg Brandl15c5ce92007-03-07 09:09:40 +00001790 # "Called after the test case test has been executed, regardless of
1791 # the outcome. The default implementation does nothing."
1792 def test_stopTest(self):
1793 class Foo(unittest.TestCase):
1794 def test_1(self):
1795 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001796
Georg Brandl15c5ce92007-03-07 09:09:40 +00001797 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001798
Georg Brandl15c5ce92007-03-07 09:09:40 +00001799 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001800
Georg Brandl15c5ce92007-03-07 09:09:40 +00001801 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001802
Georg Brandl15c5ce92007-03-07 09:09:40 +00001803 self.failUnless(result.wasSuccessful())
1804 self.assertEqual(len(result.errors), 0)
1805 self.assertEqual(len(result.failures), 0)
1806 self.assertEqual(result.testsRun, 1)
1807 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001808
Georg Brandl15c5ce92007-03-07 09:09:40 +00001809 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001810
Georg Brandl15c5ce92007-03-07 09:09:40 +00001811 # Same tests as above; make sure nothing has changed
1812 self.failUnless(result.wasSuccessful())
1813 self.assertEqual(len(result.errors), 0)
1814 self.assertEqual(len(result.failures), 0)
1815 self.assertEqual(result.testsRun, 1)
1816 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001817
Georg Brandl15c5ce92007-03-07 09:09:40 +00001818 # "addSuccess(test)"
1819 # ...
1820 # "Called when the test case test succeeds"
1821 # ...
1822 # "wasSuccessful() - Returns True if all tests run so far have passed,
1823 # otherwise returns False"
1824 # ...
1825 # "testsRun - The total number of tests run so far."
1826 # ...
1827 # "errors - A list containing 2-tuples of TestCase instances and
1828 # formatted tracebacks. Each tuple represents a test which raised an
1829 # unexpected exception. Contains formatted
1830 # tracebacks instead of sys.exc_info() results."
1831 # ...
1832 # "failures - A list containing 2-tuples of TestCase instances and
1833 # formatted tracebacks. Each tuple represents a test where a failure was
1834 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1835 # methods. Contains formatted tracebacks instead
1836 # of sys.exc_info() results."
1837 def test_addSuccess(self):
1838 class Foo(unittest.TestCase):
1839 def test_1(self):
1840 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001841
Georg Brandl15c5ce92007-03-07 09:09:40 +00001842 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001843
Georg Brandl15c5ce92007-03-07 09:09:40 +00001844 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001845
Georg Brandl15c5ce92007-03-07 09:09:40 +00001846 result.startTest(test)
1847 result.addSuccess(test)
1848 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001849
Georg Brandl15c5ce92007-03-07 09:09:40 +00001850 self.failUnless(result.wasSuccessful())
1851 self.assertEqual(len(result.errors), 0)
1852 self.assertEqual(len(result.failures), 0)
1853 self.assertEqual(result.testsRun, 1)
1854 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001855
Georg Brandl15c5ce92007-03-07 09:09:40 +00001856 # "addFailure(test, err)"
1857 # ...
1858 # "Called when the test case test signals a failure. err is a tuple of
1859 # the form returned by sys.exc_info(): (type, value, traceback)"
1860 # ...
1861 # "wasSuccessful() - Returns True if all tests run so far have passed,
1862 # otherwise returns False"
1863 # ...
1864 # "testsRun - The total number of tests run so far."
1865 # ...
1866 # "errors - A list containing 2-tuples of TestCase instances and
1867 # formatted tracebacks. Each tuple represents a test which raised an
1868 # unexpected exception. Contains formatted
1869 # tracebacks instead of sys.exc_info() results."
1870 # ...
1871 # "failures - A list containing 2-tuples of TestCase instances and
1872 # formatted tracebacks. Each tuple represents a test where a failure was
1873 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1874 # methods. Contains formatted tracebacks instead
1875 # of sys.exc_info() results."
1876 def test_addFailure(self):
1877 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001878
Georg Brandl15c5ce92007-03-07 09:09:40 +00001879 class Foo(unittest.TestCase):
1880 def test_1(self):
1881 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001882
Georg Brandl15c5ce92007-03-07 09:09:40 +00001883 test = Foo('test_1')
1884 try:
1885 test.fail("foo")
1886 except:
1887 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001888
Georg Brandl15c5ce92007-03-07 09:09:40 +00001889 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001890
Georg Brandl15c5ce92007-03-07 09:09:40 +00001891 result.startTest(test)
1892 result.addFailure(test, exc_info_tuple)
1893 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001894
Georg Brandl15c5ce92007-03-07 09:09:40 +00001895 self.failIf(result.wasSuccessful())
1896 self.assertEqual(len(result.errors), 0)
1897 self.assertEqual(len(result.failures), 1)
1898 self.assertEqual(result.testsRun, 1)
1899 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001900
Georg Brandl15c5ce92007-03-07 09:09:40 +00001901 test_case, formatted_exc = result.failures[0]
1902 self.failUnless(test_case is test)
1903 self.failUnless(isinstance(formatted_exc, str))
Tim Petersea5962f2007-03-12 18:07:52 +00001904
Georg Brandl15c5ce92007-03-07 09:09:40 +00001905 # "addError(test, err)"
1906 # ...
1907 # "Called when the test case test raises an unexpected exception err
1908 # is a tuple of the form returned by sys.exc_info():
1909 # (type, value, traceback)"
1910 # ...
1911 # "wasSuccessful() - Returns True if all tests run so far have passed,
1912 # otherwise returns False"
1913 # ...
1914 # "testsRun - The total number of tests run so far."
1915 # ...
1916 # "errors - A list containing 2-tuples of TestCase instances and
1917 # formatted tracebacks. Each tuple represents a test which raised an
1918 # unexpected exception. Contains formatted
1919 # tracebacks instead of sys.exc_info() results."
1920 # ...
1921 # "failures - A list containing 2-tuples of TestCase instances and
1922 # formatted tracebacks. Each tuple represents a test where a failure was
1923 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1924 # methods. Contains formatted tracebacks instead
1925 # of sys.exc_info() results."
1926 def test_addError(self):
1927 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001928
Georg Brandl15c5ce92007-03-07 09:09:40 +00001929 class Foo(unittest.TestCase):
1930 def test_1(self):
1931 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001932
Georg Brandl15c5ce92007-03-07 09:09:40 +00001933 test = Foo('test_1')
1934 try:
1935 raise TypeError()
1936 except:
1937 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001938
Georg Brandl15c5ce92007-03-07 09:09:40 +00001939 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001940
Georg Brandl15c5ce92007-03-07 09:09:40 +00001941 result.startTest(test)
1942 result.addError(test, exc_info_tuple)
1943 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001944
Georg Brandl15c5ce92007-03-07 09:09:40 +00001945 self.failIf(result.wasSuccessful())
1946 self.assertEqual(len(result.errors), 1)
1947 self.assertEqual(len(result.failures), 0)
1948 self.assertEqual(result.testsRun, 1)
1949 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001950
Georg Brandl15c5ce92007-03-07 09:09:40 +00001951 test_case, formatted_exc = result.errors[0]
1952 self.failUnless(test_case is test)
1953 self.failUnless(isinstance(formatted_exc, str))
1954
1955### Support code for Test_TestCase
1956################################################################
1957
1958class Foo(unittest.TestCase):
1959 def runTest(self): pass
1960 def test1(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001961
Georg Brandl15c5ce92007-03-07 09:09:40 +00001962class Bar(Foo):
1963 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001964
Georg Brandl15c5ce92007-03-07 09:09:40 +00001965################################################################
1966### /Support code for Test_TestCase
1967
1968class Test_TestCase(TestCase, TestEquality, TestHashing):
1969
1970 ### Set up attributes used by inherited tests
1971 ################################################################
1972
1973 # Used by TestHashing.test_hash and TestEquality.test_eq
1974 eq_pairs = [(Foo('test1'), Foo('test1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001975
Georg Brandl15c5ce92007-03-07 09:09:40 +00001976 # Used by TestEquality.test_ne
1977 ne_pairs = [(Foo('test1'), Foo('runTest'))
1978 ,(Foo('test1'), Bar('test1'))
1979 ,(Foo('test1'), Bar('test2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001980
Georg Brandl15c5ce92007-03-07 09:09:40 +00001981 ################################################################
1982 ### /Set up attributes used by inherited tests
Tim Petersea5962f2007-03-12 18:07:52 +00001983
Georg Brandl15c5ce92007-03-07 09:09:40 +00001984
1985 # "class TestCase([methodName])"
1986 # ...
1987 # "Each instance of TestCase will run a single test method: the
1988 # method named methodName."
1989 # ...
1990 # "methodName defaults to "runTest"."
1991 #
1992 # Make sure it really is optional, and that it defaults to the proper
1993 # thing.
1994 def test_init__no_test_name(self):
1995 class Test(unittest.TestCase):
1996 def runTest(self): raise MyException()
1997 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001998
Georg Brandl15c5ce92007-03-07 09:09:40 +00001999 self.assertEqual(Test().id()[-13:], '.Test.runTest')
Tim Petersea5962f2007-03-12 18:07:52 +00002000
Georg Brandl15c5ce92007-03-07 09:09:40 +00002001 # "class TestCase([methodName])"
2002 # ...
2003 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00002004 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002005 def test_init__test_name__valid(self):
2006 class Test(unittest.TestCase):
2007 def runTest(self): raise MyException()
2008 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002009
Georg Brandl15c5ce92007-03-07 09:09:40 +00002010 self.assertEqual(Test('test').id()[-10:], '.Test.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002011
Georg Brandl15c5ce92007-03-07 09:09:40 +00002012 # "class TestCase([methodName])"
2013 # ...
2014 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00002015 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002016 def test_init__test_name__invalid(self):
2017 class Test(unittest.TestCase):
2018 def runTest(self): raise MyException()
2019 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002020
Georg Brandl15c5ce92007-03-07 09:09:40 +00002021 try:
2022 Test('testfoo')
2023 except ValueError:
2024 pass
2025 else:
2026 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +00002027
Georg Brandl15c5ce92007-03-07 09:09:40 +00002028 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00002029 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002030 def test_countTestCases(self):
2031 class Foo(unittest.TestCase):
2032 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002033
Georg Brandl15c5ce92007-03-07 09:09:40 +00002034 self.assertEqual(Foo('test').countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00002035
Georg Brandl15c5ce92007-03-07 09:09:40 +00002036 # "Return the default type of test result object to be used to run this
2037 # test. For TestCase instances, this will always be
2038 # unittest.TestResult; subclasses of TestCase should
2039 # override this as necessary."
2040 def test_defaultTestResult(self):
2041 class Foo(unittest.TestCase):
2042 def runTest(self):
2043 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002044
Georg Brandl15c5ce92007-03-07 09:09:40 +00002045 result = Foo().defaultTestResult()
2046 self.assertEqual(type(result), unittest.TestResult)
2047
2048 # "When a setUp() method is defined, the test runner will run that method
2049 # prior to each test. Likewise, if a tearDown() method is defined, the
2050 # test runner will invoke that method after each test. In the example,
2051 # setUp() was used to create a fresh sequence for each test."
2052 #
2053 # Make sure the proper call order is maintained, even if setUp() raises
2054 # an exception.
2055 def test_run_call_order__error_in_setUp(self):
2056 events = []
2057 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002058
Georg Brandl15c5ce92007-03-07 09:09:40 +00002059 class Foo(unittest.TestCase):
2060 def setUp(self):
2061 events.append('setUp')
2062 raise RuntimeError('raised by Foo.setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002063
Georg Brandl15c5ce92007-03-07 09:09:40 +00002064 def test(self):
2065 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002066
Georg Brandl15c5ce92007-03-07 09:09:40 +00002067 def tearDown(self):
2068 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002069
Georg Brandl15c5ce92007-03-07 09:09:40 +00002070 Foo('test').run(result)
2071 expected = ['startTest', 'setUp', 'addError', 'stopTest']
2072 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002073
Georg Brandl15c5ce92007-03-07 09:09:40 +00002074 # "When a setUp() method is defined, the test runner will run that method
2075 # prior to each test. Likewise, if a tearDown() method is defined, the
2076 # test runner will invoke that method after each test. In the example,
2077 # setUp() was used to create a fresh sequence for each test."
2078 #
2079 # Make sure the proper call order is maintained, even if the test raises
2080 # an error (as opposed to a failure).
2081 def test_run_call_order__error_in_test(self):
2082 events = []
2083 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002084
Georg Brandl15c5ce92007-03-07 09:09:40 +00002085 class Foo(unittest.TestCase):
2086 def setUp(self):
2087 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002088
Georg Brandl15c5ce92007-03-07 09:09:40 +00002089 def test(self):
2090 events.append('test')
2091 raise RuntimeError('raised by Foo.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002092
Georg Brandl15c5ce92007-03-07 09:09:40 +00002093 def tearDown(self):
2094 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002095
Georg Brandl15c5ce92007-03-07 09:09:40 +00002096 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
2097 'stopTest']
2098 Foo('test').run(result)
2099 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002100
Georg Brandl15c5ce92007-03-07 09:09:40 +00002101 # "When a setUp() method is defined, the test runner will run that method
2102 # prior to each test. Likewise, if a tearDown() method is defined, the
2103 # test runner will invoke that method after each test. In the example,
2104 # setUp() was used to create a fresh sequence for each test."
2105 #
2106 # Make sure the proper call order is maintained, even if the test signals
2107 # a failure (as opposed to an error).
2108 def test_run_call_order__failure_in_test(self):
2109 events = []
2110 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002111
Georg Brandl15c5ce92007-03-07 09:09:40 +00002112 class Foo(unittest.TestCase):
2113 def setUp(self):
2114 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002115
Georg Brandl15c5ce92007-03-07 09:09:40 +00002116 def test(self):
2117 events.append('test')
2118 self.fail('raised by Foo.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002119
Georg Brandl15c5ce92007-03-07 09:09:40 +00002120 def tearDown(self):
2121 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002122
Georg Brandl15c5ce92007-03-07 09:09:40 +00002123 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
2124 'stopTest']
2125 Foo('test').run(result)
2126 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002127
Georg Brandl15c5ce92007-03-07 09:09:40 +00002128 # "When a setUp() method is defined, the test runner will run that method
2129 # prior to each test. Likewise, if a tearDown() method is defined, the
2130 # test runner will invoke that method after each test. In the example,
2131 # setUp() was used to create a fresh sequence for each test."
2132 #
2133 # Make sure the proper call order is maintained, even if tearDown() raises
2134 # an exception.
2135 def test_run_call_order__error_in_tearDown(self):
2136 events = []
2137 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002138
Georg Brandl15c5ce92007-03-07 09:09:40 +00002139 class Foo(unittest.TestCase):
2140 def setUp(self):
2141 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002142
Georg Brandl15c5ce92007-03-07 09:09:40 +00002143 def test(self):
2144 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002145
Georg Brandl15c5ce92007-03-07 09:09:40 +00002146 def tearDown(self):
2147 events.append('tearDown')
2148 raise RuntimeError('raised by Foo.tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002149
Georg Brandl15c5ce92007-03-07 09:09:40 +00002150 Foo('test').run(result)
2151 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
2152 'stopTest']
2153 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002154
Georg Brandl15c5ce92007-03-07 09:09:40 +00002155 # "This class attribute gives the exception raised by the test() method.
2156 # If a test framework needs to use a specialized exception, possibly to
2157 # carry additional information, it must subclass this exception in
2158 # order to ``play fair'' with the framework. The initial value of this
2159 # attribute is AssertionError"
2160 def test_failureException__default(self):
2161 class Foo(unittest.TestCase):
2162 def test(self):
2163 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002164
Georg Brandl15c5ce92007-03-07 09:09:40 +00002165 self.failUnless(Foo('test').failureException is AssertionError)
Tim Petersea5962f2007-03-12 18:07:52 +00002166
Georg Brandl15c5ce92007-03-07 09:09:40 +00002167 # "This class attribute gives the exception raised by the test() method.
2168 # If a test framework needs to use a specialized exception, possibly to
2169 # carry additional information, it must subclass this exception in
2170 # order to ``play fair'' with the framework."
2171 #
2172 # Make sure TestCase.run() respects the designated failureException
2173 def test_failureException__subclassing__explicit_raise(self):
2174 events = []
2175 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002176
Georg Brandl15c5ce92007-03-07 09:09:40 +00002177 class Foo(unittest.TestCase):
2178 def test(self):
2179 raise RuntimeError()
Tim Petersea5962f2007-03-12 18:07:52 +00002180
Georg Brandl15c5ce92007-03-07 09:09:40 +00002181 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002182
Georg Brandl15c5ce92007-03-07 09:09:40 +00002183 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002184
2185
Georg Brandl15c5ce92007-03-07 09:09:40 +00002186 Foo('test').run(result)
2187 expected = ['startTest', 'addFailure', 'stopTest']
2188 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002189
Georg Brandl15c5ce92007-03-07 09:09:40 +00002190 # "This class attribute gives the exception raised by the test() method.
2191 # If a test framework needs to use a specialized exception, possibly to
2192 # carry additional information, it must subclass this exception in
2193 # order to ``play fair'' with the framework."
2194 #
2195 # Make sure TestCase.run() respects the designated failureException
2196 def test_failureException__subclassing__implicit_raise(self):
2197 events = []
2198 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002199
Georg Brandl15c5ce92007-03-07 09:09:40 +00002200 class Foo(unittest.TestCase):
2201 def test(self):
2202 self.fail("foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002203
Georg Brandl15c5ce92007-03-07 09:09:40 +00002204 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002205
Georg Brandl15c5ce92007-03-07 09:09:40 +00002206 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002207
2208
Georg Brandl15c5ce92007-03-07 09:09:40 +00002209 Foo('test').run(result)
2210 expected = ['startTest', 'addFailure', 'stopTest']
2211 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002212
2213 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002214 def test_setUp(self):
2215 class Foo(unittest.TestCase):
2216 def runTest(self):
2217 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002218
Georg Brandl15c5ce92007-03-07 09:09:40 +00002219 # ... and nothing should happen
2220 Foo().setUp()
Tim Petersea5962f2007-03-12 18:07:52 +00002221
2222 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002223 def test_tearDown(self):
2224 class Foo(unittest.TestCase):
2225 def runTest(self):
2226 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002227
Georg Brandl15c5ce92007-03-07 09:09:40 +00002228 # ... and nothing should happen
2229 Foo().tearDown()
Tim Petersea5962f2007-03-12 18:07:52 +00002230
Georg Brandl15c5ce92007-03-07 09:09:40 +00002231 # "Return a string identifying the specific test case."
2232 #
2233 # Because of the vague nature of the docs, I'm not going to lock this
2234 # test down too much. Really all that can be asserted is that the id()
2235 # will be a string (either 8-byte or unicode -- again, because the docs
2236 # just say "string")
2237 def test_id(self):
2238 class Foo(unittest.TestCase):
2239 def runTest(self):
2240 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002241
Georg Brandl15c5ce92007-03-07 09:09:40 +00002242 self.failUnless(isinstance(Foo().id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00002243
Georg Brandl15c5ce92007-03-07 09:09:40 +00002244 # "Returns a one-line description of the test, or None if no description
2245 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002246 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002247 def test_shortDescription__no_docstring(self):
2248 class Foo(unittest.TestCase):
2249 def runTest(self):
2250 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002251
Georg Brandl15c5ce92007-03-07 09:09:40 +00002252 self.assertEqual(Foo().shortDescription(), None)
Tim Petersea5962f2007-03-12 18:07:52 +00002253
Georg Brandl15c5ce92007-03-07 09:09:40 +00002254 # "Returns a one-line description of the test, or None if no description
2255 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002256 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002257 def test_shortDescription__singleline_docstring(self):
2258 class Foo(unittest.TestCase):
2259 def runTest(self):
2260 "this tests foo"
2261 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002262
Georg Brandl15c5ce92007-03-07 09:09:40 +00002263 self.assertEqual(Foo().shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002264
Georg Brandl15c5ce92007-03-07 09:09:40 +00002265 # "Returns a one-line description of the test, or None if no description
2266 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002267 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002268 def test_shortDescription__multiline_docstring(self):
2269 class Foo(unittest.TestCase):
2270 def runTest(self):
2271 """this tests foo
2272 blah, bar and baz are also tested"""
2273 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002274
Georg Brandl15c5ce92007-03-07 09:09:40 +00002275 self.assertEqual(Foo().shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002276
Georg Brandl15c5ce92007-03-07 09:09:40 +00002277 # "If result is omitted or None, a temporary result object is created
Tim Petersea5962f2007-03-12 18:07:52 +00002278 # and used, but is not made available to the caller"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002279 def test_run__uses_defaultTestResult(self):
2280 events = []
Tim Petersea5962f2007-03-12 18:07:52 +00002281
Georg Brandl15c5ce92007-03-07 09:09:40 +00002282 class Foo(unittest.TestCase):
2283 def test(self):
2284 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002285
Georg Brandl15c5ce92007-03-07 09:09:40 +00002286 def defaultTestResult(self):
2287 return LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002288
2289 # Make run() find a result object on its own
Georg Brandl15c5ce92007-03-07 09:09:40 +00002290 Foo('test').run()
Tim Petersea5962f2007-03-12 18:07:52 +00002291
Georg Brandl15c5ce92007-03-07 09:09:40 +00002292 expected = ['startTest', 'test', 'stopTest']
2293 self.assertEqual(events, expected)
Jim Fultonfafd8742004-08-28 15:22:12 +00002294
2295######################################################################
2296## Main
2297######################################################################
2298
2299def test_main():
Georg Brandl15c5ce92007-03-07 09:09:40 +00002300 test_support.run_unittest(Test_TestCase, Test_TestLoader,
2301 Test_TestSuite, Test_TestResult, Test_FunctionTestCase)
Jim Fultonfafd8742004-08-28 15:22:12 +00002302
Georg Brandl15c5ce92007-03-07 09:09:40 +00002303if __name__ == "__main__":
Tim Petersea5962f2007-03-12 18:07:52 +00002304 test_main()