blob: 5626542c65eb425af9507e251a200de64e6bfa71 [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
Michael Foordbee87b42009-07-13 16:32:47 +00009import sys
Georg Brandl15c5ce92007-03-07 09:09:40 +000010from test import test_support
Jim Fultonfafd8742004-08-28 15:22:12 +000011import unittest
Georg Brandl15c5ce92007-03-07 09:09:40 +000012from unittest import TestCase
Christian Heimesc756d002007-11-27 21:34:01 +000013import types
Jim Fultonfafd8742004-08-28 15:22:12 +000014
Georg Brandl15c5ce92007-03-07 09:09:40 +000015### Support code
16################################################################
Jim Fultonfafd8742004-08-28 15:22:12 +000017
Georg Brandl15c5ce92007-03-07 09:09:40 +000018class LoggingResult(unittest.TestResult):
19 def __init__(self, log):
20 self._events = log
21 super(LoggingResult, self).__init__()
22
23 def startTest(self, test):
24 self._events.append('startTest')
25 super(LoggingResult, self).startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +000026
Georg Brandl15c5ce92007-03-07 09:09:40 +000027 def stopTest(self, test):
28 self._events.append('stopTest')
29 super(LoggingResult, self).stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +000030
Georg Brandl15c5ce92007-03-07 09:09:40 +000031 def addFailure(self, *args):
32 self._events.append('addFailure')
33 super(LoggingResult, self).addFailure(*args)
Tim Petersea5962f2007-03-12 18:07:52 +000034
Georg Brandl15c5ce92007-03-07 09:09:40 +000035 def addError(self, *args):
36 self._events.append('addError')
37 super(LoggingResult, self).addError(*args)
38
39class TestEquality(object):
Tim Petersea5962f2007-03-12 18:07:52 +000040 # Check for a valid __eq__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000041 def test_eq(self):
42 for obj_1, obj_2 in self.eq_pairs:
43 self.assertEqual(obj_1, obj_2)
44 self.assertEqual(obj_2, obj_1)
Tim Petersea5962f2007-03-12 18:07:52 +000045
46 # Check for a valid __ne__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000047 def test_ne(self):
48 for obj_1, obj_2 in self.ne_pairs:
49 self.failIfEqual(obj_1, obj_2)
50 self.failIfEqual(obj_2, obj_1)
Tim Petersea5962f2007-03-12 18:07:52 +000051
Georg Brandl15c5ce92007-03-07 09:09:40 +000052class TestHashing(object):
Tim Petersea5962f2007-03-12 18:07:52 +000053 # Check for a valid __hash__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000054 def test_hash(self):
55 for obj_1, obj_2 in self.eq_pairs:
56 try:
57 assert hash(obj_1) == hash(obj_2)
58 except KeyboardInterrupt:
59 raise
60 except AssertionError:
61 self.fail("%s and %s do not hash equal" % (obj_1, obj_2))
62 except Exception, e:
63 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
Tim Petersea5962f2007-03-12 18:07:52 +000064
Georg Brandl15c5ce92007-03-07 09:09:40 +000065 for obj_1, obj_2 in self.ne_pairs:
66 try:
67 assert hash(obj_1) != hash(obj_2)
68 except KeyboardInterrupt:
69 raise
70 except AssertionError:
71 self.fail("%s and %s hash equal, but shouldn't" % (obj_1, obj_2))
72 except Exception, e:
73 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
Tim Petersea5962f2007-03-12 18:07:52 +000074
Georg Brandl15c5ce92007-03-07 09:09:40 +000075
76################################################################
77### /Support code
78
79class Test_TestLoader(TestCase):
80
81 ### Tests for TestLoader.loadTestsFromTestCase
82 ################################################################
83
84 # "Return a suite of all tests cases contained in the TestCase-derived
85 # class testCaseClass"
86 def test_loadTestsFromTestCase(self):
87 class Foo(unittest.TestCase):
88 def test_1(self): pass
89 def test_2(self): pass
90 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +000091
Georg Brandl15c5ce92007-03-07 09:09:40 +000092 tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +000093
Georg Brandl15c5ce92007-03-07 09:09:40 +000094 loader = unittest.TestLoader()
95 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +000096
Georg Brandl15c5ce92007-03-07 09:09:40 +000097 # "Return a suite of all tests cases contained in the TestCase-derived
98 # class testCaseClass"
99 #
Tim Petersea5962f2007-03-12 18:07:52 +0000100 # Make sure it does the right thing even if no tests were found
Georg Brandl15c5ce92007-03-07 09:09:40 +0000101 def test_loadTestsFromTestCase__no_matches(self):
102 class Foo(unittest.TestCase):
103 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000104
Georg Brandl15c5ce92007-03-07 09:09:40 +0000105 empty_suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +0000106
Georg Brandl15c5ce92007-03-07 09:09:40 +0000107 loader = unittest.TestLoader()
108 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
Tim Petersea5962f2007-03-12 18:07:52 +0000109
Georg Brandl15c5ce92007-03-07 09:09:40 +0000110 # "Return a suite of all tests cases contained in the TestCase-derived
111 # class testCaseClass"
112 #
113 # What happens if loadTestsFromTestCase() is given an object
114 # that isn't a subclass of TestCase? Specifically, what happens
115 # if testCaseClass is a subclass of TestSuite?
116 #
117 # This is checked for specifically in the code, so we better add a
118 # test for it.
119 def test_loadTestsFromTestCase__TestSuite_subclass(self):
120 class NotATestCase(unittest.TestSuite):
121 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000122
Georg Brandl15c5ce92007-03-07 09:09:40 +0000123 loader = unittest.TestLoader()
124 try:
125 loader.loadTestsFromTestCase(NotATestCase)
126 except TypeError:
127 pass
128 else:
129 self.fail('Should raise TypeError')
Tim Petersea5962f2007-03-12 18:07:52 +0000130
Georg Brandl15c5ce92007-03-07 09:09:40 +0000131 # "Return a suite of all tests cases contained in the TestCase-derived
132 # class testCaseClass"
133 #
134 # Make sure loadTestsFromTestCase() picks up the default test method
135 # name (as specified by TestCase), even though the method name does
136 # not match the default TestLoader.testMethodPrefix string
137 def test_loadTestsFromTestCase__default_method_name(self):
138 class Foo(unittest.TestCase):
139 def runTest(self):
140 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000141
Georg Brandl15c5ce92007-03-07 09:09:40 +0000142 loader = unittest.TestLoader()
143 # This has to be false for the test to succeed
144 self.failIf('runTest'.startswith(loader.testMethodPrefix))
Tim Petersea5962f2007-03-12 18:07:52 +0000145
Georg Brandl15c5ce92007-03-07 09:09:40 +0000146 suite = loader.loadTestsFromTestCase(Foo)
147 self.failUnless(isinstance(suite, loader.suiteClass))
148 self.assertEqual(list(suite), [Foo('runTest')])
149
150 ################################################################
151 ### /Tests for TestLoader.loadTestsFromTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000152
Georg Brandl15c5ce92007-03-07 09:09:40 +0000153 ### Tests for TestLoader.loadTestsFromModule
154 ################################################################
155
156 # "This method searches `module` for classes derived from TestCase"
157 def test_loadTestsFromModule__TestCase_subclass(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000158 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000159 class MyTestCase(unittest.TestCase):
160 def test(self):
161 pass
162 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000163
Georg Brandl15c5ce92007-03-07 09:09:40 +0000164 loader = unittest.TestLoader()
165 suite = loader.loadTestsFromModule(m)
166 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000167
Georg Brandl15c5ce92007-03-07 09:09:40 +0000168 expected = [loader.suiteClass([MyTestCase('test')])]
169 self.assertEqual(list(suite), expected)
Tim Petersea5962f2007-03-12 18:07:52 +0000170
Georg Brandl15c5ce92007-03-07 09:09:40 +0000171 # "This method searches `module` for classes derived from TestCase"
Tim Petersea5962f2007-03-12 18:07:52 +0000172 #
Georg Brandl15c5ce92007-03-07 09:09:40 +0000173 # What happens if no tests are found (no TestCase instances)?
174 def test_loadTestsFromModule__no_TestCase_instances(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000175 m = types.ModuleType('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):
Christian Heimesc756d002007-11-27 21:34:01 +0000186 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000187 class MyTestCase(unittest.TestCase):
188 pass
189 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000190
Georg Brandl15c5ce92007-03-07 09:09:40 +0000191 loader = unittest.TestLoader()
192 suite = loader.loadTestsFromModule(m)
193 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000194
Georg Brandl15c5ce92007-03-07 09:09:40 +0000195 self.assertEqual(list(suite), [loader.suiteClass()])
Tim Petersea5962f2007-03-12 18:07:52 +0000196
Georg Brandl15c5ce92007-03-07 09:09:40 +0000197 # "This method searches `module` for classes derived from TestCase"s
198 #
199 # What happens if loadTestsFromModule() is given something other
200 # than a module?
201 #
202 # XXX Currently, it succeeds anyway. This flexibility
203 # should either be documented or loadTestsFromModule() should
204 # raise a TypeError
205 #
206 # XXX Certain people are using this behaviour. We'll add a test for it
207 def test_loadTestsFromModule__not_a_module(self):
208 class MyTestCase(unittest.TestCase):
209 def test(self):
210 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000211
Georg Brandl15c5ce92007-03-07 09:09:40 +0000212 class NotAModule(object):
213 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000214
Georg Brandl15c5ce92007-03-07 09:09:40 +0000215 loader = unittest.TestLoader()
216 suite = loader.loadTestsFromModule(NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000217
Georg Brandl15c5ce92007-03-07 09:09:40 +0000218 reference = [unittest.TestSuite([MyTestCase('test')])]
219 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000220
Georg Brandl15c5ce92007-03-07 09:09:40 +0000221 ################################################################
222 ### /Tests for TestLoader.loadTestsFromModule()
Tim Petersea5962f2007-03-12 18:07:52 +0000223
Georg Brandl15c5ce92007-03-07 09:09:40 +0000224 ### Tests for TestLoader.loadTestsFromName()
225 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000226
Georg Brandl15c5ce92007-03-07 09:09:40 +0000227 # "The specifier name is a ``dotted name'' that may resolve either to
228 # a module, a test case class, a TestSuite instance, a test method
229 # within a test case class, or a callable object which returns a
230 # TestCase or TestSuite instance."
231 #
232 # Is ValueError raised in response to an empty name?
233 def test_loadTestsFromName__empty_name(self):
234 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000235
Georg Brandl15c5ce92007-03-07 09:09:40 +0000236 try:
237 loader.loadTestsFromName('')
238 except ValueError, e:
239 self.assertEqual(str(e), "Empty module name")
240 else:
241 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000242
Georg Brandl15c5ce92007-03-07 09:09:40 +0000243 # "The specifier name is a ``dotted name'' that may resolve either to
244 # a module, a test case class, a TestSuite instance, a test method
245 # within a test case class, or a callable object which returns a
246 # TestCase or TestSuite instance."
247 #
Tim Petersea5962f2007-03-12 18:07:52 +0000248 # What happens when the name contains invalid characters?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000249 def test_loadTestsFromName__malformed_name(self):
250 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000251
Georg Brandl15c5ce92007-03-07 09:09:40 +0000252 # XXX Should this raise ValueError or ImportError?
253 try:
254 loader.loadTestsFromName('abc () //')
255 except ValueError:
256 pass
257 except ImportError:
258 pass
259 else:
260 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000261
Georg Brandl15c5ce92007-03-07 09:09:40 +0000262 # "The specifier name is a ``dotted name'' that may resolve ... to a
263 # module"
264 #
Tim Petersea5962f2007-03-12 18:07:52 +0000265 # What happens when a module by that name can't be found?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000266 def test_loadTestsFromName__unknown_module_name(self):
267 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000268
Georg Brandl15c5ce92007-03-07 09:09:40 +0000269 try:
270 loader.loadTestsFromName('sdasfasfasdf')
271 except ImportError, e:
272 self.assertEqual(str(e), "No module named sdasfasfasdf")
273 else:
274 self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
Tim Petersea5962f2007-03-12 18:07:52 +0000275
Georg Brandl15c5ce92007-03-07 09:09:40 +0000276 # "The specifier name is a ``dotted name'' that may resolve either to
277 # a module, a test case class, a TestSuite instance, a test method
278 # within a test case class, or a callable object which returns a
279 # TestCase or TestSuite instance."
280 #
Tim Petersea5962f2007-03-12 18:07:52 +0000281 # What happens when the module is found, but the attribute can't?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000282 def test_loadTestsFromName__unknown_attr_name(self):
283 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000284
Georg Brandl15c5ce92007-03-07 09:09:40 +0000285 try:
286 loader.loadTestsFromName('unittest.sdasfasfasdf')
287 except AttributeError, e:
288 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
289 else:
290 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000291
Georg Brandl15c5ce92007-03-07 09:09:40 +0000292 # "The specifier name is a ``dotted name'' that may resolve either to
293 # a module, a test case class, a TestSuite instance, a test method
294 # within a test case class, or a callable object which returns a
295 # TestCase or TestSuite instance."
296 #
297 # What happens when we provide the module, but the attribute can't be
298 # found?
299 def test_loadTestsFromName__relative_unknown_name(self):
300 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000301
Georg Brandl15c5ce92007-03-07 09:09:40 +0000302 try:
303 loader.loadTestsFromName('sdasfasfasdf', unittest)
304 except AttributeError, e:
305 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
306 else:
307 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000308
Georg Brandl15c5ce92007-03-07 09:09:40 +0000309 # "The specifier name is a ``dotted name'' that may resolve either to
310 # a module, a test case class, a TestSuite instance, a test method
311 # within a test case class, or a callable object which returns a
312 # TestCase or TestSuite instance."
313 # ...
314 # "The method optionally resolves name relative to the given module"
315 #
316 # Does loadTestsFromName raise ValueError when passed an empty
317 # name relative to a provided module?
318 #
319 # XXX Should probably raise a ValueError instead of an AttributeError
320 def test_loadTestsFromName__relative_empty_name(self):
321 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000322
Georg Brandl15c5ce92007-03-07 09:09:40 +0000323 try:
324 loader.loadTestsFromName('', unittest)
325 except AttributeError, e:
326 pass
327 else:
328 self.fail("Failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000329
Georg Brandl15c5ce92007-03-07 09:09:40 +0000330 # "The specifier name is a ``dotted name'' that may resolve either to
331 # a module, a test case class, a TestSuite instance, a test method
332 # within a test case class, or a callable object which returns a
333 # TestCase or TestSuite instance."
334 # ...
335 # "The method optionally resolves name relative to the given module"
336 #
337 # What happens when an impossible name is given, relative to the provided
338 # `module`?
339 def test_loadTestsFromName__relative_malformed_name(self):
340 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000341
Georg Brandl15c5ce92007-03-07 09:09:40 +0000342 # XXX Should this raise AttributeError or ValueError?
343 try:
344 loader.loadTestsFromName('abc () //', unittest)
345 except ValueError:
346 pass
347 except AttributeError:
348 pass
349 else:
350 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
351
352 # "The method optionally resolves name relative to the given module"
353 #
354 # Does loadTestsFromName raise TypeError when the `module` argument
355 # isn't a module object?
356 #
357 # XXX Accepts the not-a-module object, ignorning the object's type
358 # This should raise an exception or the method name should be changed
359 #
360 # XXX Some people are relying on this, so keep it for now
361 def test_loadTestsFromName__relative_not_a_module(self):
362 class MyTestCase(unittest.TestCase):
363 def test(self):
364 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000365
Georg Brandl15c5ce92007-03-07 09:09:40 +0000366 class NotAModule(object):
367 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000368
Georg Brandl15c5ce92007-03-07 09:09:40 +0000369 loader = unittest.TestLoader()
370 suite = loader.loadTestsFromName('test_2', NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000371
Georg Brandl15c5ce92007-03-07 09:09:40 +0000372 reference = [MyTestCase('test')]
373 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000374
Georg Brandl15c5ce92007-03-07 09:09:40 +0000375 # "The specifier name is a ``dotted name'' that may resolve either to
376 # a module, a test case class, a TestSuite instance, a test method
377 # within a test case class, or a callable object which returns a
378 # TestCase or TestSuite instance."
379 #
380 # Does it raise an exception if the name resolves to an invalid
381 # object?
382 def test_loadTestsFromName__relative_bad_object(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000383 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000384 m.testcase_1 = object()
Tim Petersea5962f2007-03-12 18:07:52 +0000385
Georg Brandl15c5ce92007-03-07 09:09:40 +0000386 loader = unittest.TestLoader()
387 try:
388 loader.loadTestsFromName('testcase_1', m)
389 except TypeError:
390 pass
391 else:
392 self.fail("Should have raised TypeError")
393
394 # "The specifier name is a ``dotted name'' that may
395 # resolve either to ... a test case class"
396 def test_loadTestsFromName__relative_TestCase_subclass(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000397 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000398 class MyTestCase(unittest.TestCase):
399 def test(self):
400 pass
401 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000402
Georg Brandl15c5ce92007-03-07 09:09:40 +0000403 loader = unittest.TestLoader()
404 suite = loader.loadTestsFromName('testcase_1', m)
405 self.failUnless(isinstance(suite, loader.suiteClass))
406 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000407
Georg Brandl15c5ce92007-03-07 09:09:40 +0000408 # "The specifier name is a ``dotted name'' that may resolve either to
409 # a module, a test case class, a TestSuite instance, a test method
410 # within a test case class, or a callable object which returns a
411 # TestCase or TestSuite instance."
412 def test_loadTestsFromName__relative_TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000413 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000414 class MyTestCase(unittest.TestCase):
415 def test(self):
416 pass
417 m.testsuite = unittest.TestSuite([MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000418
Georg Brandl15c5ce92007-03-07 09:09:40 +0000419 loader = unittest.TestLoader()
420 suite = loader.loadTestsFromName('testsuite', m)
421 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000422
Georg Brandl15c5ce92007-03-07 09:09:40 +0000423 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000424
Georg Brandl15c5ce92007-03-07 09:09:40 +0000425 # "The specifier name is a ``dotted name'' that may resolve ... to
426 # ... a test method within a test case class"
427 def test_loadTestsFromName__relative_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000428 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000429 class MyTestCase(unittest.TestCase):
430 def test(self):
431 pass
432 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000433
Georg Brandl15c5ce92007-03-07 09:09:40 +0000434 loader = unittest.TestLoader()
435 suite = loader.loadTestsFromName('testcase_1.test', m)
436 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000437
Georg Brandl15c5ce92007-03-07 09:09:40 +0000438 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000439
Georg Brandl15c5ce92007-03-07 09:09:40 +0000440 # "The specifier name is a ``dotted name'' that may resolve either to
441 # a module, a test case class, a TestSuite instance, a test method
442 # within a test case class, or a callable object which returns a
443 # TestCase or TestSuite instance."
444 #
445 # Does loadTestsFromName() raise the proper exception when trying to
446 # resolve "a test method within a test case class" that doesn't exist
447 # for the given name (relative to a provided module)?
448 def test_loadTestsFromName__relative_invalid_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000449 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000450 class MyTestCase(unittest.TestCase):
451 def test(self):
452 pass
453 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000454
Georg Brandl15c5ce92007-03-07 09:09:40 +0000455 loader = unittest.TestLoader()
456 try:
457 loader.loadTestsFromName('testcase_1.testfoo', m)
458 except AttributeError, e:
459 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
460 else:
461 self.fail("Failed to raise AttributeError")
462
463 # "The specifier name is a ``dotted name'' that may resolve ... to
464 # ... a callable object which returns a ... TestSuite instance"
465 def test_loadTestsFromName__callable__TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000466 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000467 testcase_1 = unittest.FunctionTestCase(lambda: None)
468 testcase_2 = unittest.FunctionTestCase(lambda: None)
469 def return_TestSuite():
470 return unittest.TestSuite([testcase_1, testcase_2])
471 m.return_TestSuite = return_TestSuite
Tim Petersea5962f2007-03-12 18:07:52 +0000472
Georg Brandl15c5ce92007-03-07 09:09:40 +0000473 loader = unittest.TestLoader()
474 suite = loader.loadTestsFromName('return_TestSuite', m)
475 self.failUnless(isinstance(suite, loader.suiteClass))
476 self.assertEqual(list(suite), [testcase_1, testcase_2])
Tim Petersea5962f2007-03-12 18:07:52 +0000477
Georg Brandl15c5ce92007-03-07 09:09:40 +0000478 # "The specifier name is a ``dotted name'' that may resolve ... to
479 # ... a callable object which returns a TestCase ... instance"
480 def test_loadTestsFromName__callable__TestCase_instance(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000481 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000482 testcase_1 = unittest.FunctionTestCase(lambda: None)
483 def return_TestCase():
484 return testcase_1
485 m.return_TestCase = return_TestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000486
Georg Brandl15c5ce92007-03-07 09:09:40 +0000487 loader = unittest.TestLoader()
488 suite = loader.loadTestsFromName('return_TestCase', m)
489 self.failUnless(isinstance(suite, loader.suiteClass))
490 self.assertEqual(list(suite), [testcase_1])
Tim Petersea5962f2007-03-12 18:07:52 +0000491
Georg Brandl15c5ce92007-03-07 09:09:40 +0000492 # "The specifier name is a ``dotted name'' that may resolve ... to
493 # ... a callable object which returns a TestCase or TestSuite instance"
494 #
495 # What happens if the callable returns something else?
496 def test_loadTestsFromName__callable__wrong_type(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000497 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000498 def return_wrong():
499 return 6
500 m.return_wrong = return_wrong
Tim Petersea5962f2007-03-12 18:07:52 +0000501
Georg Brandl15c5ce92007-03-07 09:09:40 +0000502 loader = unittest.TestLoader()
503 try:
504 suite = loader.loadTestsFromName('return_wrong', m)
505 except TypeError:
506 pass
507 else:
508 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000509
Georg Brandl15c5ce92007-03-07 09:09:40 +0000510 # "The specifier can refer to modules and packages which have not been
Tim Petersea5962f2007-03-12 18:07:52 +0000511 # imported; they will be imported as a side-effect"
Georg Brandl15c5ce92007-03-07 09:09:40 +0000512 def test_loadTestsFromName__module_not_loaded(self):
513 # We're going to try to load this module as a side-effect, so it
514 # better not be loaded before we try.
515 #
516 # Why pick audioop? Google shows it isn't used very often, so there's
517 # a good chance that it won't be imported when this test is run
518 module_name = 'audioop'
Tim Petersea5962f2007-03-12 18:07:52 +0000519
Georg Brandl15c5ce92007-03-07 09:09:40 +0000520 import sys
521 if module_name in sys.modules:
522 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000523
Georg Brandl15c5ce92007-03-07 09:09:40 +0000524 loader = unittest.TestLoader()
525 try:
526 suite = loader.loadTestsFromName(module_name)
527
528 self.failUnless(isinstance(suite, loader.suiteClass))
529 self.assertEqual(list(suite), [])
530
531 # audioop should now be loaded, thanks to loadTestsFromName()
532 self.failUnless(module_name in sys.modules)
533 finally:
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +0000534 if module_name in sys.modules:
535 del sys.modules[module_name]
Georg Brandl15c5ce92007-03-07 09:09:40 +0000536
537 ################################################################
538 ### Tests for TestLoader.loadTestsFromName()
539
540 ### Tests for TestLoader.loadTestsFromNames()
541 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000542
Georg Brandl15c5ce92007-03-07 09:09:40 +0000543 # "Similar to loadTestsFromName(), but takes a sequence of names rather
544 # than a single name."
545 #
546 # What happens if that sequence of names is empty?
547 def test_loadTestsFromNames__empty_name_list(self):
548 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000549
Georg Brandl15c5ce92007-03-07 09:09:40 +0000550 suite = loader.loadTestsFromNames([])
551 self.failUnless(isinstance(suite, loader.suiteClass))
552 self.assertEqual(list(suite), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000553
Georg Brandl15c5ce92007-03-07 09:09:40 +0000554 # "Similar to loadTestsFromName(), but takes a sequence of names rather
555 # than a single name."
556 # ...
557 # "The method optionally resolves name relative to the given module"
558 #
559 # What happens if that sequence of names is empty?
560 #
561 # XXX Should this raise a ValueError or just return an empty TestSuite?
562 def test_loadTestsFromNames__relative_empty_name_list(self):
563 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000564
Georg Brandl15c5ce92007-03-07 09:09:40 +0000565 suite = loader.loadTestsFromNames([], unittest)
566 self.failUnless(isinstance(suite, loader.suiteClass))
567 self.assertEqual(list(suite), [])
568
569 # "The specifier name is a ``dotted name'' that may resolve either to
570 # a module, a test case class, a TestSuite instance, a test method
571 # within a test case class, or a callable object which returns a
572 # TestCase or TestSuite instance."
573 #
574 # Is ValueError raised in response to an empty name?
575 def test_loadTestsFromNames__empty_name(self):
576 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000577
Georg Brandl15c5ce92007-03-07 09:09:40 +0000578 try:
579 loader.loadTestsFromNames([''])
580 except ValueError, e:
581 self.assertEqual(str(e), "Empty module name")
582 else:
583 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000584
Georg Brandl15c5ce92007-03-07 09:09:40 +0000585 # "The specifier name is a ``dotted name'' that may resolve either to
586 # a module, a test case class, a TestSuite instance, a test method
587 # within a test case class, or a callable object which returns a
588 # TestCase or TestSuite instance."
589 #
Tim Petersea5962f2007-03-12 18:07:52 +0000590 # What happens when presented with an impossible module name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000591 def test_loadTestsFromNames__malformed_name(self):
592 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000593
Georg Brandl15c5ce92007-03-07 09:09:40 +0000594 # XXX Should this raise ValueError or ImportError?
595 try:
596 loader.loadTestsFromNames(['abc () //'])
597 except ValueError:
598 pass
599 except ImportError:
600 pass
601 else:
602 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000603
Georg Brandl15c5ce92007-03-07 09:09:40 +0000604 # "The specifier name is a ``dotted name'' that may resolve either to
605 # a module, a test case class, a TestSuite instance, a test method
606 # within a test case class, or a callable object which returns a
607 # TestCase or TestSuite instance."
608 #
Tim Petersea5962f2007-03-12 18:07:52 +0000609 # What happens when no module can be found for the given name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000610 def test_loadTestsFromNames__unknown_module_name(self):
611 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000612
Georg Brandl15c5ce92007-03-07 09:09:40 +0000613 try:
614 loader.loadTestsFromNames(['sdasfasfasdf'])
615 except ImportError, e:
616 self.assertEqual(str(e), "No module named sdasfasfasdf")
617 else:
618 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
Tim Petersea5962f2007-03-12 18:07:52 +0000619
Georg Brandl15c5ce92007-03-07 09:09:40 +0000620 # "The specifier name is a ``dotted name'' that may resolve either to
621 # a module, a test case class, a TestSuite instance, a test method
622 # within a test case class, or a callable object which returns a
623 # TestCase or TestSuite instance."
624 #
Tim Petersea5962f2007-03-12 18:07:52 +0000625 # What happens when the module can be found, but not the attribute?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000626 def test_loadTestsFromNames__unknown_attr_name(self):
627 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000628
Georg Brandl15c5ce92007-03-07 09:09:40 +0000629 try:
630 loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
631 except AttributeError, e:
632 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
633 else:
634 self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000635
Georg Brandl15c5ce92007-03-07 09:09:40 +0000636 # "The specifier name is a ``dotted name'' that may resolve either to
637 # a module, a test case class, a TestSuite instance, a test method
638 # within a test case class, or a callable object which returns a
639 # TestCase or TestSuite instance."
640 # ...
641 # "The method optionally resolves name relative to the given module"
642 #
643 # What happens when given an unknown attribute on a specified `module`
644 # argument?
645 def test_loadTestsFromNames__unknown_name_relative_1(self):
646 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000647
Georg Brandl15c5ce92007-03-07 09:09:40 +0000648 try:
649 loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
650 except AttributeError, e:
651 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
652 else:
653 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000654
Georg Brandl15c5ce92007-03-07 09:09:40 +0000655 # "The specifier name is a ``dotted name'' that may resolve either to
656 # a module, a test case class, a TestSuite instance, a test method
657 # within a test case class, or a callable object which returns a
658 # TestCase or TestSuite instance."
659 # ...
660 # "The method optionally resolves name relative to the given module"
661 #
662 # Do unknown attributes (relative to a provided module) still raise an
Tim Petersea5962f2007-03-12 18:07:52 +0000663 # exception even in the presence of valid attribute names?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000664 def test_loadTestsFromNames__unknown_name_relative_2(self):
665 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000666
Georg Brandl15c5ce92007-03-07 09:09:40 +0000667 try:
668 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
669 except AttributeError, e:
670 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
671 else:
672 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
673
674 # "The specifier name is a ``dotted name'' that may resolve either to
675 # a module, a test case class, a TestSuite instance, a test method
676 # within a test case class, or a callable object which returns a
677 # TestCase or TestSuite instance."
678 # ...
679 # "The method optionally resolves name relative to the given module"
680 #
681 # What happens when faced with the empty string?
682 #
683 # XXX This currently raises AttributeError, though ValueError is probably
684 # more appropriate
685 def test_loadTestsFromNames__relative_empty_name(self):
686 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000687
Georg Brandl15c5ce92007-03-07 09:09:40 +0000688 try:
689 loader.loadTestsFromNames([''], unittest)
690 except AttributeError:
691 pass
692 else:
693 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000694
Georg Brandl15c5ce92007-03-07 09:09:40 +0000695 # "The specifier name is a ``dotted name'' that may resolve either to
696 # a module, a test case class, a TestSuite instance, a test method
697 # within a test case class, or a callable object which returns a
698 # TestCase or TestSuite instance."
699 # ...
700 # "The method optionally resolves name relative to the given module"
701 #
Tim Petersea5962f2007-03-12 18:07:52 +0000702 # What happens when presented with an impossible attribute name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000703 def test_loadTestsFromNames__relative_malformed_name(self):
704 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000705
Georg Brandl15c5ce92007-03-07 09:09:40 +0000706 # XXX Should this raise AttributeError or ValueError?
707 try:
708 loader.loadTestsFromNames(['abc () //'], unittest)
709 except AttributeError:
710 pass
711 except ValueError:
712 pass
713 else:
714 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
715
716 # "The method optionally resolves name relative to the given module"
717 #
718 # Does loadTestsFromNames() make sure the provided `module` is in fact
719 # a module?
720 #
721 # XXX This validation is currently not done. This flexibility should
722 # either be documented or a TypeError should be raised.
723 def test_loadTestsFromNames__relative_not_a_module(self):
724 class MyTestCase(unittest.TestCase):
725 def test(self):
726 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000727
Georg Brandl15c5ce92007-03-07 09:09:40 +0000728 class NotAModule(object):
729 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000730
Georg Brandl15c5ce92007-03-07 09:09:40 +0000731 loader = unittest.TestLoader()
732 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000733
Georg Brandl15c5ce92007-03-07 09:09:40 +0000734 reference = [unittest.TestSuite([MyTestCase('test')])]
735 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000736
Georg Brandl15c5ce92007-03-07 09:09:40 +0000737 # "The specifier name is a ``dotted name'' that may resolve either to
738 # a module, a test case class, a TestSuite instance, a test method
739 # within a test case class, or a callable object which returns a
740 # TestCase or TestSuite instance."
741 #
742 # Does it raise an exception if the name resolves to an invalid
743 # object?
744 def test_loadTestsFromNames__relative_bad_object(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000745 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000746 m.testcase_1 = object()
Tim Petersea5962f2007-03-12 18:07:52 +0000747
Georg Brandl15c5ce92007-03-07 09:09:40 +0000748 loader = unittest.TestLoader()
749 try:
750 loader.loadTestsFromNames(['testcase_1'], m)
751 except TypeError:
752 pass
753 else:
754 self.fail("Should have raised TypeError")
755
756 # "The specifier name is a ``dotted name'' that may resolve ... to
757 # ... a test case class"
758 def test_loadTestsFromNames__relative_TestCase_subclass(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000759 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000760 class MyTestCase(unittest.TestCase):
761 def test(self):
762 pass
763 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000764
Georg Brandl15c5ce92007-03-07 09:09:40 +0000765 loader = unittest.TestLoader()
766 suite = loader.loadTestsFromNames(['testcase_1'], m)
767 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000768
Georg Brandl15c5ce92007-03-07 09:09:40 +0000769 expected = loader.suiteClass([MyTestCase('test')])
770 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000771
Georg Brandl15c5ce92007-03-07 09:09:40 +0000772 # "The specifier name is a ``dotted name'' that may resolve ... to
773 # ... a TestSuite instance"
774 def test_loadTestsFromNames__relative_TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000775 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000776 class MyTestCase(unittest.TestCase):
777 def test(self):
778 pass
779 m.testsuite = unittest.TestSuite([MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000780
Georg Brandl15c5ce92007-03-07 09:09:40 +0000781 loader = unittest.TestLoader()
782 suite = loader.loadTestsFromNames(['testsuite'], m)
783 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000784
Georg Brandl15c5ce92007-03-07 09:09:40 +0000785 self.assertEqual(list(suite), [m.testsuite])
Tim Petersea5962f2007-03-12 18:07:52 +0000786
Georg Brandl15c5ce92007-03-07 09:09:40 +0000787 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
788 # test method within a test case class"
789 def test_loadTestsFromNames__relative_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000790 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000791 class MyTestCase(unittest.TestCase):
792 def test(self):
793 pass
794 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000795
Georg Brandl15c5ce92007-03-07 09:09:40 +0000796 loader = unittest.TestLoader()
797 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
798 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000799
Georg Brandl15c5ce92007-03-07 09:09:40 +0000800 ref_suite = unittest.TestSuite([MyTestCase('test')])
801 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000802
Georg Brandl15c5ce92007-03-07 09:09:40 +0000803 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
804 # test method within a test case class"
805 #
806 # Does the method gracefully handle names that initially look like they
Tim Petersea5962f2007-03-12 18:07:52 +0000807 # resolve to "a test method within a test case class" but don't?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000808 def test_loadTestsFromNames__relative_invalid_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000809 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000810 class MyTestCase(unittest.TestCase):
811 def test(self):
812 pass
813 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000814
Georg Brandl15c5ce92007-03-07 09:09:40 +0000815 loader = unittest.TestLoader()
816 try:
817 loader.loadTestsFromNames(['testcase_1.testfoo'], m)
818 except AttributeError, e:
819 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
820 else:
821 self.fail("Failed to raise AttributeError")
822
823 # "The specifier name is a ``dotted name'' that may resolve ... to
824 # ... a callable object which returns a ... TestSuite instance"
825 def test_loadTestsFromNames__callable__TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000826 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000827 testcase_1 = unittest.FunctionTestCase(lambda: None)
828 testcase_2 = unittest.FunctionTestCase(lambda: None)
829 def return_TestSuite():
830 return unittest.TestSuite([testcase_1, testcase_2])
831 m.return_TestSuite = return_TestSuite
Tim Petersea5962f2007-03-12 18:07:52 +0000832
Georg Brandl15c5ce92007-03-07 09:09:40 +0000833 loader = unittest.TestLoader()
834 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
835 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000836
Georg Brandl15c5ce92007-03-07 09:09:40 +0000837 expected = unittest.TestSuite([testcase_1, testcase_2])
838 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000839
Georg Brandl15c5ce92007-03-07 09:09:40 +0000840 # "The specifier name is a ``dotted name'' that may resolve ... to
841 # ... a callable object which returns a TestCase ... instance"
842 def test_loadTestsFromNames__callable__TestCase_instance(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000843 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000844 testcase_1 = unittest.FunctionTestCase(lambda: None)
845 def return_TestCase():
846 return testcase_1
847 m.return_TestCase = return_TestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000848
Georg Brandl15c5ce92007-03-07 09:09:40 +0000849 loader = unittest.TestLoader()
850 suite = loader.loadTestsFromNames(['return_TestCase'], m)
851 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000852
Georg Brandl15c5ce92007-03-07 09:09:40 +0000853 ref_suite = unittest.TestSuite([testcase_1])
854 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000855
Georg Brandl15c5ce92007-03-07 09:09:40 +0000856 # "The specifier name is a ``dotted name'' that may resolve ... to
857 # ... a callable object which returns a TestCase or TestSuite instance"
858 #
Tim Petersea5962f2007-03-12 18:07:52 +0000859 # Are staticmethods handled correctly?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000860 def test_loadTestsFromNames__callable__call_staticmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000861 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000862 class Test1(unittest.TestCase):
863 def test(self):
864 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000865
Georg Brandl15c5ce92007-03-07 09:09:40 +0000866 testcase_1 = Test1('test')
867 class Foo(unittest.TestCase):
868 @staticmethod
869 def foo():
870 return testcase_1
871 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +0000872
Georg Brandl15c5ce92007-03-07 09:09:40 +0000873 loader = unittest.TestLoader()
874 suite = loader.loadTestsFromNames(['Foo.foo'], m)
875 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000876
Georg Brandl15c5ce92007-03-07 09:09:40 +0000877 ref_suite = unittest.TestSuite([testcase_1])
878 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000879
Georg Brandl15c5ce92007-03-07 09:09:40 +0000880 # "The specifier name is a ``dotted name'' that may resolve ... to
881 # ... a callable object which returns a TestCase or TestSuite instance"
882 #
883 # What happens when the callable returns something else?
884 def test_loadTestsFromNames__callable__wrong_type(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000885 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000886 def return_wrong():
887 return 6
888 m.return_wrong = return_wrong
Tim Petersea5962f2007-03-12 18:07:52 +0000889
Georg Brandl15c5ce92007-03-07 09:09:40 +0000890 loader = unittest.TestLoader()
891 try:
892 suite = loader.loadTestsFromNames(['return_wrong'], m)
893 except TypeError:
894 pass
895 else:
896 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000897
Georg Brandl15c5ce92007-03-07 09:09:40 +0000898 # "The specifier can refer to modules and packages which have not been
Tim Petersea5962f2007-03-12 18:07:52 +0000899 # imported; they will be imported as a side-effect"
Georg Brandl15c5ce92007-03-07 09:09:40 +0000900 def test_loadTestsFromNames__module_not_loaded(self):
901 # We're going to try to load this module as a side-effect, so it
902 # better not be loaded before we try.
903 #
904 # Why pick audioop? Google shows it isn't used very often, so there's
905 # a good chance that it won't be imported when this test is run
906 module_name = 'audioop'
Tim Petersea5962f2007-03-12 18:07:52 +0000907
Georg Brandl15c5ce92007-03-07 09:09:40 +0000908 import sys
909 if module_name in sys.modules:
910 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000911
Georg Brandl15c5ce92007-03-07 09:09:40 +0000912 loader = unittest.TestLoader()
913 try:
914 suite = loader.loadTestsFromNames([module_name])
915
916 self.failUnless(isinstance(suite, loader.suiteClass))
917 self.assertEqual(list(suite), [unittest.TestSuite()])
918
919 # audioop should now be loaded, thanks to loadTestsFromName()
920 self.failUnless(module_name in sys.modules)
921 finally:
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +0000922 if module_name in sys.modules:
923 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000924
Georg Brandl15c5ce92007-03-07 09:09:40 +0000925 ################################################################
926 ### /Tests for TestLoader.loadTestsFromNames()
927
928 ### Tests for TestLoader.getTestCaseNames()
929 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000930
Georg Brandl15c5ce92007-03-07 09:09:40 +0000931 # "Return a sorted sequence of method names found within testCaseClass"
932 #
933 # Test.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +0000934 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +0000935 def test_getTestCaseNames(self):
936 class Test(unittest.TestCase):
937 def test_1(self): pass
938 def test_2(self): pass
939 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000940
Georg Brandl15c5ce92007-03-07 09:09:40 +0000941 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000942
Georg Brandl15c5ce92007-03-07 09:09:40 +0000943 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
Tim Petersea5962f2007-03-12 18:07:52 +0000944
Georg Brandl15c5ce92007-03-07 09:09:40 +0000945 # "Return a sorted sequence of method names found within testCaseClass"
946 #
Tim Petersea5962f2007-03-12 18:07:52 +0000947 # Does getTestCaseNames() behave appropriately if no tests are found?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000948 def test_getTestCaseNames__no_tests(self):
949 class Test(unittest.TestCase):
950 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000951
Georg Brandl15c5ce92007-03-07 09:09:40 +0000952 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000953
Georg Brandl15c5ce92007-03-07 09:09:40 +0000954 self.assertEqual(loader.getTestCaseNames(Test), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000955
Georg Brandl15c5ce92007-03-07 09:09:40 +0000956 # "Return a sorted sequence of method names found within testCaseClass"
957 #
958 # Are not-TestCases handled gracefully?
959 #
960 # XXX This should raise a TypeError, not return a list
961 #
962 # XXX It's too late in the 2.5 release cycle to fix this, but it should
963 # probably be revisited for 2.6
964 def test_getTestCaseNames__not_a_TestCase(self):
965 class BadCase(int):
966 def test_foo(self):
967 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000968
Georg Brandl15c5ce92007-03-07 09:09:40 +0000969 loader = unittest.TestLoader()
970 names = loader.getTestCaseNames(BadCase)
Tim Petersea5962f2007-03-12 18:07:52 +0000971
Georg Brandl15c5ce92007-03-07 09:09:40 +0000972 self.assertEqual(names, ['test_foo'])
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 # Make sure inherited names are handled.
977 #
978 # TestP.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +0000979 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +0000980 def test_getTestCaseNames__inheritance(self):
981 class TestP(unittest.TestCase):
982 def test_1(self): pass
983 def test_2(self): pass
984 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000985
Georg Brandl15c5ce92007-03-07 09:09:40 +0000986 class TestC(TestP):
987 def test_1(self): pass
988 def test_3(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000989
Georg Brandl15c5ce92007-03-07 09:09:40 +0000990 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000991
Georg Brandl15c5ce92007-03-07 09:09:40 +0000992 names = ['test_1', 'test_2', 'test_3']
993 self.assertEqual(loader.getTestCaseNames(TestC), names)
Tim Petersea5962f2007-03-12 18:07:52 +0000994
995 ################################################################
Georg Brandl15c5ce92007-03-07 09:09:40 +0000996 ### /Tests for TestLoader.getTestCaseNames()
997
998 ### Tests for TestLoader.testMethodPrefix
999 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001000
Georg Brandl15c5ce92007-03-07 09:09:40 +00001001 # "String giving the prefix of method names which will be interpreted as
1002 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001003 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001004 # Implicit in the documentation is that testMethodPrefix is respected by
1005 # all loadTestsFrom* methods.
1006 def test_testMethodPrefix__loadTestsFromTestCase(self):
1007 class Foo(unittest.TestCase):
1008 def test_1(self): pass
1009 def test_2(self): pass
1010 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001011
Georg Brandl15c5ce92007-03-07 09:09:40 +00001012 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1013 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001014
Georg Brandl15c5ce92007-03-07 09:09:40 +00001015 loader = unittest.TestLoader()
1016 loader.testMethodPrefix = 'foo'
1017 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1018
1019 loader.testMethodPrefix = 'test'
1020 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001021
Georg Brandl15c5ce92007-03-07 09:09:40 +00001022 # "String giving the prefix of method names which will be interpreted as
1023 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001024 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001025 # Implicit in the documentation is that testMethodPrefix is respected by
1026 # all loadTestsFrom* methods.
1027 def test_testMethodPrefix__loadTestsFromModule(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001028 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001029 class Foo(unittest.TestCase):
1030 def test_1(self): pass
1031 def test_2(self): pass
1032 def foo_bar(self): pass
1033 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001034
Georg Brandl15c5ce92007-03-07 09:09:40 +00001035 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
1036 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
Tim Petersea5962f2007-03-12 18:07:52 +00001037
Georg Brandl15c5ce92007-03-07 09:09:40 +00001038 loader = unittest.TestLoader()
1039 loader.testMethodPrefix = 'foo'
1040 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1041
1042 loader.testMethodPrefix = 'test'
1043 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001044
Georg Brandl15c5ce92007-03-07 09:09:40 +00001045 # "String giving the prefix of method names which will be interpreted as
1046 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001047 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001048 # Implicit in the documentation is that testMethodPrefix is respected by
1049 # all loadTestsFrom* methods.
1050 def test_testMethodPrefix__loadTestsFromName(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001051 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001052 class Foo(unittest.TestCase):
1053 def test_1(self): pass
1054 def test_2(self): pass
1055 def foo_bar(self): pass
1056 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001057
Georg Brandl15c5ce92007-03-07 09:09:40 +00001058 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1059 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001060
Georg Brandl15c5ce92007-03-07 09:09:40 +00001061 loader = unittest.TestLoader()
1062 loader.testMethodPrefix = 'foo'
1063 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1064
1065 loader.testMethodPrefix = 'test'
1066 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001067
Georg Brandl15c5ce92007-03-07 09:09:40 +00001068 # "String giving the prefix of method names which will be interpreted as
1069 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001070 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001071 # Implicit in the documentation is that testMethodPrefix is respected by
1072 # all loadTestsFrom* methods.
1073 def test_testMethodPrefix__loadTestsFromNames(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001074 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001075 class Foo(unittest.TestCase):
1076 def test_1(self): pass
1077 def test_2(self): pass
1078 def foo_bar(self): pass
1079 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001080
Georg Brandl15c5ce92007-03-07 09:09:40 +00001081 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
1082 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1083 tests_2 = unittest.TestSuite([tests_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001084
Georg Brandl15c5ce92007-03-07 09:09:40 +00001085 loader = unittest.TestLoader()
1086 loader.testMethodPrefix = 'foo'
1087 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1088
1089 loader.testMethodPrefix = 'test'
1090 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001091
Georg Brandl15c5ce92007-03-07 09:09:40 +00001092 # "The default value is 'test'"
1093 def test_testMethodPrefix__default_value(self):
1094 loader = unittest.TestLoader()
1095 self.failUnless(loader.testMethodPrefix == 'test')
Tim Petersea5962f2007-03-12 18:07:52 +00001096
Georg Brandl15c5ce92007-03-07 09:09:40 +00001097 ################################################################
1098 ### /Tests for TestLoader.testMethodPrefix
1099
Tim Petersea5962f2007-03-12 18:07:52 +00001100 ### Tests for TestLoader.sortTestMethodsUsing
Georg Brandl15c5ce92007-03-07 09:09:40 +00001101 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001102
Georg Brandl15c5ce92007-03-07 09:09:40 +00001103 # "Function to be used to compare method names when sorting them in
1104 # getTestCaseNames() and all the loadTestsFromX() methods"
1105 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1106 def reversed_cmp(x, y):
1107 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001108
Georg Brandl15c5ce92007-03-07 09:09:40 +00001109 class Foo(unittest.TestCase):
1110 def test_1(self): pass
1111 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001112
Georg Brandl15c5ce92007-03-07 09:09:40 +00001113 loader = unittest.TestLoader()
1114 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001115
Georg Brandl15c5ce92007-03-07 09:09:40 +00001116 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1117 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001118
Georg Brandl15c5ce92007-03-07 09:09:40 +00001119 # "Function to be used to compare method names when sorting them in
1120 # getTestCaseNames() and all the loadTestsFromX() methods"
1121 def test_sortTestMethodsUsing__loadTestsFromModule(self):
1122 def reversed_cmp(x, y):
1123 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001124
Christian Heimesc756d002007-11-27 21:34:01 +00001125 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001126 class Foo(unittest.TestCase):
1127 def test_1(self): pass
1128 def test_2(self): pass
1129 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001130
Georg Brandl15c5ce92007-03-07 09:09:40 +00001131 loader = unittest.TestLoader()
1132 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001133
Georg Brandl15c5ce92007-03-07 09:09:40 +00001134 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1135 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001136
Georg Brandl15c5ce92007-03-07 09:09:40 +00001137 # "Function to be used to compare method names when sorting them in
1138 # getTestCaseNames() and all the loadTestsFromX() methods"
1139 def test_sortTestMethodsUsing__loadTestsFromName(self):
1140 def reversed_cmp(x, y):
1141 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001142
Christian Heimesc756d002007-11-27 21:34:01 +00001143 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001144 class Foo(unittest.TestCase):
1145 def test_1(self): pass
1146 def test_2(self): pass
1147 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001148
Georg Brandl15c5ce92007-03-07 09:09:40 +00001149 loader = unittest.TestLoader()
1150 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001151
Georg Brandl15c5ce92007-03-07 09:09:40 +00001152 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1153 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001154
Georg Brandl15c5ce92007-03-07 09:09:40 +00001155 # "Function to be used to compare method names when sorting them in
1156 # getTestCaseNames() and all the loadTestsFromX() methods"
1157 def test_sortTestMethodsUsing__loadTestsFromNames(self):
1158 def reversed_cmp(x, y):
1159 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001160
Christian Heimesc756d002007-11-27 21:34:01 +00001161 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001162 class Foo(unittest.TestCase):
1163 def test_1(self): pass
1164 def test_2(self): pass
1165 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001166
Georg Brandl15c5ce92007-03-07 09:09:40 +00001167 loader = unittest.TestLoader()
1168 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001169
Georg Brandl15c5ce92007-03-07 09:09:40 +00001170 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1171 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001172
Georg Brandl15c5ce92007-03-07 09:09:40 +00001173 # "Function to be used to compare method names when sorting them in
1174 # getTestCaseNames()"
1175 #
1176 # Does it actually affect getTestCaseNames()?
1177 def test_sortTestMethodsUsing__getTestCaseNames(self):
1178 def reversed_cmp(x, y):
1179 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001180
Georg Brandl15c5ce92007-03-07 09:09:40 +00001181 class Foo(unittest.TestCase):
1182 def test_1(self): pass
1183 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001184
Georg Brandl15c5ce92007-03-07 09:09:40 +00001185 loader = unittest.TestLoader()
1186 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001187
Georg Brandl15c5ce92007-03-07 09:09:40 +00001188 test_names = ['test_2', 'test_1']
1189 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
Tim Petersea5962f2007-03-12 18:07:52 +00001190
Georg Brandl15c5ce92007-03-07 09:09:40 +00001191 # "The default value is the built-in cmp() function"
1192 def test_sortTestMethodsUsing__default_value(self):
1193 loader = unittest.TestLoader()
1194 self.failUnless(loader.sortTestMethodsUsing is cmp)
Tim Petersea5962f2007-03-12 18:07:52 +00001195
Georg Brandl15c5ce92007-03-07 09:09:40 +00001196 # "it can be set to None to disable the sort."
1197 #
1198 # XXX How is this different from reassigning cmp? Are the tests returned
1199 # in a random order or something? This behaviour should die
1200 def test_sortTestMethodsUsing__None(self):
1201 class Foo(unittest.TestCase):
1202 def test_1(self): pass
1203 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001204
Georg Brandl15c5ce92007-03-07 09:09:40 +00001205 loader = unittest.TestLoader()
1206 loader.sortTestMethodsUsing = None
Tim Petersea5962f2007-03-12 18:07:52 +00001207
Georg Brandl15c5ce92007-03-07 09:09:40 +00001208 test_names = ['test_2', 'test_1']
1209 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
Tim Petersea5962f2007-03-12 18:07:52 +00001210
Georg Brandl15c5ce92007-03-07 09:09:40 +00001211 ################################################################
1212 ### /Tests for TestLoader.sortTestMethodsUsing
Tim Petersea5962f2007-03-12 18:07:52 +00001213
Georg Brandl15c5ce92007-03-07 09:09:40 +00001214 ### Tests for TestLoader.suiteClass
1215 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001216
Georg Brandl15c5ce92007-03-07 09:09:40 +00001217 # "Callable object that constructs a test suite from a list of tests."
1218 def test_suiteClass__loadTestsFromTestCase(self):
1219 class Foo(unittest.TestCase):
1220 def test_1(self): pass
1221 def test_2(self): pass
1222 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001223
Georg Brandl15c5ce92007-03-07 09:09:40 +00001224 tests = [Foo('test_1'), Foo('test_2')]
1225
1226 loader = unittest.TestLoader()
1227 loader.suiteClass = list
1228 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001229
Georg Brandl15c5ce92007-03-07 09:09:40 +00001230 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001231 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001232 def test_suiteClass__loadTestsFromModule(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001233 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001234 class Foo(unittest.TestCase):
1235 def test_1(self): pass
1236 def test_2(self): pass
1237 def foo_bar(self): pass
1238 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001239
Georg Brandl15c5ce92007-03-07 09:09:40 +00001240 tests = [[Foo('test_1'), Foo('test_2')]]
1241
1242 loader = unittest.TestLoader()
1243 loader.suiteClass = list
1244 self.assertEqual(loader.loadTestsFromModule(m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001245
Georg Brandl15c5ce92007-03-07 09:09:40 +00001246 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001247 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001248 def test_suiteClass__loadTestsFromName(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001249 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001250 class Foo(unittest.TestCase):
1251 def test_1(self): pass
1252 def test_2(self): pass
1253 def foo_bar(self): pass
1254 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001255
Georg Brandl15c5ce92007-03-07 09:09:40 +00001256 tests = [Foo('test_1'), Foo('test_2')]
1257
1258 loader = unittest.TestLoader()
1259 loader.suiteClass = list
1260 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001261
Georg Brandl15c5ce92007-03-07 09:09:40 +00001262 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001263 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001264 def test_suiteClass__loadTestsFromNames(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001265 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001266 class Foo(unittest.TestCase):
1267 def test_1(self): pass
1268 def test_2(self): pass
1269 def foo_bar(self): pass
1270 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001271
Georg Brandl15c5ce92007-03-07 09:09:40 +00001272 tests = [[Foo('test_1'), Foo('test_2')]]
1273
1274 loader = unittest.TestLoader()
1275 loader.suiteClass = list
1276 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001277
Georg Brandl15c5ce92007-03-07 09:09:40 +00001278 # "The default value is the TestSuite class"
1279 def test_suiteClass__default_value(self):
1280 loader = unittest.TestLoader()
1281 self.failUnless(loader.suiteClass is unittest.TestSuite)
Tim Petersea5962f2007-03-12 18:07:52 +00001282
Georg Brandl15c5ce92007-03-07 09:09:40 +00001283 ################################################################
1284 ### /Tests for TestLoader.suiteClass
1285
1286### Support code for Test_TestSuite
1287################################################################
1288
1289class Foo(unittest.TestCase):
1290 def test_1(self): pass
1291 def test_2(self): pass
1292 def test_3(self): pass
1293 def runTest(self): pass
1294
1295def _mk_TestSuite(*names):
1296 return unittest.TestSuite(Foo(n) for n in names)
Tim Petersea5962f2007-03-12 18:07:52 +00001297
Georg Brandl15c5ce92007-03-07 09:09:40 +00001298################################################################
1299### /Support code for Test_TestSuite
1300
1301class Test_TestSuite(TestCase, TestEquality):
1302
1303 ### Set up attributes needed by inherited tests
1304 ################################################################
1305
1306 # Used by TestEquality.test_eq
1307 eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
1308 ,(unittest.TestSuite(), unittest.TestSuite([]))
1309 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001310
1311 # Used by TestEquality.test_ne
Georg Brandl15c5ce92007-03-07 09:09:40 +00001312 ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
1313 ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
1314 ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
1315 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001316
Georg Brandl15c5ce92007-03-07 09:09:40 +00001317 ################################################################
1318 ### /Set up attributes needed by inherited tests
1319
1320 ### Tests for TestSuite.__init__
1321 ################################################################
1322
1323 # "class TestSuite([tests])"
1324 #
1325 # The tests iterable should be optional
1326 def test_init__tests_optional(self):
1327 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001328
Georg Brandl15c5ce92007-03-07 09:09:40 +00001329 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001330
Georg Brandl15c5ce92007-03-07 09:09:40 +00001331 # "class TestSuite([tests])"
1332 # ...
1333 # "If tests is given, it must be an iterable of individual test cases
1334 # or other test suites that will be used to build the suite initially"
1335 #
1336 # TestSuite should deal with empty tests iterables by allowing the
Tim Petersea5962f2007-03-12 18:07:52 +00001337 # creation of an empty suite
Georg Brandl15c5ce92007-03-07 09:09:40 +00001338 def test_init__empty_tests(self):
1339 suite = unittest.TestSuite([])
Tim Petersea5962f2007-03-12 18:07:52 +00001340
Georg Brandl15c5ce92007-03-07 09:09:40 +00001341 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001342
Georg Brandl15c5ce92007-03-07 09:09:40 +00001343 # "class TestSuite([tests])"
1344 # ...
1345 # "If tests is given, it must be an iterable of individual test cases
1346 # or other test suites that will be used to build the suite initially"
1347 #
Tim Petersea5962f2007-03-12 18:07:52 +00001348 # TestSuite should allow any iterable to provide tests
Georg Brandl15c5ce92007-03-07 09:09:40 +00001349 def test_init__tests_from_any_iterable(self):
1350 def tests():
1351 yield unittest.FunctionTestCase(lambda: None)
1352 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001353
Georg Brandl15c5ce92007-03-07 09:09:40 +00001354 suite_1 = unittest.TestSuite(tests())
1355 self.assertEqual(suite_1.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001356
Georg Brandl15c5ce92007-03-07 09:09:40 +00001357 suite_2 = unittest.TestSuite(suite_1)
1358 self.assertEqual(suite_2.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001359
Georg Brandl15c5ce92007-03-07 09:09:40 +00001360 suite_3 = unittest.TestSuite(set(suite_1))
1361 self.assertEqual(suite_3.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001362
Georg Brandl15c5ce92007-03-07 09:09:40 +00001363 # "class TestSuite([tests])"
1364 # ...
1365 # "If tests is given, it must be an iterable of individual test cases
1366 # or other test suites that will be used to build the suite initially"
1367 #
1368 # Does TestSuite() also allow other TestSuite() instances to be present
1369 # in the tests iterable?
1370 def test_init__TestSuite_instances_in_tests(self):
1371 def tests():
1372 ftc = unittest.FunctionTestCase(lambda: None)
1373 yield unittest.TestSuite([ftc])
1374 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001375
Georg Brandl15c5ce92007-03-07 09:09:40 +00001376 suite = unittest.TestSuite(tests())
1377 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001378
Georg Brandl15c5ce92007-03-07 09:09:40 +00001379 ################################################################
1380 ### /Tests for TestSuite.__init__
1381
1382 # Container types should support the iter protocol
1383 def test_iter(self):
1384 test1 = unittest.FunctionTestCase(lambda: None)
1385 test2 = unittest.FunctionTestCase(lambda: None)
1386 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001387
Georg Brandl15c5ce92007-03-07 09:09:40 +00001388 self.assertEqual(list(suite), [test1, test2])
Tim Petersea5962f2007-03-12 18:07:52 +00001389
Georg Brandl15c5ce92007-03-07 09:09:40 +00001390 # "Return the number of tests represented by the this test object.
1391 # ...this method is also implemented by the TestSuite class, which can
1392 # return larger [greater than 1] values"
1393 #
Tim Petersea5962f2007-03-12 18:07:52 +00001394 # Presumably an empty TestSuite returns 0?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001395 def test_countTestCases_zero_simple(self):
1396 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001397
Georg Brandl15c5ce92007-03-07 09:09:40 +00001398 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001399
Georg Brandl15c5ce92007-03-07 09:09:40 +00001400 # "Return the number of tests represented by the this test object.
1401 # ...this method is also implemented by the TestSuite class, which can
1402 # return larger [greater than 1] values"
1403 #
1404 # Presumably an empty TestSuite (even if it contains other empty
1405 # TestSuite instances) returns 0?
1406 def test_countTestCases_zero_nested(self):
1407 class Test1(unittest.TestCase):
1408 def test(self):
1409 pass
1410
1411 suite = unittest.TestSuite([unittest.TestSuite()])
Tim Petersea5962f2007-03-12 18:07:52 +00001412
Georg Brandl15c5ce92007-03-07 09:09:40 +00001413 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001414
Georg Brandl15c5ce92007-03-07 09:09:40 +00001415 # "Return the number of tests represented by the this test object.
1416 # ...this method is also implemented by the TestSuite class, which can
1417 # return larger [greater than 1] values"
1418 def test_countTestCases_simple(self):
1419 test1 = unittest.FunctionTestCase(lambda: None)
1420 test2 = unittest.FunctionTestCase(lambda: None)
1421 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001422
Georg Brandl15c5ce92007-03-07 09:09:40 +00001423 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001424
Georg Brandl15c5ce92007-03-07 09:09:40 +00001425 # "Return the number of tests represented by the this test object.
1426 # ...this method is also implemented by the TestSuite class, which can
1427 # return larger [greater than 1] values"
1428 #
1429 # Make sure this holds for nested TestSuite instances, too
1430 def test_countTestCases_nested(self):
1431 class Test1(unittest.TestCase):
1432 def test1(self): pass
1433 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001434
Georg Brandl15c5ce92007-03-07 09:09:40 +00001435 test2 = unittest.FunctionTestCase(lambda: None)
1436 test3 = unittest.FunctionTestCase(lambda: None)
1437 child = unittest.TestSuite((Test1('test2'), test2))
1438 parent = unittest.TestSuite((test3, child, Test1('test1')))
Tim Petersea5962f2007-03-12 18:07:52 +00001439
Georg Brandl15c5ce92007-03-07 09:09:40 +00001440 self.assertEqual(parent.countTestCases(), 4)
Tim Petersea5962f2007-03-12 18:07:52 +00001441
Georg Brandl15c5ce92007-03-07 09:09:40 +00001442 # "Run the tests associated with this suite, collecting the result into
1443 # the test result object passed as result."
1444 #
1445 # And if there are no tests? What then?
1446 def test_run__empty_suite(self):
1447 events = []
1448 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001449
Georg Brandl15c5ce92007-03-07 09:09:40 +00001450 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001451
Georg Brandl15c5ce92007-03-07 09:09:40 +00001452 suite.run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001453
Georg Brandl15c5ce92007-03-07 09:09:40 +00001454 self.assertEqual(events, [])
Tim Petersea5962f2007-03-12 18:07:52 +00001455
Georg Brandl15c5ce92007-03-07 09:09:40 +00001456 # "Note that unlike TestCase.run(), TestSuite.run() requires the
1457 # "result object to be passed in."
1458 def test_run__requires_result(self):
1459 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001460
Georg Brandl15c5ce92007-03-07 09:09:40 +00001461 try:
1462 suite.run()
1463 except TypeError:
1464 pass
1465 else:
1466 self.fail("Failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +00001467
Georg Brandl15c5ce92007-03-07 09:09:40 +00001468 # "Run the tests associated with this suite, collecting the result into
Tim Petersea5962f2007-03-12 18:07:52 +00001469 # the test result object passed as result."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001470 def test_run(self):
1471 events = []
1472 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001473
Georg Brandl15c5ce92007-03-07 09:09:40 +00001474 class LoggingCase(unittest.TestCase):
1475 def run(self, result):
1476 events.append('run %s' % self._testMethodName)
Tim Petersea5962f2007-03-12 18:07:52 +00001477
Georg Brandl15c5ce92007-03-07 09:09:40 +00001478 def test1(self): pass
1479 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001480
1481 tests = [LoggingCase('test1'), LoggingCase('test2')]
1482
Georg Brandl15c5ce92007-03-07 09:09:40 +00001483 unittest.TestSuite(tests).run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001484
Georg Brandl15c5ce92007-03-07 09:09:40 +00001485 self.assertEqual(events, ['run test1', 'run test2'])
Tim Petersea5962f2007-03-12 18:07:52 +00001486
1487 # "Add a TestCase ... to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001488 def test_addTest__TestCase(self):
1489 class Foo(unittest.TestCase):
1490 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001491
Georg Brandl15c5ce92007-03-07 09:09:40 +00001492 test = Foo('test')
1493 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001494
Georg Brandl15c5ce92007-03-07 09:09:40 +00001495 suite.addTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001496
Georg Brandl15c5ce92007-03-07 09:09:40 +00001497 self.assertEqual(suite.countTestCases(), 1)
1498 self.assertEqual(list(suite), [test])
Tim Petersea5962f2007-03-12 18:07:52 +00001499
1500 # "Add a ... TestSuite to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001501 def test_addTest__TestSuite(self):
1502 class Foo(unittest.TestCase):
1503 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001504
Georg Brandl15c5ce92007-03-07 09:09:40 +00001505 suite_2 = unittest.TestSuite([Foo('test')])
Tim Petersea5962f2007-03-12 18:07:52 +00001506
Georg Brandl15c5ce92007-03-07 09:09:40 +00001507 suite = unittest.TestSuite()
1508 suite.addTest(suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001509
Georg Brandl15c5ce92007-03-07 09:09:40 +00001510 self.assertEqual(suite.countTestCases(), 1)
1511 self.assertEqual(list(suite), [suite_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001512
Georg Brandl15c5ce92007-03-07 09:09:40 +00001513 # "Add all the tests from an iterable of TestCase and TestSuite
1514 # instances to this test suite."
Tim Petersea5962f2007-03-12 18:07:52 +00001515 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001516 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001517 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001518 def test_addTests(self):
1519 class Foo(unittest.TestCase):
1520 def test_1(self): pass
1521 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001522
Georg Brandl15c5ce92007-03-07 09:09:40 +00001523 test_1 = Foo('test_1')
1524 test_2 = Foo('test_2')
1525 inner_suite = unittest.TestSuite([test_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001526
Georg Brandl15c5ce92007-03-07 09:09:40 +00001527 def gen():
1528 yield test_1
1529 yield test_2
1530 yield inner_suite
Tim Petersea5962f2007-03-12 18:07:52 +00001531
Georg Brandl15c5ce92007-03-07 09:09:40 +00001532 suite_1 = unittest.TestSuite()
1533 suite_1.addTests(gen())
Tim Petersea5962f2007-03-12 18:07:52 +00001534
Georg Brandl15c5ce92007-03-07 09:09:40 +00001535 self.assertEqual(list(suite_1), list(gen()))
Tim Petersea5962f2007-03-12 18:07:52 +00001536
Georg Brandl15c5ce92007-03-07 09:09:40 +00001537 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001538 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001539 suite_2 = unittest.TestSuite()
1540 for t in gen():
1541 suite_2.addTest(t)
Tim Petersea5962f2007-03-12 18:07:52 +00001542
Georg Brandl15c5ce92007-03-07 09:09:40 +00001543 self.assertEqual(suite_1, suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001544
Georg Brandl15c5ce92007-03-07 09:09:40 +00001545 # "Add all the tests from an iterable of TestCase and TestSuite
1546 # instances to this test suite."
1547 #
Tim Petersea5962f2007-03-12 18:07:52 +00001548 # What happens if it doesn't get an iterable?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001549 def test_addTest__noniterable(self):
1550 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001551
Georg Brandl15c5ce92007-03-07 09:09:40 +00001552 try:
1553 suite.addTests(5)
1554 except TypeError:
1555 pass
1556 else:
1557 self.fail("Failed to raise TypeError")
Georg Brandld9e50262007-03-07 11:54:49 +00001558
1559 def test_addTest__noncallable(self):
1560 suite = unittest.TestSuite()
1561 self.assertRaises(TypeError, suite.addTest, 5)
1562
1563 def test_addTest__casesuiteclass(self):
1564 suite = unittest.TestSuite()
1565 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
1566 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
1567
1568 def test_addTests__string(self):
1569 suite = unittest.TestSuite()
1570 self.assertRaises(TypeError, suite.addTests, "foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001571
1572
Georg Brandl15c5ce92007-03-07 09:09:40 +00001573class Test_FunctionTestCase(TestCase):
Tim Petersea5962f2007-03-12 18:07:52 +00001574
Georg Brandl15c5ce92007-03-07 09:09:40 +00001575 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00001576 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001577 def test_countTestCases(self):
1578 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001579
Georg Brandl15c5ce92007-03-07 09:09:40 +00001580 self.assertEqual(test.countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00001581
Georg Brandl15c5ce92007-03-07 09:09:40 +00001582 # "When a setUp() method is defined, the test runner will run that method
1583 # prior to each test. Likewise, if a tearDown() method is defined, the
1584 # test runner will invoke that method after each test. In the example,
1585 # setUp() was used to create a fresh sequence for each test."
1586 #
1587 # Make sure the proper call order is maintained, even if setUp() raises
1588 # an exception.
1589 def test_run_call_order__error_in_setUp(self):
1590 events = []
1591 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001592
Georg Brandl15c5ce92007-03-07 09:09:40 +00001593 def setUp():
1594 events.append('setUp')
1595 raise RuntimeError('raised by setUp')
1596
1597 def test():
1598 events.append('test')
1599
1600 def tearDown():
1601 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001602
1603 expected = ['startTest', 'setUp', 'addError', 'stopTest']
Georg Brandl15c5ce92007-03-07 09:09:40 +00001604 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1605 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001606
Georg Brandl15c5ce92007-03-07 09:09:40 +00001607 # "When a setUp() method is defined, the test runner will run that method
1608 # prior to each test. Likewise, if a tearDown() method is defined, the
1609 # test runner will invoke that method after each test. In the example,
1610 # setUp() was used to create a fresh sequence for each test."
1611 #
1612 # Make sure the proper call order is maintained, even if the test raises
1613 # an error (as opposed to a failure).
1614 def test_run_call_order__error_in_test(self):
1615 events = []
1616 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001617
Georg Brandl15c5ce92007-03-07 09:09:40 +00001618 def setUp():
1619 events.append('setUp')
1620
1621 def test():
1622 events.append('test')
1623 raise RuntimeError('raised by test')
1624
1625 def tearDown():
1626 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001627
Georg Brandl15c5ce92007-03-07 09:09:40 +00001628 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
1629 'stopTest']
1630 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1631 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001632
Georg Brandl15c5ce92007-03-07 09:09:40 +00001633 # "When a setUp() method is defined, the test runner will run that method
1634 # prior to each test. Likewise, if a tearDown() method is defined, the
1635 # test runner will invoke that method after each test. In the example,
1636 # setUp() was used to create a fresh sequence for each test."
1637 #
1638 # Make sure the proper call order is maintained, even if the test signals
1639 # a failure (as opposed to an error).
1640 def test_run_call_order__failure_in_test(self):
1641 events = []
1642 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001643
Georg Brandl15c5ce92007-03-07 09:09:40 +00001644 def setUp():
1645 events.append('setUp')
1646
1647 def test():
1648 events.append('test')
1649 self.fail('raised by test')
1650
1651 def tearDown():
1652 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001653
Georg Brandl15c5ce92007-03-07 09:09:40 +00001654 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
1655 'stopTest']
1656 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1657 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001658
Georg Brandl15c5ce92007-03-07 09:09:40 +00001659 # "When a setUp() method is defined, the test runner will run that method
1660 # prior to each test. Likewise, if a tearDown() method is defined, the
1661 # test runner will invoke that method after each test. In the example,
1662 # setUp() was used to create a fresh sequence for each test."
1663 #
1664 # Make sure the proper call order is maintained, even if tearDown() raises
1665 # an exception.
1666 def test_run_call_order__error_in_tearDown(self):
1667 events = []
1668 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001669
Georg Brandl15c5ce92007-03-07 09:09:40 +00001670 def setUp():
1671 events.append('setUp')
1672
1673 def test():
1674 events.append('test')
1675
1676 def tearDown():
1677 events.append('tearDown')
1678 raise RuntimeError('raised by tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001679
Georg Brandl15c5ce92007-03-07 09:09:40 +00001680 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
1681 'stopTest']
1682 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1683 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001684
Georg Brandl15c5ce92007-03-07 09:09:40 +00001685 # "Return a string identifying the specific test case."
1686 #
1687 # Because of the vague nature of the docs, I'm not going to lock this
1688 # test down too much. Really all that can be asserted is that the id()
1689 # will be a string (either 8-byte or unicode -- again, because the docs
1690 # just say "string")
1691 def test_id(self):
1692 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001693
Georg Brandl15c5ce92007-03-07 09:09:40 +00001694 self.failUnless(isinstance(test.id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00001695
Georg Brandl15c5ce92007-03-07 09:09:40 +00001696 # "Returns a one-line description of the test, or None if no description
1697 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001698 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001699 def test_shortDescription__no_docstring(self):
1700 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001701
Georg Brandl15c5ce92007-03-07 09:09:40 +00001702 self.assertEqual(test.shortDescription(), None)
Tim Petersea5962f2007-03-12 18:07:52 +00001703
Georg Brandl15c5ce92007-03-07 09:09:40 +00001704 # "Returns a one-line description of the test, or None if no description
1705 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001706 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001707 def test_shortDescription__singleline_docstring(self):
1708 desc = "this tests foo"
1709 test = unittest.FunctionTestCase(lambda: None, description=desc)
Tim Petersea5962f2007-03-12 18:07:52 +00001710
Georg Brandl15c5ce92007-03-07 09:09:40 +00001711 self.assertEqual(test.shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001712
Georg Brandl15c5ce92007-03-07 09:09:40 +00001713class Test_TestResult(TestCase):
1714 # Note: there are not separate tests for TestResult.wasSuccessful(),
1715 # TestResult.errors, TestResult.failures, TestResult.testsRun or
1716 # TestResult.shouldStop because these only have meaning in terms of
1717 # other TestResult methods.
1718 #
1719 # Accordingly, tests for the aforenamed attributes are incorporated
1720 # in with the tests for the defining methods.
1721 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001722
Georg Brandl15c5ce92007-03-07 09:09:40 +00001723 def test_init(self):
1724 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001725
Georg Brandl15c5ce92007-03-07 09:09:40 +00001726 self.failUnless(result.wasSuccessful())
1727 self.assertEqual(len(result.errors), 0)
1728 self.assertEqual(len(result.failures), 0)
1729 self.assertEqual(result.testsRun, 0)
1730 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001731
Georg Brandl15c5ce92007-03-07 09:09:40 +00001732 # "This method can be called to signal that the set of tests being
1733 # run should be aborted by setting the TestResult's shouldStop
Tim Petersea5962f2007-03-12 18:07:52 +00001734 # attribute to True."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001735 def test_stop(self):
1736 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001737
Georg Brandl15c5ce92007-03-07 09:09:40 +00001738 result.stop()
Tim Petersea5962f2007-03-12 18:07:52 +00001739
Georg Brandl15c5ce92007-03-07 09:09:40 +00001740 self.assertEqual(result.shouldStop, True)
Tim Petersea5962f2007-03-12 18:07:52 +00001741
Georg Brandl15c5ce92007-03-07 09:09:40 +00001742 # "Called when the test case test is about to be run. The default
1743 # implementation simply increments the instance's testsRun counter."
1744 def test_startTest(self):
1745 class Foo(unittest.TestCase):
1746 def test_1(self):
1747 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001748
Georg Brandl15c5ce92007-03-07 09:09:40 +00001749 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001750
Georg Brandl15c5ce92007-03-07 09:09:40 +00001751 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001752
Georg Brandl15c5ce92007-03-07 09:09:40 +00001753 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001754
Georg Brandl15c5ce92007-03-07 09:09:40 +00001755 self.failUnless(result.wasSuccessful())
1756 self.assertEqual(len(result.errors), 0)
1757 self.assertEqual(len(result.failures), 0)
1758 self.assertEqual(result.testsRun, 1)
1759 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001760
Georg Brandl15c5ce92007-03-07 09:09:40 +00001761 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001762
Georg Brandl15c5ce92007-03-07 09:09:40 +00001763 # "Called after the test case test has been executed, regardless of
1764 # the outcome. The default implementation does nothing."
1765 def test_stopTest(self):
1766 class Foo(unittest.TestCase):
1767 def test_1(self):
1768 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001769
Georg Brandl15c5ce92007-03-07 09:09:40 +00001770 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001771
Georg Brandl15c5ce92007-03-07 09:09:40 +00001772 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001773
Georg Brandl15c5ce92007-03-07 09:09:40 +00001774 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001775
Georg Brandl15c5ce92007-03-07 09:09:40 +00001776 self.failUnless(result.wasSuccessful())
1777 self.assertEqual(len(result.errors), 0)
1778 self.assertEqual(len(result.failures), 0)
1779 self.assertEqual(result.testsRun, 1)
1780 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001781
Georg Brandl15c5ce92007-03-07 09:09:40 +00001782 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001783
Georg Brandl15c5ce92007-03-07 09:09:40 +00001784 # Same tests as above; make sure nothing has changed
1785 self.failUnless(result.wasSuccessful())
1786 self.assertEqual(len(result.errors), 0)
1787 self.assertEqual(len(result.failures), 0)
1788 self.assertEqual(result.testsRun, 1)
1789 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001790
Georg Brandl15c5ce92007-03-07 09:09:40 +00001791 # "addSuccess(test)"
1792 # ...
1793 # "Called when the test case test succeeds"
1794 # ...
1795 # "wasSuccessful() - Returns True if all tests run so far have passed,
1796 # otherwise returns False"
1797 # ...
1798 # "testsRun - The total number of tests run so far."
1799 # ...
1800 # "errors - A list containing 2-tuples of TestCase instances and
1801 # formatted tracebacks. Each tuple represents a test which raised an
1802 # unexpected exception. Contains formatted
1803 # tracebacks instead of sys.exc_info() results."
1804 # ...
1805 # "failures - A list containing 2-tuples of TestCase instances and
1806 # formatted tracebacks. Each tuple represents a test where a failure was
1807 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1808 # methods. Contains formatted tracebacks instead
1809 # of sys.exc_info() results."
1810 def test_addSuccess(self):
1811 class Foo(unittest.TestCase):
1812 def test_1(self):
1813 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001814
Georg Brandl15c5ce92007-03-07 09:09:40 +00001815 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001816
Georg Brandl15c5ce92007-03-07 09:09:40 +00001817 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001818
Georg Brandl15c5ce92007-03-07 09:09:40 +00001819 result.startTest(test)
1820 result.addSuccess(test)
1821 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001822
Georg Brandl15c5ce92007-03-07 09:09:40 +00001823 self.failUnless(result.wasSuccessful())
1824 self.assertEqual(len(result.errors), 0)
1825 self.assertEqual(len(result.failures), 0)
1826 self.assertEqual(result.testsRun, 1)
1827 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001828
Georg Brandl15c5ce92007-03-07 09:09:40 +00001829 # "addFailure(test, err)"
1830 # ...
1831 # "Called when the test case test signals a failure. err is a tuple of
1832 # the form returned by sys.exc_info(): (type, value, traceback)"
1833 # ...
1834 # "wasSuccessful() - Returns True if all tests run so far have passed,
1835 # otherwise returns False"
1836 # ...
1837 # "testsRun - The total number of tests run so far."
1838 # ...
1839 # "errors - A list containing 2-tuples of TestCase instances and
1840 # formatted tracebacks. Each tuple represents a test which raised an
1841 # unexpected exception. Contains formatted
1842 # tracebacks instead of sys.exc_info() results."
1843 # ...
1844 # "failures - A list containing 2-tuples of TestCase instances and
1845 # formatted tracebacks. Each tuple represents a test where a failure was
1846 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1847 # methods. Contains formatted tracebacks instead
1848 # of sys.exc_info() results."
1849 def test_addFailure(self):
1850 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001851
Georg Brandl15c5ce92007-03-07 09:09:40 +00001852 class Foo(unittest.TestCase):
1853 def test_1(self):
1854 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001855
Georg Brandl15c5ce92007-03-07 09:09:40 +00001856 test = Foo('test_1')
1857 try:
1858 test.fail("foo")
1859 except:
1860 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001861
Georg Brandl15c5ce92007-03-07 09:09:40 +00001862 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001863
Georg Brandl15c5ce92007-03-07 09:09:40 +00001864 result.startTest(test)
1865 result.addFailure(test, exc_info_tuple)
1866 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001867
Georg Brandl15c5ce92007-03-07 09:09:40 +00001868 self.failIf(result.wasSuccessful())
1869 self.assertEqual(len(result.errors), 0)
1870 self.assertEqual(len(result.failures), 1)
1871 self.assertEqual(result.testsRun, 1)
1872 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001873
Georg Brandl15c5ce92007-03-07 09:09:40 +00001874 test_case, formatted_exc = result.failures[0]
1875 self.failUnless(test_case is test)
1876 self.failUnless(isinstance(formatted_exc, str))
Tim Petersea5962f2007-03-12 18:07:52 +00001877
Georg Brandl15c5ce92007-03-07 09:09:40 +00001878 # "addError(test, err)"
1879 # ...
1880 # "Called when the test case test raises an unexpected exception err
1881 # is a tuple of the form returned by sys.exc_info():
1882 # (type, value, traceback)"
1883 # ...
1884 # "wasSuccessful() - Returns True if all tests run so far have passed,
1885 # otherwise returns False"
1886 # ...
1887 # "testsRun - The total number of tests run so far."
1888 # ...
1889 # "errors - A list containing 2-tuples of TestCase instances and
1890 # formatted tracebacks. Each tuple represents a test which raised an
1891 # unexpected exception. Contains formatted
1892 # tracebacks instead of sys.exc_info() results."
1893 # ...
1894 # "failures - A list containing 2-tuples of TestCase instances and
1895 # formatted tracebacks. Each tuple represents a test where a failure was
1896 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1897 # methods. Contains formatted tracebacks instead
1898 # of sys.exc_info() results."
1899 def test_addError(self):
1900 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001901
Georg Brandl15c5ce92007-03-07 09:09:40 +00001902 class Foo(unittest.TestCase):
1903 def test_1(self):
1904 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001905
Georg Brandl15c5ce92007-03-07 09:09:40 +00001906 test = Foo('test_1')
1907 try:
1908 raise TypeError()
1909 except:
1910 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001911
Georg Brandl15c5ce92007-03-07 09:09:40 +00001912 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001913
Georg Brandl15c5ce92007-03-07 09:09:40 +00001914 result.startTest(test)
1915 result.addError(test, exc_info_tuple)
1916 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001917
Georg Brandl15c5ce92007-03-07 09:09:40 +00001918 self.failIf(result.wasSuccessful())
1919 self.assertEqual(len(result.errors), 1)
1920 self.assertEqual(len(result.failures), 0)
1921 self.assertEqual(result.testsRun, 1)
1922 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001923
Georg Brandl15c5ce92007-03-07 09:09:40 +00001924 test_case, formatted_exc = result.errors[0]
1925 self.failUnless(test_case is test)
1926 self.failUnless(isinstance(formatted_exc, str))
1927
1928### Support code for Test_TestCase
1929################################################################
1930
1931class Foo(unittest.TestCase):
1932 def runTest(self): pass
1933 def test1(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001934
Georg Brandl15c5ce92007-03-07 09:09:40 +00001935class Bar(Foo):
1936 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001937
Georg Brandl15c5ce92007-03-07 09:09:40 +00001938################################################################
1939### /Support code for Test_TestCase
1940
1941class Test_TestCase(TestCase, TestEquality, TestHashing):
1942
1943 ### Set up attributes used by inherited tests
1944 ################################################################
1945
1946 # Used by TestHashing.test_hash and TestEquality.test_eq
1947 eq_pairs = [(Foo('test1'), Foo('test1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001948
Georg Brandl15c5ce92007-03-07 09:09:40 +00001949 # Used by TestEquality.test_ne
1950 ne_pairs = [(Foo('test1'), Foo('runTest'))
1951 ,(Foo('test1'), Bar('test1'))
1952 ,(Foo('test1'), Bar('test2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001953
Georg Brandl15c5ce92007-03-07 09:09:40 +00001954 ################################################################
1955 ### /Set up attributes used by inherited tests
Tim Petersea5962f2007-03-12 18:07:52 +00001956
Georg Brandl15c5ce92007-03-07 09:09:40 +00001957
1958 # "class TestCase([methodName])"
1959 # ...
1960 # "Each instance of TestCase will run a single test method: the
1961 # method named methodName."
1962 # ...
1963 # "methodName defaults to "runTest"."
1964 #
1965 # Make sure it really is optional, and that it defaults to the proper
1966 # thing.
1967 def test_init__no_test_name(self):
1968 class Test(unittest.TestCase):
1969 def runTest(self): raise MyException()
1970 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001971
Georg Brandl15c5ce92007-03-07 09:09:40 +00001972 self.assertEqual(Test().id()[-13:], '.Test.runTest')
Tim Petersea5962f2007-03-12 18:07:52 +00001973
Georg Brandl15c5ce92007-03-07 09:09:40 +00001974 # "class TestCase([methodName])"
1975 # ...
1976 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00001977 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001978 def test_init__test_name__valid(self):
1979 class Test(unittest.TestCase):
1980 def runTest(self): raise MyException()
1981 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001982
Georg Brandl15c5ce92007-03-07 09:09:40 +00001983 self.assertEqual(Test('test').id()[-10:], '.Test.test')
Tim Petersea5962f2007-03-12 18:07:52 +00001984
Georg Brandl15c5ce92007-03-07 09:09:40 +00001985 # "class TestCase([methodName])"
1986 # ...
1987 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00001988 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001989 def test_init__test_name__invalid(self):
1990 class Test(unittest.TestCase):
1991 def runTest(self): raise MyException()
1992 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001993
Georg Brandl15c5ce92007-03-07 09:09:40 +00001994 try:
1995 Test('testfoo')
1996 except ValueError:
1997 pass
1998 else:
1999 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +00002000
Georg Brandl15c5ce92007-03-07 09:09:40 +00002001 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00002002 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002003 def test_countTestCases(self):
2004 class Foo(unittest.TestCase):
2005 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002006
Georg Brandl15c5ce92007-03-07 09:09:40 +00002007 self.assertEqual(Foo('test').countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00002008
Georg Brandl15c5ce92007-03-07 09:09:40 +00002009 # "Return the default type of test result object to be used to run this
2010 # test. For TestCase instances, this will always be
2011 # unittest.TestResult; subclasses of TestCase should
2012 # override this as necessary."
2013 def test_defaultTestResult(self):
2014 class Foo(unittest.TestCase):
2015 def runTest(self):
2016 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002017
Georg Brandl15c5ce92007-03-07 09:09:40 +00002018 result = Foo().defaultTestResult()
2019 self.assertEqual(type(result), unittest.TestResult)
2020
2021 # "When a setUp() method is defined, the test runner will run that method
2022 # prior to each test. Likewise, if a tearDown() method is defined, the
2023 # test runner will invoke that method after each test. In the example,
2024 # setUp() was used to create a fresh sequence for each test."
2025 #
2026 # Make sure the proper call order is maintained, even if setUp() raises
2027 # an exception.
2028 def test_run_call_order__error_in_setUp(self):
2029 events = []
2030 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002031
Georg Brandl15c5ce92007-03-07 09:09:40 +00002032 class Foo(unittest.TestCase):
2033 def setUp(self):
2034 events.append('setUp')
2035 raise RuntimeError('raised by Foo.setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002036
Georg Brandl15c5ce92007-03-07 09:09:40 +00002037 def test(self):
2038 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002039
Georg Brandl15c5ce92007-03-07 09:09:40 +00002040 def tearDown(self):
2041 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002042
Georg Brandl15c5ce92007-03-07 09:09:40 +00002043 Foo('test').run(result)
2044 expected = ['startTest', 'setUp', 'addError', 'stopTest']
2045 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002046
Georg Brandl15c5ce92007-03-07 09:09:40 +00002047 # "When a setUp() method is defined, the test runner will run that method
2048 # prior to each test. Likewise, if a tearDown() method is defined, the
2049 # test runner will invoke that method after each test. In the example,
2050 # setUp() was used to create a fresh sequence for each test."
2051 #
2052 # Make sure the proper call order is maintained, even if the test raises
2053 # an error (as opposed to a failure).
2054 def test_run_call_order__error_in_test(self):
2055 events = []
2056 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002057
Georg Brandl15c5ce92007-03-07 09:09:40 +00002058 class Foo(unittest.TestCase):
2059 def setUp(self):
2060 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002061
Georg Brandl15c5ce92007-03-07 09:09:40 +00002062 def test(self):
2063 events.append('test')
2064 raise RuntimeError('raised by Foo.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002065
Georg Brandl15c5ce92007-03-07 09:09:40 +00002066 def tearDown(self):
2067 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002068
Georg Brandl15c5ce92007-03-07 09:09:40 +00002069 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
2070 'stopTest']
2071 Foo('test').run(result)
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 signals
2080 # a failure (as opposed to an error).
2081 def test_run_call_order__failure_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 self.fail('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', 'addFailure', '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 tearDown() raises
2107 # an exception.
2108 def test_run_call_order__error_in_tearDown(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')
Tim Petersea5962f2007-03-12 18:07:52 +00002118
Georg Brandl15c5ce92007-03-07 09:09:40 +00002119 def tearDown(self):
2120 events.append('tearDown')
2121 raise RuntimeError('raised by Foo.tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002122
Georg Brandl15c5ce92007-03-07 09:09:40 +00002123 Foo('test').run(result)
2124 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
2125 'stopTest']
2126 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002127
Georg Brandl15c5ce92007-03-07 09:09:40 +00002128 # "This class attribute gives the exception raised by the test() method.
2129 # If a test framework needs to use a specialized exception, possibly to
2130 # carry additional information, it must subclass this exception in
2131 # order to ``play fair'' with the framework. The initial value of this
2132 # attribute is AssertionError"
2133 def test_failureException__default(self):
2134 class Foo(unittest.TestCase):
2135 def test(self):
2136 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002137
Georg Brandl15c5ce92007-03-07 09:09:40 +00002138 self.failUnless(Foo('test').failureException is AssertionError)
Tim Petersea5962f2007-03-12 18:07:52 +00002139
Georg Brandl15c5ce92007-03-07 09:09:40 +00002140 # "This class attribute gives the exception raised by the test() method.
2141 # If a test framework needs to use a specialized exception, possibly to
2142 # carry additional information, it must subclass this exception in
2143 # order to ``play fair'' with the framework."
2144 #
2145 # Make sure TestCase.run() respects the designated failureException
2146 def test_failureException__subclassing__explicit_raise(self):
2147 events = []
2148 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002149
Georg Brandl15c5ce92007-03-07 09:09:40 +00002150 class Foo(unittest.TestCase):
2151 def test(self):
2152 raise RuntimeError()
Tim Petersea5962f2007-03-12 18:07:52 +00002153
Georg Brandl15c5ce92007-03-07 09:09:40 +00002154 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002155
Georg Brandl15c5ce92007-03-07 09:09:40 +00002156 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002157
2158
Georg Brandl15c5ce92007-03-07 09:09:40 +00002159 Foo('test').run(result)
2160 expected = ['startTest', 'addFailure', 'stopTest']
2161 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002162
Georg Brandl15c5ce92007-03-07 09:09:40 +00002163 # "This class attribute gives the exception raised by the test() method.
2164 # If a test framework needs to use a specialized exception, possibly to
2165 # carry additional information, it must subclass this exception in
2166 # order to ``play fair'' with the framework."
2167 #
2168 # Make sure TestCase.run() respects the designated failureException
2169 def test_failureException__subclassing__implicit_raise(self):
2170 events = []
2171 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002172
Georg Brandl15c5ce92007-03-07 09:09:40 +00002173 class Foo(unittest.TestCase):
2174 def test(self):
2175 self.fail("foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002176
Georg Brandl15c5ce92007-03-07 09:09:40 +00002177 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002178
Georg Brandl15c5ce92007-03-07 09:09:40 +00002179 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002180
2181
Georg Brandl15c5ce92007-03-07 09:09:40 +00002182 Foo('test').run(result)
2183 expected = ['startTest', 'addFailure', 'stopTest']
2184 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002185
2186 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002187 def test_setUp(self):
2188 class Foo(unittest.TestCase):
2189 def runTest(self):
2190 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002191
Georg Brandl15c5ce92007-03-07 09:09:40 +00002192 # ... and nothing should happen
2193 Foo().setUp()
Tim Petersea5962f2007-03-12 18:07:52 +00002194
2195 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002196 def test_tearDown(self):
2197 class Foo(unittest.TestCase):
2198 def runTest(self):
2199 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002200
Georg Brandl15c5ce92007-03-07 09:09:40 +00002201 # ... and nothing should happen
2202 Foo().tearDown()
Tim Petersea5962f2007-03-12 18:07:52 +00002203
Georg Brandl15c5ce92007-03-07 09:09:40 +00002204 # "Return a string identifying the specific test case."
2205 #
2206 # Because of the vague nature of the docs, I'm not going to lock this
2207 # test down too much. Really all that can be asserted is that the id()
2208 # will be a string (either 8-byte or unicode -- again, because the docs
2209 # just say "string")
2210 def test_id(self):
2211 class Foo(unittest.TestCase):
2212 def runTest(self):
2213 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002214
Georg Brandl15c5ce92007-03-07 09:09:40 +00002215 self.failUnless(isinstance(Foo().id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00002216
Georg Brandl15c5ce92007-03-07 09:09:40 +00002217 # "Returns a one-line description of the test, or None if no description
2218 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002219 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002220 def test_shortDescription__no_docstring(self):
2221 class Foo(unittest.TestCase):
2222 def runTest(self):
2223 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002224
Georg Brandl15c5ce92007-03-07 09:09:40 +00002225 self.assertEqual(Foo().shortDescription(), None)
Tim Petersea5962f2007-03-12 18:07:52 +00002226
Georg Brandl15c5ce92007-03-07 09:09:40 +00002227 # "Returns a one-line description of the test, or None if no description
2228 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002229 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002230 def test_shortDescription__singleline_docstring(self):
2231 class Foo(unittest.TestCase):
2232 def runTest(self):
2233 "this tests foo"
2234 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002235
Georg Brandl15c5ce92007-03-07 09:09:40 +00002236 self.assertEqual(Foo().shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002237
Georg Brandl15c5ce92007-03-07 09:09:40 +00002238 # "Returns a one-line description of the test, or None if no description
2239 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00002240 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002241 def test_shortDescription__multiline_docstring(self):
2242 class Foo(unittest.TestCase):
2243 def runTest(self):
2244 """this tests foo
2245 blah, bar and baz are also tested"""
2246 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002247
Georg Brandl15c5ce92007-03-07 09:09:40 +00002248 self.assertEqual(Foo().shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002249
Georg Brandl15c5ce92007-03-07 09:09:40 +00002250 # "If result is omitted or None, a temporary result object is created
Tim Petersea5962f2007-03-12 18:07:52 +00002251 # and used, but is not made available to the caller"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002252 def test_run__uses_defaultTestResult(self):
2253 events = []
Tim Petersea5962f2007-03-12 18:07:52 +00002254
Georg Brandl15c5ce92007-03-07 09:09:40 +00002255 class Foo(unittest.TestCase):
2256 def test(self):
2257 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002258
Georg Brandl15c5ce92007-03-07 09:09:40 +00002259 def defaultTestResult(self):
2260 return LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002261
2262 # Make run() find a result object on its own
Georg Brandl15c5ce92007-03-07 09:09:40 +00002263 Foo('test').run()
Tim Petersea5962f2007-03-12 18:07:52 +00002264
Georg Brandl15c5ce92007-03-07 09:09:40 +00002265 expected = ['startTest', 'test', 'stopTest']
2266 self.assertEqual(events, expected)
Jim Fultonfafd8742004-08-28 15:22:12 +00002267
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002268class Test_Assertions(TestCase):
2269 def test_AlmostEqual(self):
2270 self.failUnlessAlmostEqual(1.00000001, 1.0)
2271 self.failIfAlmostEqual(1.0000001, 1.0)
2272 self.assertRaises(AssertionError,
2273 self.failUnlessAlmostEqual, 1.0000001, 1.0)
2274 self.assertRaises(AssertionError,
2275 self.failIfAlmostEqual, 1.00000001, 1.0)
2276
2277 self.failUnlessAlmostEqual(1.1, 1.0, places=0)
2278 self.assertRaises(AssertionError,
2279 self.failUnlessAlmostEqual, 1.1, 1.0, places=1)
2280
2281 self.failUnlessAlmostEqual(0, .1+.1j, places=0)
2282 self.failIfAlmostEqual(0, .1+.1j, places=1)
2283 self.assertRaises(AssertionError,
2284 self.failUnlessAlmostEqual, 0, .1+.1j, places=1)
2285 self.assertRaises(AssertionError,
2286 self.failIfAlmostEqual, 0, .1+.1j, places=0)
2287
Michael Foord48bc8202009-06-02 18:22:38 +00002288
Michael Foord48bc8202009-06-02 18:22:38 +00002289
Jim Fultonfafd8742004-08-28 15:22:12 +00002290######################################################################
2291## Main
2292######################################################################
2293
2294def test_main():
Georg Brandl15c5ce92007-03-07 09:09:40 +00002295 test_support.run_unittest(Test_TestCase, Test_TestLoader,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002296 Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
Michael Foordbee87b42009-07-13 16:32:47 +00002297 Test_Assertions)
Jim Fultonfafd8742004-08-28 15:22:12 +00002298
Georg Brandl15c5ce92007-03-07 09:09:40 +00002299if __name__ == "__main__":
Tim Petersea5962f2007-03-12 18:07:52 +00002300 test_main()