blob: c16327e4072cddb4bf1c027e768c23b3a3cb3567 [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
Gregory P. Smith28399852009-03-31 16:54:10 +00009import re
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
Benjamin Peterson692428e2009-03-23 21:50:21 +000035 def addSuccess(self, *args):
36 self._events.append('addSuccess')
37 super(LoggingResult, self).addSuccess(*args)
38
Georg Brandl15c5ce92007-03-07 09:09:40 +000039 def addError(self, *args):
40 self._events.append('addError')
41 super(LoggingResult, self).addError(*args)
42
Benjamin Peterson692428e2009-03-23 21:50:21 +000043 def addSkip(self, *args):
44 self._events.append('addSkip')
45 super(LoggingResult, self).addSkip(*args)
46
47 def addExpectedFailure(self, *args):
48 self._events.append('addExpectedFailure')
49 super(LoggingResult, self).addExpectedFailure(*args)
50
51 def addUnexpectedSuccess(self, *args):
52 self._events.append('addUnexpectedSuccess')
53 super(LoggingResult, self).addUnexpectedSuccess(*args)
54
55
Georg Brandl15c5ce92007-03-07 09:09:40 +000056class TestEquality(object):
Michael Foord345b2fe2009-04-02 03:20:38 +000057 """Used as a mixin for TestCase"""
58
Tim Petersea5962f2007-03-12 18:07:52 +000059 # Check for a valid __eq__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000060 def test_eq(self):
61 for obj_1, obj_2 in self.eq_pairs:
62 self.assertEqual(obj_1, obj_2)
63 self.assertEqual(obj_2, obj_1)
Tim Petersea5962f2007-03-12 18:07:52 +000064
65 # Check for a valid __ne__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000066 def test_ne(self):
67 for obj_1, obj_2 in self.ne_pairs:
68 self.failIfEqual(obj_1, obj_2)
69 self.failIfEqual(obj_2, obj_1)
Tim Petersea5962f2007-03-12 18:07:52 +000070
Georg Brandl15c5ce92007-03-07 09:09:40 +000071class TestHashing(object):
Michael Foord345b2fe2009-04-02 03:20:38 +000072 """Used as a mixin for TestCase"""
73
Tim Petersea5962f2007-03-12 18:07:52 +000074 # Check for a valid __hash__ implementation
Georg Brandl15c5ce92007-03-07 09:09:40 +000075 def test_hash(self):
76 for obj_1, obj_2 in self.eq_pairs:
77 try:
Gregory P. Smith28399852009-03-31 16:54:10 +000078 if not hash(obj_1) == hash(obj_2):
79 self.fail("%r and %r do not hash equal" % (obj_1, obj_2))
Georg Brandl15c5ce92007-03-07 09:09:40 +000080 except KeyboardInterrupt:
81 raise
Georg Brandl15c5ce92007-03-07 09:09:40 +000082 except Exception, e:
Gregory P. Smith28399852009-03-31 16:54:10 +000083 self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e))
Tim Petersea5962f2007-03-12 18:07:52 +000084
Georg Brandl15c5ce92007-03-07 09:09:40 +000085 for obj_1, obj_2 in self.ne_pairs:
86 try:
Gregory P. Smith28399852009-03-31 16:54:10 +000087 if hash(obj_1) == hash(obj_2):
88 self.fail("%s and %s hash equal, but shouldn't" %
89 (obj_1, obj_2))
Georg Brandl15c5ce92007-03-07 09:09:40 +000090 except KeyboardInterrupt:
91 raise
Georg Brandl15c5ce92007-03-07 09:09:40 +000092 except Exception, e:
93 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
Tim Petersea5962f2007-03-12 18:07:52 +000094
Georg Brandl15c5ce92007-03-07 09:09:40 +000095
Benjamin Peterson692428e2009-03-23 21:50:21 +000096# List subclass we can add attributes to.
97class MyClassSuite(list):
98
99 def __init__(self, tests, klass):
100 super(MyClassSuite, self).__init__(tests)
101
102
Georg Brandl15c5ce92007-03-07 09:09:40 +0000103################################################################
104### /Support code
105
106class Test_TestLoader(TestCase):
107
108 ### Tests for TestLoader.loadTestsFromTestCase
109 ################################################################
110
111 # "Return a suite of all tests cases contained in the TestCase-derived
112 # class testCaseClass"
113 def test_loadTestsFromTestCase(self):
114 class Foo(unittest.TestCase):
115 def test_1(self): pass
116 def test_2(self): pass
117 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000118
Georg Brandl15c5ce92007-03-07 09:09:40 +0000119 tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +0000120
Georg Brandl15c5ce92007-03-07 09:09:40 +0000121 loader = unittest.TestLoader()
122 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +0000123
Georg Brandl15c5ce92007-03-07 09:09:40 +0000124 # "Return a suite of all tests cases contained in the TestCase-derived
125 # class testCaseClass"
126 #
Tim Petersea5962f2007-03-12 18:07:52 +0000127 # Make sure it does the right thing even if no tests were found
Georg Brandl15c5ce92007-03-07 09:09:40 +0000128 def test_loadTestsFromTestCase__no_matches(self):
129 class Foo(unittest.TestCase):
130 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000131
Georg Brandl15c5ce92007-03-07 09:09:40 +0000132 empty_suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +0000133
Georg Brandl15c5ce92007-03-07 09:09:40 +0000134 loader = unittest.TestLoader()
135 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
Tim Petersea5962f2007-03-12 18:07:52 +0000136
Georg Brandl15c5ce92007-03-07 09:09:40 +0000137 # "Return a suite of all tests cases contained in the TestCase-derived
138 # class testCaseClass"
139 #
140 # What happens if loadTestsFromTestCase() is given an object
141 # that isn't a subclass of TestCase? Specifically, what happens
142 # if testCaseClass is a subclass of TestSuite?
143 #
144 # This is checked for specifically in the code, so we better add a
145 # test for it.
146 def test_loadTestsFromTestCase__TestSuite_subclass(self):
147 class NotATestCase(unittest.TestSuite):
148 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000149
Georg Brandl15c5ce92007-03-07 09:09:40 +0000150 loader = unittest.TestLoader()
151 try:
152 loader.loadTestsFromTestCase(NotATestCase)
153 except TypeError:
154 pass
155 else:
156 self.fail('Should raise TypeError')
Tim Petersea5962f2007-03-12 18:07:52 +0000157
Georg Brandl15c5ce92007-03-07 09:09:40 +0000158 # "Return a suite of all tests cases contained in the TestCase-derived
159 # class testCaseClass"
160 #
161 # Make sure loadTestsFromTestCase() picks up the default test method
162 # name (as specified by TestCase), even though the method name does
163 # not match the default TestLoader.testMethodPrefix string
164 def test_loadTestsFromTestCase__default_method_name(self):
165 class Foo(unittest.TestCase):
166 def runTest(self):
167 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000168
Georg Brandl15c5ce92007-03-07 09:09:40 +0000169 loader = unittest.TestLoader()
170 # This has to be false for the test to succeed
171 self.failIf('runTest'.startswith(loader.testMethodPrefix))
Tim Petersea5962f2007-03-12 18:07:52 +0000172
Georg Brandl15c5ce92007-03-07 09:09:40 +0000173 suite = loader.loadTestsFromTestCase(Foo)
174 self.failUnless(isinstance(suite, loader.suiteClass))
175 self.assertEqual(list(suite), [Foo('runTest')])
176
177 ################################################################
178 ### /Tests for TestLoader.loadTestsFromTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000179
Georg Brandl15c5ce92007-03-07 09:09:40 +0000180 ### Tests for TestLoader.loadTestsFromModule
181 ################################################################
182
183 # "This method searches `module` for classes derived from TestCase"
184 def test_loadTestsFromModule__TestCase_subclass(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000185 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000186 class MyTestCase(unittest.TestCase):
187 def test(self):
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 expected = [loader.suiteClass([MyTestCase('test')])]
196 self.assertEqual(list(suite), expected)
Tim Petersea5962f2007-03-12 18:07:52 +0000197
Georg Brandl15c5ce92007-03-07 09:09:40 +0000198 # "This method searches `module` for classes derived from TestCase"
Tim Petersea5962f2007-03-12 18:07:52 +0000199 #
Georg Brandl15c5ce92007-03-07 09:09:40 +0000200 # What happens if no tests are found (no TestCase instances)?
201 def test_loadTestsFromModule__no_TestCase_instances(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000202 m = types.ModuleType('m')
Tim Petersea5962f2007-03-12 18:07:52 +0000203
Georg Brandl15c5ce92007-03-07 09:09:40 +0000204 loader = unittest.TestLoader()
205 suite = loader.loadTestsFromModule(m)
206 self.failUnless(isinstance(suite, loader.suiteClass))
207 self.assertEqual(list(suite), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000208
Georg Brandl15c5ce92007-03-07 09:09:40 +0000209 # "This method searches `module` for classes derived from TestCase"
210 #
Tim Petersea5962f2007-03-12 18:07:52 +0000211 # What happens if no tests are found (TestCases instances, but no tests)?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000212 def test_loadTestsFromModule__no_TestCase_tests(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000213 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000214 class MyTestCase(unittest.TestCase):
215 pass
216 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000217
Georg Brandl15c5ce92007-03-07 09:09:40 +0000218 loader = unittest.TestLoader()
219 suite = loader.loadTestsFromModule(m)
220 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000221
Georg Brandl15c5ce92007-03-07 09:09:40 +0000222 self.assertEqual(list(suite), [loader.suiteClass()])
Tim Petersea5962f2007-03-12 18:07:52 +0000223
Georg Brandl15c5ce92007-03-07 09:09:40 +0000224 # "This method searches `module` for classes derived from TestCase"s
225 #
226 # What happens if loadTestsFromModule() is given something other
227 # than a module?
228 #
229 # XXX Currently, it succeeds anyway. This flexibility
230 # should either be documented or loadTestsFromModule() should
231 # raise a TypeError
232 #
233 # XXX Certain people are using this behaviour. We'll add a test for it
234 def test_loadTestsFromModule__not_a_module(self):
235 class MyTestCase(unittest.TestCase):
236 def test(self):
237 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000238
Georg Brandl15c5ce92007-03-07 09:09:40 +0000239 class NotAModule(object):
240 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000241
Georg Brandl15c5ce92007-03-07 09:09:40 +0000242 loader = unittest.TestLoader()
243 suite = loader.loadTestsFromModule(NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000244
Georg Brandl15c5ce92007-03-07 09:09:40 +0000245 reference = [unittest.TestSuite([MyTestCase('test')])]
246 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000247
Georg Brandl15c5ce92007-03-07 09:09:40 +0000248 ################################################################
249 ### /Tests for TestLoader.loadTestsFromModule()
Tim Petersea5962f2007-03-12 18:07:52 +0000250
Georg Brandl15c5ce92007-03-07 09:09:40 +0000251 ### Tests for TestLoader.loadTestsFromName()
252 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000253
Georg Brandl15c5ce92007-03-07 09:09:40 +0000254 # "The specifier name is a ``dotted name'' that may resolve either to
255 # a module, a test case class, a TestSuite instance, a test method
256 # within a test case class, or a callable object which returns a
257 # TestCase or TestSuite instance."
258 #
259 # Is ValueError raised in response to an empty name?
260 def test_loadTestsFromName__empty_name(self):
261 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000262
Georg Brandl15c5ce92007-03-07 09:09:40 +0000263 try:
264 loader.loadTestsFromName('')
265 except ValueError, e:
266 self.assertEqual(str(e), "Empty module name")
267 else:
268 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000269
Georg Brandl15c5ce92007-03-07 09:09:40 +0000270 # "The specifier name is a ``dotted name'' that may resolve either to
271 # a module, a test case class, a TestSuite instance, a test method
272 # within a test case class, or a callable object which returns a
273 # TestCase or TestSuite instance."
274 #
Tim Petersea5962f2007-03-12 18:07:52 +0000275 # What happens when the name contains invalid characters?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000276 def test_loadTestsFromName__malformed_name(self):
277 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000278
Georg Brandl15c5ce92007-03-07 09:09:40 +0000279 # XXX Should this raise ValueError or ImportError?
280 try:
281 loader.loadTestsFromName('abc () //')
282 except ValueError:
283 pass
284 except ImportError:
285 pass
286 else:
287 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000288
Georg Brandl15c5ce92007-03-07 09:09:40 +0000289 # "The specifier name is a ``dotted name'' that may resolve ... to a
290 # module"
291 #
Tim Petersea5962f2007-03-12 18:07:52 +0000292 # What happens when a module by that name can't be found?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000293 def test_loadTestsFromName__unknown_module_name(self):
294 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000295
Georg Brandl15c5ce92007-03-07 09:09:40 +0000296 try:
297 loader.loadTestsFromName('sdasfasfasdf')
298 except ImportError, e:
299 self.assertEqual(str(e), "No module named sdasfasfasdf")
300 else:
301 self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
Tim Petersea5962f2007-03-12 18:07:52 +0000302
Georg Brandl15c5ce92007-03-07 09:09:40 +0000303 # "The specifier name is a ``dotted name'' that may resolve either to
304 # a module, a test case class, a TestSuite instance, a test method
305 # within a test case class, or a callable object which returns a
306 # TestCase or TestSuite instance."
307 #
Tim Petersea5962f2007-03-12 18:07:52 +0000308 # What happens when the module is found, but the attribute can't?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000309 def test_loadTestsFromName__unknown_attr_name(self):
310 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000311
Georg Brandl15c5ce92007-03-07 09:09:40 +0000312 try:
313 loader.loadTestsFromName('unittest.sdasfasfasdf')
314 except AttributeError, e:
315 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
316 else:
317 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000318
Georg Brandl15c5ce92007-03-07 09:09:40 +0000319 # "The specifier name is a ``dotted name'' that may resolve either to
320 # a module, a test case class, a TestSuite instance, a test method
321 # within a test case class, or a callable object which returns a
322 # TestCase or TestSuite instance."
323 #
324 # What happens when we provide the module, but the attribute can't be
325 # found?
326 def test_loadTestsFromName__relative_unknown_name(self):
327 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000328
Georg Brandl15c5ce92007-03-07 09:09:40 +0000329 try:
330 loader.loadTestsFromName('sdasfasfasdf', unittest)
331 except AttributeError, e:
332 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
333 else:
334 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000335
Georg Brandl15c5ce92007-03-07 09:09:40 +0000336 # "The specifier name is a ``dotted name'' that may resolve either to
337 # a module, a test case class, a TestSuite instance, a test method
338 # within a test case class, or a callable object which returns a
339 # TestCase or TestSuite instance."
340 # ...
341 # "The method optionally resolves name relative to the given module"
342 #
343 # Does loadTestsFromName raise ValueError when passed an empty
344 # name relative to a provided module?
345 #
346 # XXX Should probably raise a ValueError instead of an AttributeError
347 def test_loadTestsFromName__relative_empty_name(self):
348 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000349
Georg Brandl15c5ce92007-03-07 09:09:40 +0000350 try:
351 loader.loadTestsFromName('', unittest)
352 except AttributeError, e:
353 pass
354 else:
355 self.fail("Failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000356
Georg Brandl15c5ce92007-03-07 09:09:40 +0000357 # "The specifier name is a ``dotted name'' that may resolve either to
358 # a module, a test case class, a TestSuite instance, a test method
359 # within a test case class, or a callable object which returns a
360 # TestCase or TestSuite instance."
361 # ...
362 # "The method optionally resolves name relative to the given module"
363 #
364 # What happens when an impossible name is given, relative to the provided
365 # `module`?
366 def test_loadTestsFromName__relative_malformed_name(self):
367 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000368
Georg Brandl15c5ce92007-03-07 09:09:40 +0000369 # XXX Should this raise AttributeError or ValueError?
370 try:
371 loader.loadTestsFromName('abc () //', unittest)
372 except ValueError:
373 pass
374 except AttributeError:
375 pass
376 else:
377 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
378
379 # "The method optionally resolves name relative to the given module"
380 #
381 # Does loadTestsFromName raise TypeError when the `module` argument
382 # isn't a module object?
383 #
384 # XXX Accepts the not-a-module object, ignorning the object's type
385 # This should raise an exception or the method name should be changed
386 #
387 # XXX Some people are relying on this, so keep it for now
388 def test_loadTestsFromName__relative_not_a_module(self):
389 class MyTestCase(unittest.TestCase):
390 def test(self):
391 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000392
Georg Brandl15c5ce92007-03-07 09:09:40 +0000393 class NotAModule(object):
394 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000395
Georg Brandl15c5ce92007-03-07 09:09:40 +0000396 loader = unittest.TestLoader()
397 suite = loader.loadTestsFromName('test_2', NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000398
Georg Brandl15c5ce92007-03-07 09:09:40 +0000399 reference = [MyTestCase('test')]
400 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000401
Georg Brandl15c5ce92007-03-07 09:09:40 +0000402 # "The specifier name is a ``dotted name'' that may resolve either to
403 # a module, a test case class, a TestSuite instance, a test method
404 # within a test case class, or a callable object which returns a
405 # TestCase or TestSuite instance."
406 #
407 # Does it raise an exception if the name resolves to an invalid
408 # object?
409 def test_loadTestsFromName__relative_bad_object(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000410 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000411 m.testcase_1 = object()
Tim Petersea5962f2007-03-12 18:07:52 +0000412
Georg Brandl15c5ce92007-03-07 09:09:40 +0000413 loader = unittest.TestLoader()
414 try:
415 loader.loadTestsFromName('testcase_1', m)
416 except TypeError:
417 pass
418 else:
419 self.fail("Should have raised TypeError")
420
421 # "The specifier name is a ``dotted name'' that may
422 # resolve either to ... a test case class"
423 def test_loadTestsFromName__relative_TestCase_subclass(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000424 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000425 class MyTestCase(unittest.TestCase):
426 def test(self):
427 pass
428 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000429
Georg Brandl15c5ce92007-03-07 09:09:40 +0000430 loader = unittest.TestLoader()
431 suite = loader.loadTestsFromName('testcase_1', m)
432 self.failUnless(isinstance(suite, loader.suiteClass))
433 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000434
Georg Brandl15c5ce92007-03-07 09:09:40 +0000435 # "The specifier name is a ``dotted name'' that may resolve either to
436 # a module, a test case class, a TestSuite instance, a test method
437 # within a test case class, or a callable object which returns a
438 # TestCase or TestSuite instance."
439 def test_loadTestsFromName__relative_TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000440 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000441 class MyTestCase(unittest.TestCase):
442 def test(self):
443 pass
444 m.testsuite = unittest.TestSuite([MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000445
Georg Brandl15c5ce92007-03-07 09:09:40 +0000446 loader = unittest.TestLoader()
447 suite = loader.loadTestsFromName('testsuite', m)
448 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000449
Georg Brandl15c5ce92007-03-07 09:09:40 +0000450 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000451
Georg Brandl15c5ce92007-03-07 09:09:40 +0000452 # "The specifier name is a ``dotted name'' that may resolve ... to
453 # ... a test method within a test case class"
454 def test_loadTestsFromName__relative_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000455 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000456 class MyTestCase(unittest.TestCase):
457 def test(self):
458 pass
459 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000460
Georg Brandl15c5ce92007-03-07 09:09:40 +0000461 loader = unittest.TestLoader()
462 suite = loader.loadTestsFromName('testcase_1.test', m)
463 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000464
Georg Brandl15c5ce92007-03-07 09:09:40 +0000465 self.assertEqual(list(suite), [MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000466
Georg Brandl15c5ce92007-03-07 09:09:40 +0000467 # "The specifier name is a ``dotted name'' that may resolve either to
468 # a module, a test case class, a TestSuite instance, a test method
469 # within a test case class, or a callable object which returns a
470 # TestCase or TestSuite instance."
471 #
472 # Does loadTestsFromName() raise the proper exception when trying to
473 # resolve "a test method within a test case class" that doesn't exist
474 # for the given name (relative to a provided module)?
475 def test_loadTestsFromName__relative_invalid_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000476 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000477 class MyTestCase(unittest.TestCase):
478 def test(self):
479 pass
480 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000481
Georg Brandl15c5ce92007-03-07 09:09:40 +0000482 loader = unittest.TestLoader()
483 try:
484 loader.loadTestsFromName('testcase_1.testfoo', m)
485 except AttributeError, e:
486 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
487 else:
488 self.fail("Failed to raise AttributeError")
489
490 # "The specifier name is a ``dotted name'' that may resolve ... to
491 # ... a callable object which returns a ... TestSuite instance"
492 def test_loadTestsFromName__callable__TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000493 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000494 testcase_1 = unittest.FunctionTestCase(lambda: None)
495 testcase_2 = unittest.FunctionTestCase(lambda: None)
496 def return_TestSuite():
497 return unittest.TestSuite([testcase_1, testcase_2])
498 m.return_TestSuite = return_TestSuite
Tim Petersea5962f2007-03-12 18:07:52 +0000499
Georg Brandl15c5ce92007-03-07 09:09:40 +0000500 loader = unittest.TestLoader()
501 suite = loader.loadTestsFromName('return_TestSuite', m)
502 self.failUnless(isinstance(suite, loader.suiteClass))
503 self.assertEqual(list(suite), [testcase_1, testcase_2])
Tim Petersea5962f2007-03-12 18:07:52 +0000504
Georg Brandl15c5ce92007-03-07 09:09:40 +0000505 # "The specifier name is a ``dotted name'' that may resolve ... to
506 # ... a callable object which returns a TestCase ... instance"
507 def test_loadTestsFromName__callable__TestCase_instance(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000508 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000509 testcase_1 = unittest.FunctionTestCase(lambda: None)
510 def return_TestCase():
511 return testcase_1
512 m.return_TestCase = return_TestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000513
Georg Brandl15c5ce92007-03-07 09:09:40 +0000514 loader = unittest.TestLoader()
515 suite = loader.loadTestsFromName('return_TestCase', m)
516 self.failUnless(isinstance(suite, loader.suiteClass))
517 self.assertEqual(list(suite), [testcase_1])
Tim Petersea5962f2007-03-12 18:07:52 +0000518
Georg Brandl15c5ce92007-03-07 09:09:40 +0000519 # "The specifier name is a ``dotted name'' that may resolve ... to
520 # ... a callable object which returns a TestCase or TestSuite instance"
521 #
522 # What happens if the callable returns something else?
523 def test_loadTestsFromName__callable__wrong_type(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000524 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000525 def return_wrong():
526 return 6
527 m.return_wrong = return_wrong
Tim Petersea5962f2007-03-12 18:07:52 +0000528
Georg Brandl15c5ce92007-03-07 09:09:40 +0000529 loader = unittest.TestLoader()
530 try:
531 suite = loader.loadTestsFromName('return_wrong', m)
532 except TypeError:
533 pass
534 else:
535 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000536
Georg Brandl15c5ce92007-03-07 09:09:40 +0000537 # "The specifier can refer to modules and packages which have not been
Tim Petersea5962f2007-03-12 18:07:52 +0000538 # imported; they will be imported as a side-effect"
Georg Brandl15c5ce92007-03-07 09:09:40 +0000539 def test_loadTestsFromName__module_not_loaded(self):
540 # We're going to try to load this module as a side-effect, so it
541 # better not be loaded before we try.
542 #
543 # Why pick audioop? Google shows it isn't used very often, so there's
544 # a good chance that it won't be imported when this test is run
545 module_name = 'audioop'
Tim Petersea5962f2007-03-12 18:07:52 +0000546
Georg Brandl15c5ce92007-03-07 09:09:40 +0000547 import sys
548 if module_name in sys.modules:
549 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000550
Georg Brandl15c5ce92007-03-07 09:09:40 +0000551 loader = unittest.TestLoader()
552 try:
553 suite = loader.loadTestsFromName(module_name)
554
555 self.failUnless(isinstance(suite, loader.suiteClass))
556 self.assertEqual(list(suite), [])
557
558 # audioop should now be loaded, thanks to loadTestsFromName()
559 self.failUnless(module_name in sys.modules)
560 finally:
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +0000561 if module_name in sys.modules:
562 del sys.modules[module_name]
Georg Brandl15c5ce92007-03-07 09:09:40 +0000563
564 ################################################################
565 ### Tests for TestLoader.loadTestsFromName()
566
567 ### Tests for TestLoader.loadTestsFromNames()
568 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000569
Georg Brandl15c5ce92007-03-07 09:09:40 +0000570 # "Similar to loadTestsFromName(), but takes a sequence of names rather
571 # than a single name."
572 #
573 # What happens if that sequence of names is empty?
574 def test_loadTestsFromNames__empty_name_list(self):
575 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000576
Georg Brandl15c5ce92007-03-07 09:09:40 +0000577 suite = loader.loadTestsFromNames([])
578 self.failUnless(isinstance(suite, loader.suiteClass))
579 self.assertEqual(list(suite), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000580
Georg Brandl15c5ce92007-03-07 09:09:40 +0000581 # "Similar to loadTestsFromName(), but takes a sequence of names rather
582 # than a single name."
583 # ...
584 # "The method optionally resolves name relative to the given module"
585 #
586 # What happens if that sequence of names is empty?
587 #
588 # XXX Should this raise a ValueError or just return an empty TestSuite?
589 def test_loadTestsFromNames__relative_empty_name_list(self):
590 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000591
Georg Brandl15c5ce92007-03-07 09:09:40 +0000592 suite = loader.loadTestsFromNames([], unittest)
593 self.failUnless(isinstance(suite, loader.suiteClass))
594 self.assertEqual(list(suite), [])
595
596 # "The specifier name is a ``dotted name'' that may resolve either to
597 # a module, a test case class, a TestSuite instance, a test method
598 # within a test case class, or a callable object which returns a
599 # TestCase or TestSuite instance."
600 #
601 # Is ValueError raised in response to an empty name?
602 def test_loadTestsFromNames__empty_name(self):
603 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000604
Georg Brandl15c5ce92007-03-07 09:09:40 +0000605 try:
606 loader.loadTestsFromNames([''])
607 except ValueError, e:
608 self.assertEqual(str(e), "Empty module name")
609 else:
610 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000611
Georg Brandl15c5ce92007-03-07 09:09:40 +0000612 # "The specifier name is a ``dotted name'' that may resolve either to
613 # a module, a test case class, a TestSuite instance, a test method
614 # within a test case class, or a callable object which returns a
615 # TestCase or TestSuite instance."
616 #
Tim Petersea5962f2007-03-12 18:07:52 +0000617 # What happens when presented with an impossible module name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000618 def test_loadTestsFromNames__malformed_name(self):
619 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000620
Georg Brandl15c5ce92007-03-07 09:09:40 +0000621 # XXX Should this raise ValueError or ImportError?
622 try:
623 loader.loadTestsFromNames(['abc () //'])
624 except ValueError:
625 pass
626 except ImportError:
627 pass
628 else:
629 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000630
Georg Brandl15c5ce92007-03-07 09:09:40 +0000631 # "The specifier name is a ``dotted name'' that may resolve either to
632 # a module, a test case class, a TestSuite instance, a test method
633 # within a test case class, or a callable object which returns a
634 # TestCase or TestSuite instance."
635 #
Tim Petersea5962f2007-03-12 18:07:52 +0000636 # What happens when no module can be found for the given name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000637 def test_loadTestsFromNames__unknown_module_name(self):
638 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000639
Georg Brandl15c5ce92007-03-07 09:09:40 +0000640 try:
641 loader.loadTestsFromNames(['sdasfasfasdf'])
642 except ImportError, e:
643 self.assertEqual(str(e), "No module named sdasfasfasdf")
644 else:
645 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
Tim Petersea5962f2007-03-12 18:07:52 +0000646
Georg Brandl15c5ce92007-03-07 09:09:40 +0000647 # "The specifier name is a ``dotted name'' that may resolve either to
648 # a module, a test case class, a TestSuite instance, a test method
649 # within a test case class, or a callable object which returns a
650 # TestCase or TestSuite instance."
651 #
Tim Petersea5962f2007-03-12 18:07:52 +0000652 # What happens when the module can be found, but not the attribute?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000653 def test_loadTestsFromNames__unknown_attr_name(self):
654 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000655
Georg Brandl15c5ce92007-03-07 09:09:40 +0000656 try:
657 loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
658 except AttributeError, e:
659 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
660 else:
661 self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000662
Georg Brandl15c5ce92007-03-07 09:09:40 +0000663 # "The specifier name is a ``dotted name'' that may resolve either to
664 # a module, a test case class, a TestSuite instance, a test method
665 # within a test case class, or a callable object which returns a
666 # TestCase or TestSuite instance."
667 # ...
668 # "The method optionally resolves name relative to the given module"
669 #
670 # What happens when given an unknown attribute on a specified `module`
671 # argument?
672 def test_loadTestsFromNames__unknown_name_relative_1(self):
673 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000674
Georg Brandl15c5ce92007-03-07 09:09:40 +0000675 try:
676 loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
677 except AttributeError, e:
678 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
679 else:
680 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000681
Georg Brandl15c5ce92007-03-07 09:09:40 +0000682 # "The specifier name is a ``dotted name'' that may resolve either to
683 # a module, a test case class, a TestSuite instance, a test method
684 # within a test case class, or a callable object which returns a
685 # TestCase or TestSuite instance."
686 # ...
687 # "The method optionally resolves name relative to the given module"
688 #
689 # Do unknown attributes (relative to a provided module) still raise an
Tim Petersea5962f2007-03-12 18:07:52 +0000690 # exception even in the presence of valid attribute names?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000691 def test_loadTestsFromNames__unknown_name_relative_2(self):
692 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000693
Georg Brandl15c5ce92007-03-07 09:09:40 +0000694 try:
695 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
696 except AttributeError, e:
697 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
698 else:
699 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
700
701 # "The specifier name is a ``dotted name'' that may resolve either to
702 # a module, a test case class, a TestSuite instance, a test method
703 # within a test case class, or a callable object which returns a
704 # TestCase or TestSuite instance."
705 # ...
706 # "The method optionally resolves name relative to the given module"
707 #
708 # What happens when faced with the empty string?
709 #
710 # XXX This currently raises AttributeError, though ValueError is probably
711 # more appropriate
712 def test_loadTestsFromNames__relative_empty_name(self):
713 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000714
Georg Brandl15c5ce92007-03-07 09:09:40 +0000715 try:
716 loader.loadTestsFromNames([''], unittest)
717 except AttributeError:
718 pass
719 else:
720 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +0000721
Georg Brandl15c5ce92007-03-07 09:09:40 +0000722 # "The specifier name is a ``dotted name'' that may resolve either to
723 # a module, a test case class, a TestSuite instance, a test method
724 # within a test case class, or a callable object which returns a
725 # TestCase or TestSuite instance."
726 # ...
727 # "The method optionally resolves name relative to the given module"
728 #
Tim Petersea5962f2007-03-12 18:07:52 +0000729 # What happens when presented with an impossible attribute name?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000730 def test_loadTestsFromNames__relative_malformed_name(self):
731 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000732
Georg Brandl15c5ce92007-03-07 09:09:40 +0000733 # XXX Should this raise AttributeError or ValueError?
734 try:
735 loader.loadTestsFromNames(['abc () //'], unittest)
736 except AttributeError:
737 pass
738 except ValueError:
739 pass
740 else:
741 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
742
743 # "The method optionally resolves name relative to the given module"
744 #
745 # Does loadTestsFromNames() make sure the provided `module` is in fact
746 # a module?
747 #
748 # XXX This validation is currently not done. This flexibility should
749 # either be documented or a TypeError should be raised.
750 def test_loadTestsFromNames__relative_not_a_module(self):
751 class MyTestCase(unittest.TestCase):
752 def test(self):
753 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000754
Georg Brandl15c5ce92007-03-07 09:09:40 +0000755 class NotAModule(object):
756 test_2 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000757
Georg Brandl15c5ce92007-03-07 09:09:40 +0000758 loader = unittest.TestLoader()
759 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
Tim Petersea5962f2007-03-12 18:07:52 +0000760
Georg Brandl15c5ce92007-03-07 09:09:40 +0000761 reference = [unittest.TestSuite([MyTestCase('test')])]
762 self.assertEqual(list(suite), reference)
Tim Petersea5962f2007-03-12 18:07:52 +0000763
Georg Brandl15c5ce92007-03-07 09:09:40 +0000764 # "The specifier name is a ``dotted name'' that may resolve either to
765 # a module, a test case class, a TestSuite instance, a test method
766 # within a test case class, or a callable object which returns a
767 # TestCase or TestSuite instance."
768 #
769 # Does it raise an exception if the name resolves to an invalid
770 # object?
771 def test_loadTestsFromNames__relative_bad_object(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000772 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000773 m.testcase_1 = object()
Tim Petersea5962f2007-03-12 18:07:52 +0000774
Georg Brandl15c5ce92007-03-07 09:09:40 +0000775 loader = unittest.TestLoader()
776 try:
777 loader.loadTestsFromNames(['testcase_1'], m)
778 except TypeError:
779 pass
780 else:
781 self.fail("Should have raised TypeError")
782
783 # "The specifier name is a ``dotted name'' that may resolve ... to
784 # ... a test case class"
785 def test_loadTestsFromNames__relative_TestCase_subclass(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000786 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000787 class MyTestCase(unittest.TestCase):
788 def test(self):
789 pass
790 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000791
Georg Brandl15c5ce92007-03-07 09:09:40 +0000792 loader = unittest.TestLoader()
793 suite = loader.loadTestsFromNames(['testcase_1'], m)
794 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000795
Georg Brandl15c5ce92007-03-07 09:09:40 +0000796 expected = loader.suiteClass([MyTestCase('test')])
797 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000798
Georg Brandl15c5ce92007-03-07 09:09:40 +0000799 # "The specifier name is a ``dotted name'' that may resolve ... to
800 # ... a TestSuite instance"
801 def test_loadTestsFromNames__relative_TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000802 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000803 class MyTestCase(unittest.TestCase):
804 def test(self):
805 pass
806 m.testsuite = unittest.TestSuite([MyTestCase('test')])
Tim Petersea5962f2007-03-12 18:07:52 +0000807
Georg Brandl15c5ce92007-03-07 09:09:40 +0000808 loader = unittest.TestLoader()
809 suite = loader.loadTestsFromNames(['testsuite'], m)
810 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000811
Georg Brandl15c5ce92007-03-07 09:09:40 +0000812 self.assertEqual(list(suite), [m.testsuite])
Tim Petersea5962f2007-03-12 18:07:52 +0000813
Georg Brandl15c5ce92007-03-07 09:09:40 +0000814 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
815 # test method within a test case class"
816 def test_loadTestsFromNames__relative_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000817 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000818 class MyTestCase(unittest.TestCase):
819 def test(self):
820 pass
821 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000822
Georg Brandl15c5ce92007-03-07 09:09:40 +0000823 loader = unittest.TestLoader()
824 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
825 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000826
Georg Brandl15c5ce92007-03-07 09:09:40 +0000827 ref_suite = unittest.TestSuite([MyTestCase('test')])
828 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000829
Georg Brandl15c5ce92007-03-07 09:09:40 +0000830 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
831 # test method within a test case class"
832 #
833 # Does the method gracefully handle names that initially look like they
Tim Petersea5962f2007-03-12 18:07:52 +0000834 # resolve to "a test method within a test case class" but don't?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000835 def test_loadTestsFromNames__relative_invalid_testmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000836 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000837 class MyTestCase(unittest.TestCase):
838 def test(self):
839 pass
840 m.testcase_1 = MyTestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000841
Georg Brandl15c5ce92007-03-07 09:09:40 +0000842 loader = unittest.TestLoader()
843 try:
844 loader.loadTestsFromNames(['testcase_1.testfoo'], m)
845 except AttributeError, e:
846 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
847 else:
848 self.fail("Failed to raise AttributeError")
849
850 # "The specifier name is a ``dotted name'' that may resolve ... to
851 # ... a callable object which returns a ... TestSuite instance"
852 def test_loadTestsFromNames__callable__TestSuite(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000853 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000854 testcase_1 = unittest.FunctionTestCase(lambda: None)
855 testcase_2 = unittest.FunctionTestCase(lambda: None)
856 def return_TestSuite():
857 return unittest.TestSuite([testcase_1, testcase_2])
858 m.return_TestSuite = return_TestSuite
Tim Petersea5962f2007-03-12 18:07:52 +0000859
Georg Brandl15c5ce92007-03-07 09:09:40 +0000860 loader = unittest.TestLoader()
861 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
862 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000863
Georg Brandl15c5ce92007-03-07 09:09:40 +0000864 expected = unittest.TestSuite([testcase_1, testcase_2])
865 self.assertEqual(list(suite), [expected])
Tim Petersea5962f2007-03-12 18:07:52 +0000866
Georg Brandl15c5ce92007-03-07 09:09:40 +0000867 # "The specifier name is a ``dotted name'' that may resolve ... to
868 # ... a callable object which returns a TestCase ... instance"
869 def test_loadTestsFromNames__callable__TestCase_instance(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000870 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000871 testcase_1 = unittest.FunctionTestCase(lambda: None)
872 def return_TestCase():
873 return testcase_1
874 m.return_TestCase = return_TestCase
Tim Petersea5962f2007-03-12 18:07:52 +0000875
Georg Brandl15c5ce92007-03-07 09:09:40 +0000876 loader = unittest.TestLoader()
877 suite = loader.loadTestsFromNames(['return_TestCase'], m)
878 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000879
Georg Brandl15c5ce92007-03-07 09:09:40 +0000880 ref_suite = unittest.TestSuite([testcase_1])
881 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000882
Georg Brandl15c5ce92007-03-07 09:09:40 +0000883 # "The specifier name is a ``dotted name'' that may resolve ... to
884 # ... a callable object which returns a TestCase or TestSuite instance"
885 #
Tim Petersea5962f2007-03-12 18:07:52 +0000886 # Are staticmethods handled correctly?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000887 def test_loadTestsFromNames__callable__call_staticmethod(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000888 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000889 class Test1(unittest.TestCase):
890 def test(self):
891 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000892
Georg Brandl15c5ce92007-03-07 09:09:40 +0000893 testcase_1 = Test1('test')
894 class Foo(unittest.TestCase):
895 @staticmethod
896 def foo():
897 return testcase_1
898 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +0000899
Georg Brandl15c5ce92007-03-07 09:09:40 +0000900 loader = unittest.TestLoader()
901 suite = loader.loadTestsFromNames(['Foo.foo'], m)
902 self.failUnless(isinstance(suite, loader.suiteClass))
Tim Petersea5962f2007-03-12 18:07:52 +0000903
Georg Brandl15c5ce92007-03-07 09:09:40 +0000904 ref_suite = unittest.TestSuite([testcase_1])
905 self.assertEqual(list(suite), [ref_suite])
Tim Petersea5962f2007-03-12 18:07:52 +0000906
Georg Brandl15c5ce92007-03-07 09:09:40 +0000907 # "The specifier name is a ``dotted name'' that may resolve ... to
908 # ... a callable object which returns a TestCase or TestSuite instance"
909 #
910 # What happens when the callable returns something else?
911 def test_loadTestsFromNames__callable__wrong_type(self):
Christian Heimesc756d002007-11-27 21:34:01 +0000912 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +0000913 def return_wrong():
914 return 6
915 m.return_wrong = return_wrong
Tim Petersea5962f2007-03-12 18:07:52 +0000916
Georg Brandl15c5ce92007-03-07 09:09:40 +0000917 loader = unittest.TestLoader()
918 try:
919 suite = loader.loadTestsFromNames(['return_wrong'], m)
920 except TypeError:
921 pass
922 else:
923 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +0000924
Georg Brandl15c5ce92007-03-07 09:09:40 +0000925 # "The specifier can refer to modules and packages which have not been
Tim Petersea5962f2007-03-12 18:07:52 +0000926 # imported; they will be imported as a side-effect"
Georg Brandl15c5ce92007-03-07 09:09:40 +0000927 def test_loadTestsFromNames__module_not_loaded(self):
928 # We're going to try to load this module as a side-effect, so it
929 # better not be loaded before we try.
930 #
931 # Why pick audioop? Google shows it isn't used very often, so there's
932 # a good chance that it won't be imported when this test is run
933 module_name = 'audioop'
Tim Petersea5962f2007-03-12 18:07:52 +0000934
Georg Brandl15c5ce92007-03-07 09:09:40 +0000935 import sys
936 if module_name in sys.modules:
937 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000938
Georg Brandl15c5ce92007-03-07 09:09:40 +0000939 loader = unittest.TestLoader()
940 try:
941 suite = loader.loadTestsFromNames([module_name])
942
943 self.failUnless(isinstance(suite, loader.suiteClass))
944 self.assertEqual(list(suite), [unittest.TestSuite()])
945
946 # audioop should now be loaded, thanks to loadTestsFromName()
947 self.failUnless(module_name in sys.modules)
948 finally:
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +0000949 if module_name in sys.modules:
950 del sys.modules[module_name]
Tim Petersea5962f2007-03-12 18:07:52 +0000951
Georg Brandl15c5ce92007-03-07 09:09:40 +0000952 ################################################################
953 ### /Tests for TestLoader.loadTestsFromNames()
954
955 ### Tests for TestLoader.getTestCaseNames()
956 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +0000957
Georg Brandl15c5ce92007-03-07 09:09:40 +0000958 # "Return a sorted sequence of method names found within testCaseClass"
959 #
960 # Test.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +0000961 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +0000962 def test_getTestCaseNames(self):
963 class Test(unittest.TestCase):
964 def test_1(self): pass
965 def test_2(self): pass
966 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000967
Georg Brandl15c5ce92007-03-07 09:09:40 +0000968 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000969
Georg Brandl15c5ce92007-03-07 09:09:40 +0000970 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
Tim Petersea5962f2007-03-12 18:07:52 +0000971
Georg Brandl15c5ce92007-03-07 09:09:40 +0000972 # "Return a sorted sequence of method names found within testCaseClass"
973 #
Tim Petersea5962f2007-03-12 18:07:52 +0000974 # Does getTestCaseNames() behave appropriately if no tests are found?
Georg Brandl15c5ce92007-03-07 09:09:40 +0000975 def test_getTestCaseNames__no_tests(self):
976 class Test(unittest.TestCase):
977 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +0000978
Georg Brandl15c5ce92007-03-07 09:09:40 +0000979 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +0000980
Georg Brandl15c5ce92007-03-07 09:09:40 +0000981 self.assertEqual(loader.getTestCaseNames(Test), [])
Tim Petersea5962f2007-03-12 18:07:52 +0000982
Georg Brandl15c5ce92007-03-07 09:09:40 +0000983 # "Return a sorted sequence of method names found within testCaseClass"
984 #
985 # Are not-TestCases handled gracefully?
986 #
987 # XXX This should raise a TypeError, not return a list
988 #
989 # XXX It's too late in the 2.5 release cycle to fix this, but it should
990 # probably be revisited for 2.6
991 def test_getTestCaseNames__not_a_TestCase(self):
992 class BadCase(int):
993 def test_foo(self):
994 pass
Tim Petersea5962f2007-03-12 18:07:52 +0000995
Georg Brandl15c5ce92007-03-07 09:09:40 +0000996 loader = unittest.TestLoader()
997 names = loader.getTestCaseNames(BadCase)
Tim Petersea5962f2007-03-12 18:07:52 +0000998
Georg Brandl15c5ce92007-03-07 09:09:40 +0000999 self.assertEqual(names, ['test_foo'])
Tim Petersea5962f2007-03-12 18:07:52 +00001000
Georg Brandl15c5ce92007-03-07 09:09:40 +00001001 # "Return a sorted sequence of method names found within testCaseClass"
1002 #
1003 # Make sure inherited names are handled.
1004 #
1005 # TestP.foobar is defined to make sure getTestCaseNames() respects
Tim Petersea5962f2007-03-12 18:07:52 +00001006 # loader.testMethodPrefix
Georg Brandl15c5ce92007-03-07 09:09:40 +00001007 def test_getTestCaseNames__inheritance(self):
1008 class TestP(unittest.TestCase):
1009 def test_1(self): pass
1010 def test_2(self): pass
1011 def foobar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001012
Georg Brandl15c5ce92007-03-07 09:09:40 +00001013 class TestC(TestP):
1014 def test_1(self): pass
1015 def test_3(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001016
Georg Brandl15c5ce92007-03-07 09:09:40 +00001017 loader = unittest.TestLoader()
Tim Petersea5962f2007-03-12 18:07:52 +00001018
Georg Brandl15c5ce92007-03-07 09:09:40 +00001019 names = ['test_1', 'test_2', 'test_3']
1020 self.assertEqual(loader.getTestCaseNames(TestC), names)
Tim Petersea5962f2007-03-12 18:07:52 +00001021
1022 ################################################################
Georg Brandl15c5ce92007-03-07 09:09:40 +00001023 ### /Tests for TestLoader.getTestCaseNames()
1024
1025 ### Tests for TestLoader.testMethodPrefix
1026 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001027
Georg Brandl15c5ce92007-03-07 09:09:40 +00001028 # "String giving the prefix of method names which will be interpreted as
1029 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001030 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001031 # Implicit in the documentation is that testMethodPrefix is respected by
1032 # all loadTestsFrom* methods.
1033 def test_testMethodPrefix__loadTestsFromTestCase(self):
1034 class Foo(unittest.TestCase):
1035 def test_1(self): pass
1036 def test_2(self): pass
1037 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001038
Georg Brandl15c5ce92007-03-07 09:09:40 +00001039 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1040 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001041
Georg Brandl15c5ce92007-03-07 09:09:40 +00001042 loader = unittest.TestLoader()
1043 loader.testMethodPrefix = 'foo'
1044 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1045
1046 loader.testMethodPrefix = 'test'
1047 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001048
Georg Brandl15c5ce92007-03-07 09:09:40 +00001049 # "String giving the prefix of method names which will be interpreted as
1050 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001051 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001052 # Implicit in the documentation is that testMethodPrefix is respected by
1053 # all loadTestsFrom* methods.
1054 def test_testMethodPrefix__loadTestsFromModule(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001055 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001056 class Foo(unittest.TestCase):
1057 def test_1(self): pass
1058 def test_2(self): pass
1059 def foo_bar(self): pass
1060 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001061
Georg Brandl15c5ce92007-03-07 09:09:40 +00001062 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
1063 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
Tim Petersea5962f2007-03-12 18:07:52 +00001064
Georg Brandl15c5ce92007-03-07 09:09:40 +00001065 loader = unittest.TestLoader()
1066 loader.testMethodPrefix = 'foo'
1067 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1068
1069 loader.testMethodPrefix = 'test'
1070 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001071
Georg Brandl15c5ce92007-03-07 09:09:40 +00001072 # "String giving the prefix of method names which will be interpreted as
1073 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001074 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001075 # Implicit in the documentation is that testMethodPrefix is respected by
1076 # all loadTestsFrom* methods.
1077 def test_testMethodPrefix__loadTestsFromName(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001078 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001079 class Foo(unittest.TestCase):
1080 def test_1(self): pass
1081 def test_2(self): pass
1082 def foo_bar(self): pass
1083 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001084
Georg Brandl15c5ce92007-03-07 09:09:40 +00001085 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1086 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
Tim Petersea5962f2007-03-12 18:07:52 +00001087
Georg Brandl15c5ce92007-03-07 09:09:40 +00001088 loader = unittest.TestLoader()
1089 loader.testMethodPrefix = 'foo'
1090 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1091
1092 loader.testMethodPrefix = 'test'
1093 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001094
Georg Brandl15c5ce92007-03-07 09:09:40 +00001095 # "String giving the prefix of method names which will be interpreted as
1096 # test methods"
Tim Petersea5962f2007-03-12 18:07:52 +00001097 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001098 # Implicit in the documentation is that testMethodPrefix is respected by
1099 # all loadTestsFrom* methods.
1100 def test_testMethodPrefix__loadTestsFromNames(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001101 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001102 class Foo(unittest.TestCase):
1103 def test_1(self): pass
1104 def test_2(self): pass
1105 def foo_bar(self): pass
1106 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001107
Georg Brandl15c5ce92007-03-07 09:09:40 +00001108 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
1109 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1110 tests_2 = unittest.TestSuite([tests_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001111
Georg Brandl15c5ce92007-03-07 09:09:40 +00001112 loader = unittest.TestLoader()
1113 loader.testMethodPrefix = 'foo'
1114 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1115
1116 loader.testMethodPrefix = 'test'
1117 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001118
Georg Brandl15c5ce92007-03-07 09:09:40 +00001119 # "The default value is 'test'"
1120 def test_testMethodPrefix__default_value(self):
1121 loader = unittest.TestLoader()
1122 self.failUnless(loader.testMethodPrefix == 'test')
Tim Petersea5962f2007-03-12 18:07:52 +00001123
Georg Brandl15c5ce92007-03-07 09:09:40 +00001124 ################################################################
1125 ### /Tests for TestLoader.testMethodPrefix
1126
Tim Petersea5962f2007-03-12 18:07:52 +00001127 ### Tests for TestLoader.sortTestMethodsUsing
Georg Brandl15c5ce92007-03-07 09:09:40 +00001128 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001129
Georg Brandl15c5ce92007-03-07 09:09:40 +00001130 # "Function to be used to compare method names when sorting them in
1131 # getTestCaseNames() and all the loadTestsFromX() methods"
1132 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1133 def reversed_cmp(x, y):
1134 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001135
Georg Brandl15c5ce92007-03-07 09:09:40 +00001136 class Foo(unittest.TestCase):
1137 def test_1(self): pass
1138 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001139
Georg Brandl15c5ce92007-03-07 09:09:40 +00001140 loader = unittest.TestLoader()
1141 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001142
Georg Brandl15c5ce92007-03-07 09:09:40 +00001143 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1144 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001145
Georg Brandl15c5ce92007-03-07 09:09:40 +00001146 # "Function to be used to compare method names when sorting them in
1147 # getTestCaseNames() and all the loadTestsFromX() methods"
1148 def test_sortTestMethodsUsing__loadTestsFromModule(self):
1149 def reversed_cmp(x, y):
1150 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001151
Christian Heimesc756d002007-11-27 21:34:01 +00001152 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001153 class Foo(unittest.TestCase):
1154 def test_1(self): pass
1155 def test_2(self): pass
1156 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001157
Georg Brandl15c5ce92007-03-07 09:09:40 +00001158 loader = unittest.TestLoader()
1159 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001160
Georg Brandl15c5ce92007-03-07 09:09:40 +00001161 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1162 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001163
Georg Brandl15c5ce92007-03-07 09:09:40 +00001164 # "Function to be used to compare method names when sorting them in
1165 # getTestCaseNames() and all the loadTestsFromX() methods"
1166 def test_sortTestMethodsUsing__loadTestsFromName(self):
1167 def reversed_cmp(x, y):
1168 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001169
Christian Heimesc756d002007-11-27 21:34:01 +00001170 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001171 class Foo(unittest.TestCase):
1172 def test_1(self): pass
1173 def test_2(self): pass
1174 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001175
Georg Brandl15c5ce92007-03-07 09:09:40 +00001176 loader = unittest.TestLoader()
1177 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001178
Georg Brandl15c5ce92007-03-07 09:09:40 +00001179 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1180 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001181
Georg Brandl15c5ce92007-03-07 09:09:40 +00001182 # "Function to be used to compare method names when sorting them in
1183 # getTestCaseNames() and all the loadTestsFromX() methods"
1184 def test_sortTestMethodsUsing__loadTestsFromNames(self):
1185 def reversed_cmp(x, y):
1186 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001187
Christian Heimesc756d002007-11-27 21:34:01 +00001188 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001189 class Foo(unittest.TestCase):
1190 def test_1(self): pass
1191 def test_2(self): pass
1192 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001193
Georg Brandl15c5ce92007-03-07 09:09:40 +00001194 loader = unittest.TestLoader()
1195 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001196
Georg Brandl15c5ce92007-03-07 09:09:40 +00001197 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1198 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001199
Georg Brandl15c5ce92007-03-07 09:09:40 +00001200 # "Function to be used to compare method names when sorting them in
1201 # getTestCaseNames()"
1202 #
1203 # Does it actually affect getTestCaseNames()?
1204 def test_sortTestMethodsUsing__getTestCaseNames(self):
1205 def reversed_cmp(x, y):
1206 return -cmp(x, y)
Tim Petersea5962f2007-03-12 18:07:52 +00001207
Georg Brandl15c5ce92007-03-07 09:09:40 +00001208 class Foo(unittest.TestCase):
1209 def test_1(self): pass
1210 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001211
Georg Brandl15c5ce92007-03-07 09:09:40 +00001212 loader = unittest.TestLoader()
1213 loader.sortTestMethodsUsing = reversed_cmp
Tim Petersea5962f2007-03-12 18:07:52 +00001214
Georg Brandl15c5ce92007-03-07 09:09:40 +00001215 test_names = ['test_2', 'test_1']
1216 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
Tim Petersea5962f2007-03-12 18:07:52 +00001217
Georg Brandl15c5ce92007-03-07 09:09:40 +00001218 # "The default value is the built-in cmp() function"
1219 def test_sortTestMethodsUsing__default_value(self):
1220 loader = unittest.TestLoader()
1221 self.failUnless(loader.sortTestMethodsUsing is cmp)
Tim Petersea5962f2007-03-12 18:07:52 +00001222
Georg Brandl15c5ce92007-03-07 09:09:40 +00001223 # "it can be set to None to disable the sort."
1224 #
1225 # XXX How is this different from reassigning cmp? Are the tests returned
1226 # in a random order or something? This behaviour should die
1227 def test_sortTestMethodsUsing__None(self):
1228 class Foo(unittest.TestCase):
1229 def test_1(self): pass
1230 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001231
Georg Brandl15c5ce92007-03-07 09:09:40 +00001232 loader = unittest.TestLoader()
1233 loader.sortTestMethodsUsing = None
Tim Petersea5962f2007-03-12 18:07:52 +00001234
Georg Brandl15c5ce92007-03-07 09:09:40 +00001235 test_names = ['test_2', 'test_1']
1236 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
Tim Petersea5962f2007-03-12 18:07:52 +00001237
Georg Brandl15c5ce92007-03-07 09:09:40 +00001238 ################################################################
1239 ### /Tests for TestLoader.sortTestMethodsUsing
Tim Petersea5962f2007-03-12 18:07:52 +00001240
Georg Brandl15c5ce92007-03-07 09:09:40 +00001241 ### Tests for TestLoader.suiteClass
1242 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001243
Georg Brandl15c5ce92007-03-07 09:09:40 +00001244 # "Callable object that constructs a test suite from a list of tests."
1245 def test_suiteClass__loadTestsFromTestCase(self):
1246 class Foo(unittest.TestCase):
1247 def test_1(self): pass
1248 def test_2(self): pass
1249 def foo_bar(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001250
Georg Brandl15c5ce92007-03-07 09:09:40 +00001251 tests = [Foo('test_1'), Foo('test_2')]
1252
1253 loader = unittest.TestLoader()
Benjamin Peterson692428e2009-03-23 21:50:21 +00001254 loader.classSuiteClass = MyClassSuite
Georg Brandl15c5ce92007-03-07 09:09:40 +00001255 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001256
Georg Brandl15c5ce92007-03-07 09:09:40 +00001257 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001258 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001259 def test_suiteClass__loadTestsFromModule(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001260 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001261 class Foo(unittest.TestCase):
1262 def test_1(self): pass
1263 def test_2(self): pass
1264 def foo_bar(self): pass
1265 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001266
Benjamin Peterson692428e2009-03-23 21:50:21 +00001267 tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)]
Georg Brandl15c5ce92007-03-07 09:09:40 +00001268
1269 loader = unittest.TestLoader()
1270 loader.suiteClass = list
1271 self.assertEqual(loader.loadTestsFromModule(m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001272
Georg Brandl15c5ce92007-03-07 09:09:40 +00001273 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001274 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001275 def test_suiteClass__loadTestsFromName(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001276 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001277 class Foo(unittest.TestCase):
1278 def test_1(self): pass
1279 def test_2(self): pass
1280 def foo_bar(self): pass
1281 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001282
Georg Brandl15c5ce92007-03-07 09:09:40 +00001283 tests = [Foo('test_1'), Foo('test_2')]
1284
1285 loader = unittest.TestLoader()
Benjamin Peterson692428e2009-03-23 21:50:21 +00001286 loader.classSuiteClass = MyClassSuite
Georg Brandl15c5ce92007-03-07 09:09:40 +00001287 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001288
Georg Brandl15c5ce92007-03-07 09:09:40 +00001289 # It is implicit in the documentation for TestLoader.suiteClass that
Tim Petersea5962f2007-03-12 18:07:52 +00001290 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
Georg Brandl15c5ce92007-03-07 09:09:40 +00001291 def test_suiteClass__loadTestsFromNames(self):
Christian Heimesc756d002007-11-27 21:34:01 +00001292 m = types.ModuleType('m')
Georg Brandl15c5ce92007-03-07 09:09:40 +00001293 class Foo(unittest.TestCase):
1294 def test_1(self): pass
1295 def test_2(self): pass
1296 def foo_bar(self): pass
1297 m.Foo = Foo
Tim Petersea5962f2007-03-12 18:07:52 +00001298
Benjamin Peterson692428e2009-03-23 21:50:21 +00001299 tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)]
Georg Brandl15c5ce92007-03-07 09:09:40 +00001300
1301 loader = unittest.TestLoader()
1302 loader.suiteClass = list
1303 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
Tim Petersea5962f2007-03-12 18:07:52 +00001304
Georg Brandl15c5ce92007-03-07 09:09:40 +00001305 # "The default value is the TestSuite class"
1306 def test_suiteClass__default_value(self):
1307 loader = unittest.TestLoader()
1308 self.failUnless(loader.suiteClass is unittest.TestSuite)
Tim Petersea5962f2007-03-12 18:07:52 +00001309
Georg Brandl15c5ce92007-03-07 09:09:40 +00001310 ################################################################
1311 ### /Tests for TestLoader.suiteClass
1312
1313### Support code for Test_TestSuite
1314################################################################
1315
1316class Foo(unittest.TestCase):
1317 def test_1(self): pass
1318 def test_2(self): pass
1319 def test_3(self): pass
1320 def runTest(self): pass
1321
1322def _mk_TestSuite(*names):
1323 return unittest.TestSuite(Foo(n) for n in names)
Tim Petersea5962f2007-03-12 18:07:52 +00001324
Georg Brandl15c5ce92007-03-07 09:09:40 +00001325################################################################
1326### /Support code for Test_TestSuite
1327
1328class Test_TestSuite(TestCase, TestEquality):
1329
1330 ### Set up attributes needed by inherited tests
1331 ################################################################
1332
1333 # Used by TestEquality.test_eq
1334 eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
1335 ,(unittest.TestSuite(), unittest.TestSuite([]))
1336 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001337
1338 # Used by TestEquality.test_ne
Georg Brandl15c5ce92007-03-07 09:09:40 +00001339 ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
1340 ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
1341 ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
1342 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001343
Georg Brandl15c5ce92007-03-07 09:09:40 +00001344 ################################################################
1345 ### /Set up attributes needed by inherited tests
1346
1347 ### Tests for TestSuite.__init__
1348 ################################################################
1349
1350 # "class TestSuite([tests])"
1351 #
1352 # The tests iterable should be optional
1353 def test_init__tests_optional(self):
1354 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001355
Georg Brandl15c5ce92007-03-07 09:09:40 +00001356 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001357
Georg Brandl15c5ce92007-03-07 09:09:40 +00001358 # "class TestSuite([tests])"
1359 # ...
1360 # "If tests is given, it must be an iterable of individual test cases
1361 # or other test suites that will be used to build the suite initially"
1362 #
1363 # TestSuite should deal with empty tests iterables by allowing the
Tim Petersea5962f2007-03-12 18:07:52 +00001364 # creation of an empty suite
Georg Brandl15c5ce92007-03-07 09:09:40 +00001365 def test_init__empty_tests(self):
1366 suite = unittest.TestSuite([])
Tim Petersea5962f2007-03-12 18:07:52 +00001367
Georg Brandl15c5ce92007-03-07 09:09:40 +00001368 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001369
Georg Brandl15c5ce92007-03-07 09:09:40 +00001370 # "class TestSuite([tests])"
1371 # ...
1372 # "If tests is given, it must be an iterable of individual test cases
1373 # or other test suites that will be used to build the suite initially"
1374 #
Tim Petersea5962f2007-03-12 18:07:52 +00001375 # TestSuite should allow any iterable to provide tests
Georg Brandl15c5ce92007-03-07 09:09:40 +00001376 def test_init__tests_from_any_iterable(self):
1377 def tests():
1378 yield unittest.FunctionTestCase(lambda: None)
1379 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001380
Georg Brandl15c5ce92007-03-07 09:09:40 +00001381 suite_1 = unittest.TestSuite(tests())
1382 self.assertEqual(suite_1.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001383
Georg Brandl15c5ce92007-03-07 09:09:40 +00001384 suite_2 = unittest.TestSuite(suite_1)
1385 self.assertEqual(suite_2.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001386
Georg Brandl15c5ce92007-03-07 09:09:40 +00001387 suite_3 = unittest.TestSuite(set(suite_1))
1388 self.assertEqual(suite_3.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001389
Georg Brandl15c5ce92007-03-07 09:09:40 +00001390 # "class TestSuite([tests])"
1391 # ...
1392 # "If tests is given, it must be an iterable of individual test cases
1393 # or other test suites that will be used to build the suite initially"
1394 #
1395 # Does TestSuite() also allow other TestSuite() instances to be present
1396 # in the tests iterable?
1397 def test_init__TestSuite_instances_in_tests(self):
1398 def tests():
1399 ftc = unittest.FunctionTestCase(lambda: None)
1400 yield unittest.TestSuite([ftc])
1401 yield unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001402
Georg Brandl15c5ce92007-03-07 09:09:40 +00001403 suite = unittest.TestSuite(tests())
1404 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001405
Georg Brandl15c5ce92007-03-07 09:09:40 +00001406 ################################################################
1407 ### /Tests for TestSuite.__init__
1408
1409 # Container types should support the iter protocol
1410 def test_iter(self):
1411 test1 = unittest.FunctionTestCase(lambda: None)
1412 test2 = unittest.FunctionTestCase(lambda: None)
1413 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001414
Georg Brandl15c5ce92007-03-07 09:09:40 +00001415 self.assertEqual(list(suite), [test1, test2])
Tim Petersea5962f2007-03-12 18:07:52 +00001416
Georg Brandl15c5ce92007-03-07 09:09:40 +00001417 # "Return the number of tests represented by the this test object.
1418 # ...this method is also implemented by the TestSuite class, which can
1419 # return larger [greater than 1] values"
1420 #
Tim Petersea5962f2007-03-12 18:07:52 +00001421 # Presumably an empty TestSuite returns 0?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001422 def test_countTestCases_zero_simple(self):
1423 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001424
Georg Brandl15c5ce92007-03-07 09:09:40 +00001425 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001426
Georg Brandl15c5ce92007-03-07 09:09:40 +00001427 # "Return the number of tests represented by the this test object.
1428 # ...this method is also implemented by the TestSuite class, which can
1429 # return larger [greater than 1] values"
1430 #
1431 # Presumably an empty TestSuite (even if it contains other empty
1432 # TestSuite instances) returns 0?
1433 def test_countTestCases_zero_nested(self):
1434 class Test1(unittest.TestCase):
1435 def test(self):
1436 pass
1437
1438 suite = unittest.TestSuite([unittest.TestSuite()])
Tim Petersea5962f2007-03-12 18:07:52 +00001439
Georg Brandl15c5ce92007-03-07 09:09:40 +00001440 self.assertEqual(suite.countTestCases(), 0)
Tim Petersea5962f2007-03-12 18:07:52 +00001441
Georg Brandl15c5ce92007-03-07 09:09:40 +00001442 # "Return the number of tests represented by the this test object.
1443 # ...this method is also implemented by the TestSuite class, which can
1444 # return larger [greater than 1] values"
1445 def test_countTestCases_simple(self):
1446 test1 = unittest.FunctionTestCase(lambda: None)
1447 test2 = unittest.FunctionTestCase(lambda: None)
1448 suite = unittest.TestSuite((test1, test2))
Tim Petersea5962f2007-03-12 18:07:52 +00001449
Georg Brandl15c5ce92007-03-07 09:09:40 +00001450 self.assertEqual(suite.countTestCases(), 2)
Tim Petersea5962f2007-03-12 18:07:52 +00001451
Georg Brandl15c5ce92007-03-07 09:09:40 +00001452 # "Return the number of tests represented by the this test object.
1453 # ...this method is also implemented by the TestSuite class, which can
1454 # return larger [greater than 1] values"
1455 #
1456 # Make sure this holds for nested TestSuite instances, too
1457 def test_countTestCases_nested(self):
1458 class Test1(unittest.TestCase):
1459 def test1(self): pass
1460 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001461
Georg Brandl15c5ce92007-03-07 09:09:40 +00001462 test2 = unittest.FunctionTestCase(lambda: None)
1463 test3 = unittest.FunctionTestCase(lambda: None)
1464 child = unittest.TestSuite((Test1('test2'), test2))
1465 parent = unittest.TestSuite((test3, child, Test1('test1')))
Tim Petersea5962f2007-03-12 18:07:52 +00001466
Georg Brandl15c5ce92007-03-07 09:09:40 +00001467 self.assertEqual(parent.countTestCases(), 4)
Tim Petersea5962f2007-03-12 18:07:52 +00001468
Georg Brandl15c5ce92007-03-07 09:09:40 +00001469 # "Run the tests associated with this suite, collecting the result into
1470 # the test result object passed as result."
1471 #
1472 # And if there are no tests? What then?
1473 def test_run__empty_suite(self):
1474 events = []
1475 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001476
Georg Brandl15c5ce92007-03-07 09:09:40 +00001477 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001478
Georg Brandl15c5ce92007-03-07 09:09:40 +00001479 suite.run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001480
Georg Brandl15c5ce92007-03-07 09:09:40 +00001481 self.assertEqual(events, [])
Tim Petersea5962f2007-03-12 18:07:52 +00001482
Georg Brandl15c5ce92007-03-07 09:09:40 +00001483 # "Note that unlike TestCase.run(), TestSuite.run() requires the
1484 # "result object to be passed in."
1485 def test_run__requires_result(self):
1486 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001487
Georg Brandl15c5ce92007-03-07 09:09:40 +00001488 try:
1489 suite.run()
1490 except TypeError:
1491 pass
1492 else:
1493 self.fail("Failed to raise TypeError")
Tim Petersea5962f2007-03-12 18:07:52 +00001494
Georg Brandl15c5ce92007-03-07 09:09:40 +00001495 # "Run the tests associated with this suite, collecting the result into
Tim Petersea5962f2007-03-12 18:07:52 +00001496 # the test result object passed as result."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001497 def test_run(self):
1498 events = []
1499 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001500
Georg Brandl15c5ce92007-03-07 09:09:40 +00001501 class LoggingCase(unittest.TestCase):
1502 def run(self, result):
1503 events.append('run %s' % self._testMethodName)
Tim Petersea5962f2007-03-12 18:07:52 +00001504
Georg Brandl15c5ce92007-03-07 09:09:40 +00001505 def test1(self): pass
1506 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001507
1508 tests = [LoggingCase('test1'), LoggingCase('test2')]
1509
Georg Brandl15c5ce92007-03-07 09:09:40 +00001510 unittest.TestSuite(tests).run(result)
Tim Petersea5962f2007-03-12 18:07:52 +00001511
Georg Brandl15c5ce92007-03-07 09:09:40 +00001512 self.assertEqual(events, ['run test1', 'run test2'])
Tim Petersea5962f2007-03-12 18:07:52 +00001513
1514 # "Add a TestCase ... to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001515 def test_addTest__TestCase(self):
1516 class Foo(unittest.TestCase):
1517 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001518
Georg Brandl15c5ce92007-03-07 09:09:40 +00001519 test = Foo('test')
1520 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001521
Georg Brandl15c5ce92007-03-07 09:09:40 +00001522 suite.addTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001523
Georg Brandl15c5ce92007-03-07 09:09:40 +00001524 self.assertEqual(suite.countTestCases(), 1)
1525 self.assertEqual(list(suite), [test])
Tim Petersea5962f2007-03-12 18:07:52 +00001526
1527 # "Add a ... TestSuite to the suite"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001528 def test_addTest__TestSuite(self):
1529 class Foo(unittest.TestCase):
1530 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001531
Georg Brandl15c5ce92007-03-07 09:09:40 +00001532 suite_2 = unittest.TestSuite([Foo('test')])
Tim Petersea5962f2007-03-12 18:07:52 +00001533
Georg Brandl15c5ce92007-03-07 09:09:40 +00001534 suite = unittest.TestSuite()
1535 suite.addTest(suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001536
Georg Brandl15c5ce92007-03-07 09:09:40 +00001537 self.assertEqual(suite.countTestCases(), 1)
1538 self.assertEqual(list(suite), [suite_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001539
Georg Brandl15c5ce92007-03-07 09:09:40 +00001540 # "Add all the tests from an iterable of TestCase and TestSuite
1541 # instances to this test suite."
Tim Petersea5962f2007-03-12 18:07:52 +00001542 #
Georg Brandl15c5ce92007-03-07 09:09:40 +00001543 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001544 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001545 def test_addTests(self):
1546 class Foo(unittest.TestCase):
1547 def test_1(self): pass
1548 def test_2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001549
Georg Brandl15c5ce92007-03-07 09:09:40 +00001550 test_1 = Foo('test_1')
1551 test_2 = Foo('test_2')
1552 inner_suite = unittest.TestSuite([test_2])
Tim Petersea5962f2007-03-12 18:07:52 +00001553
Georg Brandl15c5ce92007-03-07 09:09:40 +00001554 def gen():
1555 yield test_1
1556 yield test_2
1557 yield inner_suite
Tim Petersea5962f2007-03-12 18:07:52 +00001558
Georg Brandl15c5ce92007-03-07 09:09:40 +00001559 suite_1 = unittest.TestSuite()
1560 suite_1.addTests(gen())
Tim Petersea5962f2007-03-12 18:07:52 +00001561
Georg Brandl15c5ce92007-03-07 09:09:40 +00001562 self.assertEqual(list(suite_1), list(gen()))
Tim Petersea5962f2007-03-12 18:07:52 +00001563
Georg Brandl15c5ce92007-03-07 09:09:40 +00001564 # "This is equivalent to iterating over tests, calling addTest() for
Tim Petersea5962f2007-03-12 18:07:52 +00001565 # each element"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001566 suite_2 = unittest.TestSuite()
1567 for t in gen():
1568 suite_2.addTest(t)
Tim Petersea5962f2007-03-12 18:07:52 +00001569
Georg Brandl15c5ce92007-03-07 09:09:40 +00001570 self.assertEqual(suite_1, suite_2)
Tim Petersea5962f2007-03-12 18:07:52 +00001571
Georg Brandl15c5ce92007-03-07 09:09:40 +00001572 # "Add all the tests from an iterable of TestCase and TestSuite
1573 # instances to this test suite."
1574 #
Tim Petersea5962f2007-03-12 18:07:52 +00001575 # What happens if it doesn't get an iterable?
Georg Brandl15c5ce92007-03-07 09:09:40 +00001576 def test_addTest__noniterable(self):
1577 suite = unittest.TestSuite()
Tim Petersea5962f2007-03-12 18:07:52 +00001578
Georg Brandl15c5ce92007-03-07 09:09:40 +00001579 try:
1580 suite.addTests(5)
1581 except TypeError:
1582 pass
1583 else:
1584 self.fail("Failed to raise TypeError")
Georg Brandld9e50262007-03-07 11:54:49 +00001585
1586 def test_addTest__noncallable(self):
1587 suite = unittest.TestSuite()
1588 self.assertRaises(TypeError, suite.addTest, 5)
1589
1590 def test_addTest__casesuiteclass(self):
1591 suite = unittest.TestSuite()
1592 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
1593 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
1594
1595 def test_addTests__string(self):
1596 suite = unittest.TestSuite()
1597 self.assertRaises(TypeError, suite.addTests, "foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001598
1599
Georg Brandl15c5ce92007-03-07 09:09:40 +00001600class Test_FunctionTestCase(TestCase):
Tim Petersea5962f2007-03-12 18:07:52 +00001601
Georg Brandl15c5ce92007-03-07 09:09:40 +00001602 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00001603 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00001604 def test_countTestCases(self):
1605 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001606
Georg Brandl15c5ce92007-03-07 09:09:40 +00001607 self.assertEqual(test.countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00001608
Georg Brandl15c5ce92007-03-07 09:09:40 +00001609 # "When a setUp() method is defined, the test runner will run that method
1610 # prior to each test. Likewise, if a tearDown() method is defined, the
1611 # test runner will invoke that method after each test. In the example,
1612 # setUp() was used to create a fresh sequence for each test."
1613 #
1614 # Make sure the proper call order is maintained, even if setUp() raises
1615 # an exception.
1616 def test_run_call_order__error_in_setUp(self):
1617 events = []
1618 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001619
Georg Brandl15c5ce92007-03-07 09:09:40 +00001620 def setUp():
1621 events.append('setUp')
1622 raise RuntimeError('raised by setUp')
1623
1624 def test():
1625 events.append('test')
1626
1627 def tearDown():
1628 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001629
1630 expected = ['startTest', 'setUp', 'addError', 'stopTest']
Georg Brandl15c5ce92007-03-07 09:09:40 +00001631 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1632 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001633
Georg Brandl15c5ce92007-03-07 09:09:40 +00001634 # "When a setUp() method is defined, the test runner will run that method
1635 # prior to each test. Likewise, if a tearDown() method is defined, the
1636 # test runner will invoke that method after each test. In the example,
1637 # setUp() was used to create a fresh sequence for each test."
1638 #
1639 # Make sure the proper call order is maintained, even if the test raises
1640 # an error (as opposed to a failure).
1641 def test_run_call_order__error_in_test(self):
1642 events = []
1643 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001644
Georg Brandl15c5ce92007-03-07 09:09:40 +00001645 def setUp():
1646 events.append('setUp')
1647
1648 def test():
1649 events.append('test')
1650 raise RuntimeError('raised by test')
1651
1652 def tearDown():
1653 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001654
Georg Brandl15c5ce92007-03-07 09:09:40 +00001655 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
1656 'stopTest']
1657 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1658 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001659
Georg Brandl15c5ce92007-03-07 09:09:40 +00001660 # "When a setUp() method is defined, the test runner will run that method
1661 # prior to each test. Likewise, if a tearDown() method is defined, the
1662 # test runner will invoke that method after each test. In the example,
1663 # setUp() was used to create a fresh sequence for each test."
1664 #
1665 # Make sure the proper call order is maintained, even if the test signals
1666 # a failure (as opposed to an error).
1667 def test_run_call_order__failure_in_test(self):
1668 events = []
1669 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001670
Georg Brandl15c5ce92007-03-07 09:09:40 +00001671 def setUp():
1672 events.append('setUp')
1673
1674 def test():
1675 events.append('test')
1676 self.fail('raised by test')
1677
1678 def tearDown():
1679 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001680
Georg Brandl15c5ce92007-03-07 09:09:40 +00001681 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
1682 'stopTest']
1683 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1684 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001685
Georg Brandl15c5ce92007-03-07 09:09:40 +00001686 # "When a setUp() method is defined, the test runner will run that method
1687 # prior to each test. Likewise, if a tearDown() method is defined, the
1688 # test runner will invoke that method after each test. In the example,
1689 # setUp() was used to create a fresh sequence for each test."
1690 #
1691 # Make sure the proper call order is maintained, even if tearDown() raises
1692 # an exception.
1693 def test_run_call_order__error_in_tearDown(self):
1694 events = []
1695 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00001696
Georg Brandl15c5ce92007-03-07 09:09:40 +00001697 def setUp():
1698 events.append('setUp')
1699
1700 def test():
1701 events.append('test')
1702
1703 def tearDown():
1704 events.append('tearDown')
1705 raise RuntimeError('raised by tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00001706
Georg Brandl15c5ce92007-03-07 09:09:40 +00001707 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
1708 'stopTest']
1709 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
1710 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00001711
Georg Brandl15c5ce92007-03-07 09:09:40 +00001712 # "Return a string identifying the specific test case."
1713 #
1714 # Because of the vague nature of the docs, I'm not going to lock this
1715 # test down too much. Really all that can be asserted is that the id()
1716 # will be a string (either 8-byte or unicode -- again, because the docs
1717 # just say "string")
1718 def test_id(self):
1719 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001720
Georg Brandl15c5ce92007-03-07 09:09:40 +00001721 self.failUnless(isinstance(test.id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00001722
Georg Brandl15c5ce92007-03-07 09:09:40 +00001723 # "Returns a one-line description of the test, or None if no description
1724 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001725 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001726 def test_shortDescription__no_docstring(self):
1727 test = unittest.FunctionTestCase(lambda: None)
Tim Petersea5962f2007-03-12 18:07:52 +00001728
Georg Brandl15c5ce92007-03-07 09:09:40 +00001729 self.assertEqual(test.shortDescription(), None)
Tim Petersea5962f2007-03-12 18:07:52 +00001730
Georg Brandl15c5ce92007-03-07 09:09:40 +00001731 # "Returns a one-line description of the test, or None if no description
1732 # has been provided. The default implementation of this method returns
Tim Petersea5962f2007-03-12 18:07:52 +00001733 # the first line of the test method's docstring, if available, or None."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001734 def test_shortDescription__singleline_docstring(self):
1735 desc = "this tests foo"
1736 test = unittest.FunctionTestCase(lambda: None, description=desc)
Tim Petersea5962f2007-03-12 18:07:52 +00001737
Georg Brandl15c5ce92007-03-07 09:09:40 +00001738 self.assertEqual(test.shortDescription(), "this tests foo")
Tim Petersea5962f2007-03-12 18:07:52 +00001739
Georg Brandl15c5ce92007-03-07 09:09:40 +00001740class Test_TestResult(TestCase):
1741 # Note: there are not separate tests for TestResult.wasSuccessful(),
1742 # TestResult.errors, TestResult.failures, TestResult.testsRun or
1743 # TestResult.shouldStop because these only have meaning in terms of
1744 # other TestResult methods.
1745 #
1746 # Accordingly, tests for the aforenamed attributes are incorporated
1747 # in with the tests for the defining methods.
1748 ################################################################
Tim Petersea5962f2007-03-12 18:07:52 +00001749
Georg Brandl15c5ce92007-03-07 09:09:40 +00001750 def test_init(self):
1751 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001752
Georg Brandl15c5ce92007-03-07 09:09:40 +00001753 self.failUnless(result.wasSuccessful())
1754 self.assertEqual(len(result.errors), 0)
1755 self.assertEqual(len(result.failures), 0)
1756 self.assertEqual(result.testsRun, 0)
1757 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001758
Georg Brandl15c5ce92007-03-07 09:09:40 +00001759 # "This method can be called to signal that the set of tests being
1760 # run should be aborted by setting the TestResult's shouldStop
Tim Petersea5962f2007-03-12 18:07:52 +00001761 # attribute to True."
Georg Brandl15c5ce92007-03-07 09:09:40 +00001762 def test_stop(self):
1763 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001764
Georg Brandl15c5ce92007-03-07 09:09:40 +00001765 result.stop()
Tim Petersea5962f2007-03-12 18:07:52 +00001766
Georg Brandl15c5ce92007-03-07 09:09:40 +00001767 self.assertEqual(result.shouldStop, True)
Tim Petersea5962f2007-03-12 18:07:52 +00001768
Georg Brandl15c5ce92007-03-07 09:09:40 +00001769 # "Called when the test case test is about to be run. The default
1770 # implementation simply increments the instance's testsRun counter."
1771 def test_startTest(self):
1772 class Foo(unittest.TestCase):
1773 def test_1(self):
1774 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001775
Georg Brandl15c5ce92007-03-07 09:09:40 +00001776 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001777
Georg Brandl15c5ce92007-03-07 09:09:40 +00001778 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001779
Georg Brandl15c5ce92007-03-07 09:09:40 +00001780 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001781
Georg Brandl15c5ce92007-03-07 09:09:40 +00001782 self.failUnless(result.wasSuccessful())
1783 self.assertEqual(len(result.errors), 0)
1784 self.assertEqual(len(result.failures), 0)
1785 self.assertEqual(result.testsRun, 1)
1786 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001787
Georg Brandl15c5ce92007-03-07 09:09:40 +00001788 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001789
Georg Brandl15c5ce92007-03-07 09:09:40 +00001790 # "Called after the test case test has been executed, regardless of
1791 # the outcome. The default implementation does nothing."
1792 def test_stopTest(self):
1793 class Foo(unittest.TestCase):
1794 def test_1(self):
1795 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001796
Georg Brandl15c5ce92007-03-07 09:09:40 +00001797 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001798
Georg Brandl15c5ce92007-03-07 09:09:40 +00001799 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001800
Georg Brandl15c5ce92007-03-07 09:09:40 +00001801 result.startTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001802
Georg Brandl15c5ce92007-03-07 09:09:40 +00001803 self.failUnless(result.wasSuccessful())
1804 self.assertEqual(len(result.errors), 0)
1805 self.assertEqual(len(result.failures), 0)
1806 self.assertEqual(result.testsRun, 1)
1807 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001808
Georg Brandl15c5ce92007-03-07 09:09:40 +00001809 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001810
Georg Brandl15c5ce92007-03-07 09:09:40 +00001811 # Same tests as above; make sure nothing has changed
1812 self.failUnless(result.wasSuccessful())
1813 self.assertEqual(len(result.errors), 0)
1814 self.assertEqual(len(result.failures), 0)
1815 self.assertEqual(result.testsRun, 1)
1816 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001817
Georg Brandl15c5ce92007-03-07 09:09:40 +00001818 # "addSuccess(test)"
1819 # ...
1820 # "Called when the test case test succeeds"
1821 # ...
1822 # "wasSuccessful() - Returns True if all tests run so far have passed,
1823 # otherwise returns False"
1824 # ...
1825 # "testsRun - The total number of tests run so far."
1826 # ...
1827 # "errors - A list containing 2-tuples of TestCase instances and
1828 # formatted tracebacks. Each tuple represents a test which raised an
1829 # unexpected exception. Contains formatted
1830 # tracebacks instead of sys.exc_info() results."
1831 # ...
1832 # "failures - A list containing 2-tuples of TestCase instances and
1833 # formatted tracebacks. Each tuple represents a test where a failure was
1834 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1835 # methods. Contains formatted tracebacks instead
1836 # of sys.exc_info() results."
1837 def test_addSuccess(self):
1838 class Foo(unittest.TestCase):
1839 def test_1(self):
1840 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001841
Georg Brandl15c5ce92007-03-07 09:09:40 +00001842 test = Foo('test_1')
Tim Petersea5962f2007-03-12 18:07:52 +00001843
Georg Brandl15c5ce92007-03-07 09:09:40 +00001844 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001845
Georg Brandl15c5ce92007-03-07 09:09:40 +00001846 result.startTest(test)
1847 result.addSuccess(test)
1848 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001849
Georg Brandl15c5ce92007-03-07 09:09:40 +00001850 self.failUnless(result.wasSuccessful())
1851 self.assertEqual(len(result.errors), 0)
1852 self.assertEqual(len(result.failures), 0)
1853 self.assertEqual(result.testsRun, 1)
1854 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001855
Georg Brandl15c5ce92007-03-07 09:09:40 +00001856 # "addFailure(test, err)"
1857 # ...
1858 # "Called when the test case test signals a failure. err is a tuple of
1859 # the form returned by sys.exc_info(): (type, value, traceback)"
1860 # ...
1861 # "wasSuccessful() - Returns True if all tests run so far have passed,
1862 # otherwise returns False"
1863 # ...
1864 # "testsRun - The total number of tests run so far."
1865 # ...
1866 # "errors - A list containing 2-tuples of TestCase instances and
1867 # formatted tracebacks. Each tuple represents a test which raised an
1868 # unexpected exception. Contains formatted
1869 # tracebacks instead of sys.exc_info() results."
1870 # ...
1871 # "failures - A list containing 2-tuples of TestCase instances and
1872 # formatted tracebacks. Each tuple represents a test where a failure was
1873 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1874 # methods. Contains formatted tracebacks instead
1875 # of sys.exc_info() results."
1876 def test_addFailure(self):
1877 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001878
Georg Brandl15c5ce92007-03-07 09:09:40 +00001879 class Foo(unittest.TestCase):
1880 def test_1(self):
1881 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001882
Georg Brandl15c5ce92007-03-07 09:09:40 +00001883 test = Foo('test_1')
1884 try:
1885 test.fail("foo")
1886 except:
1887 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001888
Georg Brandl15c5ce92007-03-07 09:09:40 +00001889 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001890
Georg Brandl15c5ce92007-03-07 09:09:40 +00001891 result.startTest(test)
1892 result.addFailure(test, exc_info_tuple)
1893 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001894
Georg Brandl15c5ce92007-03-07 09:09:40 +00001895 self.failIf(result.wasSuccessful())
1896 self.assertEqual(len(result.errors), 0)
1897 self.assertEqual(len(result.failures), 1)
1898 self.assertEqual(result.testsRun, 1)
1899 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001900
Georg Brandl15c5ce92007-03-07 09:09:40 +00001901 test_case, formatted_exc = result.failures[0]
1902 self.failUnless(test_case is test)
1903 self.failUnless(isinstance(formatted_exc, str))
Tim Petersea5962f2007-03-12 18:07:52 +00001904
Georg Brandl15c5ce92007-03-07 09:09:40 +00001905 # "addError(test, err)"
1906 # ...
1907 # "Called when the test case test raises an unexpected exception err
1908 # is a tuple of the form returned by sys.exc_info():
1909 # (type, value, traceback)"
1910 # ...
1911 # "wasSuccessful() - Returns True if all tests run so far have passed,
1912 # otherwise returns False"
1913 # ...
1914 # "testsRun - The total number of tests run so far."
1915 # ...
1916 # "errors - A list containing 2-tuples of TestCase instances and
1917 # formatted tracebacks. Each tuple represents a test which raised an
1918 # unexpected exception. Contains formatted
1919 # tracebacks instead of sys.exc_info() results."
1920 # ...
1921 # "failures - A list containing 2-tuples of TestCase instances and
1922 # formatted tracebacks. Each tuple represents a test where a failure was
1923 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
1924 # methods. Contains formatted tracebacks instead
1925 # of sys.exc_info() results."
1926 def test_addError(self):
1927 import sys
Tim Petersea5962f2007-03-12 18:07:52 +00001928
Georg Brandl15c5ce92007-03-07 09:09:40 +00001929 class Foo(unittest.TestCase):
1930 def test_1(self):
1931 pass
Tim Petersea5962f2007-03-12 18:07:52 +00001932
Georg Brandl15c5ce92007-03-07 09:09:40 +00001933 test = Foo('test_1')
1934 try:
1935 raise TypeError()
1936 except:
1937 exc_info_tuple = sys.exc_info()
Tim Petersea5962f2007-03-12 18:07:52 +00001938
Georg Brandl15c5ce92007-03-07 09:09:40 +00001939 result = unittest.TestResult()
Tim Petersea5962f2007-03-12 18:07:52 +00001940
Georg Brandl15c5ce92007-03-07 09:09:40 +00001941 result.startTest(test)
1942 result.addError(test, exc_info_tuple)
1943 result.stopTest(test)
Tim Petersea5962f2007-03-12 18:07:52 +00001944
Georg Brandl15c5ce92007-03-07 09:09:40 +00001945 self.failIf(result.wasSuccessful())
1946 self.assertEqual(len(result.errors), 1)
1947 self.assertEqual(len(result.failures), 0)
1948 self.assertEqual(result.testsRun, 1)
1949 self.assertEqual(result.shouldStop, False)
Tim Petersea5962f2007-03-12 18:07:52 +00001950
Georg Brandl15c5ce92007-03-07 09:09:40 +00001951 test_case, formatted_exc = result.errors[0]
1952 self.failUnless(test_case is test)
1953 self.failUnless(isinstance(formatted_exc, str))
1954
1955### Support code for Test_TestCase
1956################################################################
1957
1958class Foo(unittest.TestCase):
1959 def runTest(self): pass
1960 def test1(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001961
Georg Brandl15c5ce92007-03-07 09:09:40 +00001962class Bar(Foo):
1963 def test2(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001964
Georg Brandl15c5ce92007-03-07 09:09:40 +00001965################################################################
1966### /Support code for Test_TestCase
1967
1968class Test_TestCase(TestCase, TestEquality, TestHashing):
1969
1970 ### Set up attributes used by inherited tests
1971 ################################################################
1972
1973 # Used by TestHashing.test_hash and TestEquality.test_eq
1974 eq_pairs = [(Foo('test1'), Foo('test1'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001975
Georg Brandl15c5ce92007-03-07 09:09:40 +00001976 # Used by TestEquality.test_ne
1977 ne_pairs = [(Foo('test1'), Foo('runTest'))
1978 ,(Foo('test1'), Bar('test1'))
1979 ,(Foo('test1'), Bar('test2'))]
Tim Petersea5962f2007-03-12 18:07:52 +00001980
Georg Brandl15c5ce92007-03-07 09:09:40 +00001981 ################################################################
1982 ### /Set up attributes used by inherited tests
Tim Petersea5962f2007-03-12 18:07:52 +00001983
Georg Brandl15c5ce92007-03-07 09:09:40 +00001984
1985 # "class TestCase([methodName])"
1986 # ...
1987 # "Each instance of TestCase will run a single test method: the
1988 # method named methodName."
1989 # ...
1990 # "methodName defaults to "runTest"."
1991 #
1992 # Make sure it really is optional, and that it defaults to the proper
1993 # thing.
1994 def test_init__no_test_name(self):
1995 class Test(unittest.TestCase):
1996 def runTest(self): raise MyException()
1997 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00001998
Georg Brandl15c5ce92007-03-07 09:09:40 +00001999 self.assertEqual(Test().id()[-13:], '.Test.runTest')
Tim Petersea5962f2007-03-12 18:07:52 +00002000
Georg Brandl15c5ce92007-03-07 09:09:40 +00002001 # "class TestCase([methodName])"
2002 # ...
2003 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00002004 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002005 def test_init__test_name__valid(self):
2006 class Test(unittest.TestCase):
2007 def runTest(self): raise MyException()
2008 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002009
Georg Brandl15c5ce92007-03-07 09:09:40 +00002010 self.assertEqual(Test('test').id()[-10:], '.Test.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002011
Georg Brandl15c5ce92007-03-07 09:09:40 +00002012 # "class TestCase([methodName])"
2013 # ...
2014 # "Each instance of TestCase will run a single test method: the
Tim Petersea5962f2007-03-12 18:07:52 +00002015 # method named methodName."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002016 def test_init__test_name__invalid(self):
2017 class Test(unittest.TestCase):
2018 def runTest(self): raise MyException()
2019 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002020
Georg Brandl15c5ce92007-03-07 09:09:40 +00002021 try:
2022 Test('testfoo')
2023 except ValueError:
2024 pass
2025 else:
2026 self.fail("Failed to raise ValueError")
Tim Petersea5962f2007-03-12 18:07:52 +00002027
Georg Brandl15c5ce92007-03-07 09:09:40 +00002028 # "Return the number of tests represented by the this test object. For
Tim Petersea5962f2007-03-12 18:07:52 +00002029 # TestCase instances, this will always be 1"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002030 def test_countTestCases(self):
2031 class Foo(unittest.TestCase):
2032 def test(self): pass
Tim Petersea5962f2007-03-12 18:07:52 +00002033
Georg Brandl15c5ce92007-03-07 09:09:40 +00002034 self.assertEqual(Foo('test').countTestCases(), 1)
Tim Petersea5962f2007-03-12 18:07:52 +00002035
Georg Brandl15c5ce92007-03-07 09:09:40 +00002036 # "Return the default type of test result object to be used to run this
2037 # test. For TestCase instances, this will always be
2038 # unittest.TestResult; subclasses of TestCase should
2039 # override this as necessary."
2040 def test_defaultTestResult(self):
2041 class Foo(unittest.TestCase):
2042 def runTest(self):
2043 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002044
Georg Brandl15c5ce92007-03-07 09:09:40 +00002045 result = Foo().defaultTestResult()
2046 self.assertEqual(type(result), unittest.TestResult)
2047
2048 # "When a setUp() method is defined, the test runner will run that method
2049 # prior to each test. Likewise, if a tearDown() method is defined, the
2050 # test runner will invoke that method after each test. In the example,
2051 # setUp() was used to create a fresh sequence for each test."
2052 #
2053 # Make sure the proper call order is maintained, even if setUp() raises
2054 # an exception.
2055 def test_run_call_order__error_in_setUp(self):
2056 events = []
2057 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002058
Georg Brandl15c5ce92007-03-07 09:09:40 +00002059 class Foo(unittest.TestCase):
2060 def setUp(self):
2061 events.append('setUp')
2062 raise RuntimeError('raised by Foo.setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002063
Georg Brandl15c5ce92007-03-07 09:09:40 +00002064 def test(self):
2065 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002066
Georg Brandl15c5ce92007-03-07 09:09:40 +00002067 def tearDown(self):
2068 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002069
Georg Brandl15c5ce92007-03-07 09:09:40 +00002070 Foo('test').run(result)
2071 expected = ['startTest', 'setUp', 'addError', 'stopTest']
2072 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002073
Georg Brandl15c5ce92007-03-07 09:09:40 +00002074 # "When a setUp() method is defined, the test runner will run that method
2075 # prior to each test. Likewise, if a tearDown() method is defined, the
2076 # test runner will invoke that method after each test. In the example,
2077 # setUp() was used to create a fresh sequence for each test."
2078 #
2079 # Make sure the proper call order is maintained, even if the test raises
2080 # an error (as opposed to a failure).
2081 def test_run_call_order__error_in_test(self):
2082 events = []
2083 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002084
Georg Brandl15c5ce92007-03-07 09:09:40 +00002085 class Foo(unittest.TestCase):
2086 def setUp(self):
2087 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002088
Georg Brandl15c5ce92007-03-07 09:09:40 +00002089 def test(self):
2090 events.append('test')
2091 raise RuntimeError('raised by Foo.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002092
Georg Brandl15c5ce92007-03-07 09:09:40 +00002093 def tearDown(self):
2094 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002095
Georg Brandl15c5ce92007-03-07 09:09:40 +00002096 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
2097 'stopTest']
2098 Foo('test').run(result)
2099 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002100
Georg Brandl15c5ce92007-03-07 09:09:40 +00002101 # "When a setUp() method is defined, the test runner will run that method
2102 # prior to each test. Likewise, if a tearDown() method is defined, the
2103 # test runner will invoke that method after each test. In the example,
2104 # setUp() was used to create a fresh sequence for each test."
2105 #
2106 # Make sure the proper call order is maintained, even if the test signals
2107 # a failure (as opposed to an error).
2108 def test_run_call_order__failure_in_test(self):
2109 events = []
2110 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002111
Georg Brandl15c5ce92007-03-07 09:09:40 +00002112 class Foo(unittest.TestCase):
2113 def setUp(self):
2114 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002115
Georg Brandl15c5ce92007-03-07 09:09:40 +00002116 def test(self):
2117 events.append('test')
2118 self.fail('raised by Foo.test')
Tim Petersea5962f2007-03-12 18:07:52 +00002119
Georg Brandl15c5ce92007-03-07 09:09:40 +00002120 def tearDown(self):
2121 events.append('tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002122
Georg Brandl15c5ce92007-03-07 09:09:40 +00002123 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
2124 'stopTest']
2125 Foo('test').run(result)
2126 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002127
Georg Brandl15c5ce92007-03-07 09:09:40 +00002128 # "When a setUp() method is defined, the test runner will run that method
2129 # prior to each test. Likewise, if a tearDown() method is defined, the
2130 # test runner will invoke that method after each test. In the example,
2131 # setUp() was used to create a fresh sequence for each test."
2132 #
2133 # Make sure the proper call order is maintained, even if tearDown() raises
2134 # an exception.
2135 def test_run_call_order__error_in_tearDown(self):
2136 events = []
2137 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002138
Georg Brandl15c5ce92007-03-07 09:09:40 +00002139 class Foo(unittest.TestCase):
2140 def setUp(self):
2141 events.append('setUp')
Tim Petersea5962f2007-03-12 18:07:52 +00002142
Georg Brandl15c5ce92007-03-07 09:09:40 +00002143 def test(self):
2144 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002145
Georg Brandl15c5ce92007-03-07 09:09:40 +00002146 def tearDown(self):
2147 events.append('tearDown')
2148 raise RuntimeError('raised by Foo.tearDown')
Tim Petersea5962f2007-03-12 18:07:52 +00002149
Georg Brandl15c5ce92007-03-07 09:09:40 +00002150 Foo('test').run(result)
2151 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
2152 'stopTest']
2153 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002154
Georg Brandl15c5ce92007-03-07 09:09:40 +00002155 # "This class attribute gives the exception raised by the test() method.
2156 # If a test framework needs to use a specialized exception, possibly to
2157 # carry additional information, it must subclass this exception in
2158 # order to ``play fair'' with the framework. The initial value of this
2159 # attribute is AssertionError"
2160 def test_failureException__default(self):
2161 class Foo(unittest.TestCase):
2162 def test(self):
2163 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002164
Georg Brandl15c5ce92007-03-07 09:09:40 +00002165 self.failUnless(Foo('test').failureException is AssertionError)
Tim Petersea5962f2007-03-12 18:07:52 +00002166
Georg Brandl15c5ce92007-03-07 09:09:40 +00002167 # "This class attribute gives the exception raised by the test() method.
2168 # If a test framework needs to use a specialized exception, possibly to
2169 # carry additional information, it must subclass this exception in
2170 # order to ``play fair'' with the framework."
2171 #
2172 # Make sure TestCase.run() respects the designated failureException
2173 def test_failureException__subclassing__explicit_raise(self):
2174 events = []
2175 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002176
Georg Brandl15c5ce92007-03-07 09:09:40 +00002177 class Foo(unittest.TestCase):
2178 def test(self):
2179 raise RuntimeError()
Tim Petersea5962f2007-03-12 18:07:52 +00002180
Georg Brandl15c5ce92007-03-07 09:09:40 +00002181 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002182
Georg Brandl15c5ce92007-03-07 09:09:40 +00002183 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002184
2185
Georg Brandl15c5ce92007-03-07 09:09:40 +00002186 Foo('test').run(result)
2187 expected = ['startTest', 'addFailure', 'stopTest']
2188 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002189
Georg Brandl15c5ce92007-03-07 09:09:40 +00002190 # "This class attribute gives the exception raised by the test() method.
2191 # If a test framework needs to use a specialized exception, possibly to
2192 # carry additional information, it must subclass this exception in
2193 # order to ``play fair'' with the framework."
2194 #
2195 # Make sure TestCase.run() respects the designated failureException
2196 def test_failureException__subclassing__implicit_raise(self):
2197 events = []
2198 result = LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002199
Georg Brandl15c5ce92007-03-07 09:09:40 +00002200 class Foo(unittest.TestCase):
2201 def test(self):
2202 self.fail("foo")
Tim Petersea5962f2007-03-12 18:07:52 +00002203
Georg Brandl15c5ce92007-03-07 09:09:40 +00002204 failureException = RuntimeError
Tim Petersea5962f2007-03-12 18:07:52 +00002205
Georg Brandl15c5ce92007-03-07 09:09:40 +00002206 self.failUnless(Foo('test').failureException is RuntimeError)
Tim Petersea5962f2007-03-12 18:07:52 +00002207
2208
Georg Brandl15c5ce92007-03-07 09:09:40 +00002209 Foo('test').run(result)
2210 expected = ['startTest', 'addFailure', 'stopTest']
2211 self.assertEqual(events, expected)
Tim Petersea5962f2007-03-12 18:07:52 +00002212
2213 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002214 def test_setUp(self):
2215 class Foo(unittest.TestCase):
2216 def runTest(self):
2217 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002218
Georg Brandl15c5ce92007-03-07 09:09:40 +00002219 # ... and nothing should happen
2220 Foo().setUp()
Tim Petersea5962f2007-03-12 18:07:52 +00002221
2222 # "The default implementation does nothing."
Georg Brandl15c5ce92007-03-07 09:09:40 +00002223 def test_tearDown(self):
2224 class Foo(unittest.TestCase):
2225 def runTest(self):
2226 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002227
Georg Brandl15c5ce92007-03-07 09:09:40 +00002228 # ... and nothing should happen
2229 Foo().tearDown()
Tim Petersea5962f2007-03-12 18:07:52 +00002230
Georg Brandl15c5ce92007-03-07 09:09:40 +00002231 # "Return a string identifying the specific test case."
2232 #
2233 # Because of the vague nature of the docs, I'm not going to lock this
2234 # test down too much. Really all that can be asserted is that the id()
2235 # will be a string (either 8-byte or unicode -- again, because the docs
2236 # just say "string")
2237 def test_id(self):
2238 class Foo(unittest.TestCase):
2239 def runTest(self):
2240 pass
Tim Petersea5962f2007-03-12 18:07:52 +00002241
Georg Brandl15c5ce92007-03-07 09:09:40 +00002242 self.failUnless(isinstance(Foo().id(), basestring))
Tim Petersea5962f2007-03-12 18:07:52 +00002243
Georg Brandl15c5ce92007-03-07 09:09:40 +00002244 # "If result is omitted or None, a temporary result object is created
Tim Petersea5962f2007-03-12 18:07:52 +00002245 # and used, but is not made available to the caller"
Georg Brandl15c5ce92007-03-07 09:09:40 +00002246 def test_run__uses_defaultTestResult(self):
2247 events = []
Tim Petersea5962f2007-03-12 18:07:52 +00002248
Georg Brandl15c5ce92007-03-07 09:09:40 +00002249 class Foo(unittest.TestCase):
2250 def test(self):
2251 events.append('test')
Tim Petersea5962f2007-03-12 18:07:52 +00002252
Georg Brandl15c5ce92007-03-07 09:09:40 +00002253 def defaultTestResult(self):
2254 return LoggingResult(events)
Tim Petersea5962f2007-03-12 18:07:52 +00002255
2256 # Make run() find a result object on its own
Georg Brandl15c5ce92007-03-07 09:09:40 +00002257 Foo('test').run()
Tim Petersea5962f2007-03-12 18:07:52 +00002258
Benjamin Peterson692428e2009-03-23 21:50:21 +00002259 expected = ['startTest', 'test', 'addSuccess', 'stopTest']
Georg Brandl15c5ce92007-03-07 09:09:40 +00002260 self.assertEqual(events, expected)
Jim Fultonfafd8742004-08-28 15:22:12 +00002261
Gregory P. Smith28399852009-03-31 16:54:10 +00002262 def testShortDescriptionWithoutDocstring(self):
2263 self.assertEqual(
2264 self.shortDescription(),
2265 'testShortDescriptionWithoutDocstring (' + __name__ +
2266 '.Test_TestCase)')
2267
2268 def testShortDescriptionWithOneLineDocstring(self):
2269 """Tests shortDescription() for a method with a docstring."""
2270 self.assertEqual(
2271 self.shortDescription(),
2272 ('testShortDescriptionWithOneLineDocstring '
2273 '(' + __name__ + '.Test_TestCase)\n'
2274 'Tests shortDescription() for a method with a docstring.'))
2275
2276 def testShortDescriptionWithMultiLineDocstring(self):
2277 """Tests shortDescription() for a method with a longer docstring.
2278
2279 This method ensures that only the first line of a docstring is
2280 returned used in the short description, no matter how long the
2281 whole thing is.
2282 """
2283 self.assertEqual(
2284 self.shortDescription(),
2285 ('testShortDescriptionWithMultiLineDocstring '
2286 '(' + __name__ + '.Test_TestCase)\n'
2287 'Tests shortDescription() for a method with a longer '
2288 'docstring.'))
2289
Gregory P. Smith28399852009-03-31 16:54:10 +00002290 def testAddTypeEqualityFunc(self):
2291 class SadSnake(object):
2292 """Dummy class for test_addTypeEqualityFunc."""
2293 s1, s2 = SadSnake(), SadSnake()
2294 self.assertFalse(s1 == s2)
2295 def AllSnakesCreatedEqual(a, b, msg=None):
2296 return type(a) == type(b) == SadSnake
2297 self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual)
2298 self.assertEqual(s1, s2)
2299 # No this doesn't clean up and remove the SadSnake equality func
2300 # from this TestCase instance but since its a local nothing else
2301 # will ever notice that.
2302
2303 def testAssertIn(self):
2304 animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
2305
2306 self.assertIn('a', 'abc')
2307 self.assertIn(2, [1, 2, 3])
2308 self.assertIn('monkey', animals)
2309
2310 self.assertNotIn('d', 'abc')
2311 self.assertNotIn(0, [1, 2, 3])
2312 self.assertNotIn('otter', animals)
2313
2314 self.assertRaises(self.failureException, self.assertIn, 'x', 'abc')
2315 self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3])
2316 self.assertRaises(self.failureException, self.assertIn, 'elephant',
2317 animals)
2318
2319 self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc')
2320 self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3])
2321 self.assertRaises(self.failureException, self.assertNotIn, 'cow',
2322 animals)
2323
2324 def testAssertDictContainsSubset(self):
2325 self.assertDictContainsSubset({}, {})
2326 self.assertDictContainsSubset({}, {'a': 1})
2327 self.assertDictContainsSubset({'a': 1}, {'a': 1})
2328 self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})
2329 self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
2330
2331 self.assertRaises(unittest.TestCase.failureException,
2332 self.assertDictContainsSubset, {'a': 2}, {'a': 1},
2333 '.*Mismatched values:.*')
2334
2335 self.assertRaises(unittest.TestCase.failureException,
2336 self.assertDictContainsSubset, {'c': 1}, {'a': 1},
2337 '.*Missing:.*')
2338
2339 self.assertRaises(unittest.TestCase.failureException,
2340 self.assertDictContainsSubset, {'a': 1, 'c': 1},
2341 {'a': 1}, '.*Missing:.*')
2342
2343 self.assertRaises(unittest.TestCase.failureException,
2344 self.assertDictContainsSubset, {'a': 1, 'c': 1},
2345 {'a': 1}, '.*Missing:.*Mismatched values:.*')
2346
2347 def testAssertEqual(self):
2348 equal_pairs = [
2349 ((), ()),
2350 ({}, {}),
2351 ([], []),
2352 (set(), set()),
2353 (frozenset(), frozenset())]
2354 for a, b in equal_pairs:
2355 # This mess of try excepts is to test the assertEqual behavior
2356 # itself.
2357 try:
2358 self.assertEqual(a, b)
2359 except self.failureException:
2360 self.fail('assertEqual(%r, %r) failed' % (a, b))
2361 try:
2362 self.assertEqual(a, b, msg='foo')
2363 except self.failureException:
2364 self.fail('assertEqual(%r, %r) with msg= failed' % (a, b))
2365 try:
2366 self.assertEqual(a, b, 'foo')
2367 except self.failureException:
2368 self.fail('assertEqual(%r, %r) with third parameter failed' %
2369 (a, b))
2370
2371 unequal_pairs = [
2372 ((), []),
2373 ({}, set()),
2374 (set([4,1]), frozenset([4,2])),
2375 (frozenset([4,5]), set([2,3])),
2376 (set([3,4]), set([5,4]))]
2377 for a, b in unequal_pairs:
2378 self.assertRaises(self.failureException, self.assertEqual, a, b)
2379 self.assertRaises(self.failureException, self.assertEqual, a, b,
2380 'foo')
2381 self.assertRaises(self.failureException, self.assertEqual, a, b,
2382 msg='foo')
2383
2384 def testEquality(self):
2385 self.assertListEqual([], [])
2386 self.assertTupleEqual((), ())
2387 self.assertSequenceEqual([], ())
2388
2389 a = [0, 'a', []]
2390 b = []
2391 self.assertRaises(unittest.TestCase.failureException,
2392 self.assertListEqual, a, b)
2393 self.assertRaises(unittest.TestCase.failureException,
2394 self.assertListEqual, tuple(a), tuple(b))
2395 self.assertRaises(unittest.TestCase.failureException,
2396 self.assertSequenceEqual, a, tuple(b))
2397
2398 b.extend(a)
2399 self.assertListEqual(a, b)
2400 self.assertTupleEqual(tuple(a), tuple(b))
2401 self.assertSequenceEqual(a, tuple(b))
2402 self.assertSequenceEqual(tuple(a), b)
2403
2404 self.assertRaises(self.failureException, self.assertListEqual,
2405 a, tuple(b))
2406 self.assertRaises(self.failureException, self.assertTupleEqual,
2407 tuple(a), b)
2408 self.assertRaises(self.failureException, self.assertListEqual, None, b)
2409 self.assertRaises(self.failureException, self.assertTupleEqual, None,
2410 tuple(b))
2411 self.assertRaises(self.failureException, self.assertSequenceEqual,
2412 None, tuple(b))
2413 self.assertRaises(self.failureException, self.assertListEqual, 1, 1)
2414 self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1)
2415 self.assertRaises(self.failureException, self.assertSequenceEqual,
2416 1, 1)
2417
2418 self.assertDictEqual({}, {})
2419
2420 c = { 'x': 1 }
2421 d = {}
2422 self.assertRaises(unittest.TestCase.failureException,
2423 self.assertDictEqual, c, d)
2424
2425 d.update(c)
2426 self.assertDictEqual(c, d)
2427
2428 d['x'] = 0
2429 self.assertRaises(unittest.TestCase.failureException,
2430 self.assertDictEqual, c, d, 'These are unequal')
2431
2432 self.assertRaises(self.failureException, self.assertDictEqual, None, d)
2433 self.assertRaises(self.failureException, self.assertDictEqual, [], d)
2434 self.assertRaises(self.failureException, self.assertDictEqual, 1, 1)
2435
2436 self.assertSameElements([1, 2, 3], [3, 2, 1])
2437 self.assertSameElements([1, 2] + [3] * 100, [1] * 100 + [2, 3])
2438 self.assertSameElements(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo'])
2439 self.assertRaises(self.failureException, self.assertSameElements,
2440 [10], [10, 11])
2441 self.assertRaises(self.failureException, self.assertSameElements,
2442 [10, 11], [10])
2443
2444 # Test that sequences of unhashable objects can be tested for sameness:
2445 self.assertSameElements([[1, 2], [3, 4]], [[3, 4], [1, 2]])
2446 self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
2447 self.assertRaises(self.failureException, self.assertSameElements,
2448 [[1]], [[2]])
2449
2450 def testAssertSetEqual(self):
2451 set1 = set()
2452 set2 = set()
2453 self.assertSetEqual(set1, set2)
2454
2455 self.assertRaises(self.failureException, self.assertSetEqual, None, set2)
2456 self.assertRaises(self.failureException, self.assertSetEqual, [], set2)
2457 self.assertRaises(self.failureException, self.assertSetEqual, set1, None)
2458 self.assertRaises(self.failureException, self.assertSetEqual, set1, [])
2459
2460 set1 = set(['a'])
2461 set2 = set()
2462 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
2463
2464 set1 = set(['a'])
2465 set2 = set(['a'])
2466 self.assertSetEqual(set1, set2)
2467
2468 set1 = set(['a'])
2469 set2 = set(['a', 'b'])
2470 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
2471
2472 set1 = set(['a'])
2473 set2 = frozenset(['a', 'b'])
2474 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
2475
2476 set1 = set(['a', 'b'])
2477 set2 = frozenset(['a', 'b'])
2478 self.assertSetEqual(set1, set2)
2479
2480 set1 = set()
2481 set2 = "foo"
2482 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
2483 self.assertRaises(self.failureException, self.assertSetEqual, set2, set1)
2484
2485 # make sure any string formatting is tuple-safe
2486 set1 = set([(0, 1), (2, 3)])
2487 set2 = set([(4, 5)])
2488 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
2489
2490 def testInequality(self):
2491 # Try ints
2492 self.assertGreater(2, 1)
2493 self.assertGreaterEqual(2, 1)
2494 self.assertGreaterEqual(1, 1)
2495 self.assertLess(1, 2)
2496 self.assertLessEqual(1, 2)
2497 self.assertLessEqual(1, 1)
2498 self.assertRaises(self.failureException, self.assertGreater, 1, 2)
2499 self.assertRaises(self.failureException, self.assertGreater, 1, 1)
2500 self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2)
2501 self.assertRaises(self.failureException, self.assertLess, 2, 1)
2502 self.assertRaises(self.failureException, self.assertLess, 1, 1)
2503 self.assertRaises(self.failureException, self.assertLessEqual, 2, 1)
2504
2505 # Try Floats
2506 self.assertGreater(1.1, 1.0)
2507 self.assertGreaterEqual(1.1, 1.0)
2508 self.assertGreaterEqual(1.0, 1.0)
2509 self.assertLess(1.0, 1.1)
2510 self.assertLessEqual(1.0, 1.1)
2511 self.assertLessEqual(1.0, 1.0)
2512 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1)
2513 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0)
2514 self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1)
2515 self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0)
2516 self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0)
2517 self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0)
2518
2519 # Try Strings
2520 self.assertGreater('bug', 'ant')
2521 self.assertGreaterEqual('bug', 'ant')
2522 self.assertGreaterEqual('ant', 'ant')
2523 self.assertLess('ant', 'bug')
2524 self.assertLessEqual('ant', 'bug')
2525 self.assertLessEqual('ant', 'ant')
2526 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug')
2527 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant')
2528 self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug')
2529 self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant')
2530 self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant')
2531 self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant')
2532
2533 # Try Unicode
2534 self.assertGreater(u'bug', u'ant')
2535 self.assertGreaterEqual(u'bug', u'ant')
2536 self.assertGreaterEqual(u'ant', u'ant')
2537 self.assertLess(u'ant', u'bug')
2538 self.assertLessEqual(u'ant', u'bug')
2539 self.assertLessEqual(u'ant', u'ant')
2540 self.assertRaises(self.failureException, self.assertGreater, u'ant', u'bug')
2541 self.assertRaises(self.failureException, self.assertGreater, u'ant', u'ant')
2542 self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',
2543 u'bug')
2544 self.assertRaises(self.failureException, self.assertLess, u'bug', u'ant')
2545 self.assertRaises(self.failureException, self.assertLess, u'ant', u'ant')
2546 self.assertRaises(self.failureException, self.assertLessEqual, u'bug', u'ant')
2547
2548 # Try Mixed String/Unicode
2549 self.assertGreater('bug', u'ant')
2550 self.assertGreater(u'bug', 'ant')
2551 self.assertGreaterEqual('bug', u'ant')
2552 self.assertGreaterEqual(u'bug', 'ant')
2553 self.assertGreaterEqual('ant', u'ant')
2554 self.assertGreaterEqual(u'ant', 'ant')
2555 self.assertLess('ant', u'bug')
2556 self.assertLess(u'ant', 'bug')
2557 self.assertLessEqual('ant', u'bug')
2558 self.assertLessEqual(u'ant', 'bug')
2559 self.assertLessEqual('ant', u'ant')
2560 self.assertLessEqual(u'ant', 'ant')
2561 self.assertRaises(self.failureException, self.assertGreater, 'ant', u'bug')
2562 self.assertRaises(self.failureException, self.assertGreater, u'ant', 'bug')
2563 self.assertRaises(self.failureException, self.assertGreater, 'ant', u'ant')
2564 self.assertRaises(self.failureException, self.assertGreater, u'ant', 'ant')
2565 self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant',
2566 u'bug')
2567 self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',
2568 'bug')
2569 self.assertRaises(self.failureException, self.assertLess, 'bug', u'ant')
2570 self.assertRaises(self.failureException, self.assertLess, u'bug', 'ant')
2571 self.assertRaises(self.failureException, self.assertLess, 'ant', u'ant')
2572 self.assertRaises(self.failureException, self.assertLess, u'ant', 'ant')
2573 self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u'ant')
2574 self.assertRaises(self.failureException, self.assertLessEqual, u'bug', 'ant')
2575
2576 def testAssertMultiLineEqual(self):
2577 sample_text = b"""\
2578http://www.python.org/doc/2.3/lib/module-unittest.html
2579test case
2580 A test case is the smallest unit of testing. [...]
2581"""
2582 revised_sample_text = b"""\
2583http://www.python.org/doc/2.4.1/lib/module-unittest.html
2584test case
2585 A test case is the smallest unit of testing. [...] You may provide your
2586 own implementation that does not subclass from TestCase, of course.
2587"""
2588 sample_text_error = b"""
2589- http://www.python.org/doc/2.3/lib/module-unittest.html
2590? ^
2591+ http://www.python.org/doc/2.4.1/lib/module-unittest.html
2592? ^^^
2593 test case
2594- A test case is the smallest unit of testing. [...]
2595+ A test case is the smallest unit of testing. [...] You may provide your
2596? +++++++++++++++++++++
2597+ own implementation that does not subclass from TestCase, of course.
2598"""
2599
2600 for type_changer in (lambda x: x, lambda x: x.decode('utf8')):
2601 try:
2602 self.assertMultiLineEqual(type_changer(sample_text),
2603 type_changer(revised_sample_text))
2604 except self.failureException, e:
2605 # no fair testing ourself with ourself, use assertEqual..
2606 self.assertEqual(sample_text_error, str(e).encode('utf8'))
2607
2608 def testAssertIsNone(self):
2609 self.assertIsNone(None)
2610 self.assertRaises(self.failureException, self.assertIsNone, False)
2611 self.assertIsNotNone('DjZoPloGears on Rails')
2612 self.assertRaises(self.failureException, self.assertIsNotNone, None)
2613
2614 def testAssertRegexpMatches(self):
2615 self.assertRegexpMatches('asdfabasdf', r'ab+')
2616 self.assertRaises(self.failureException, self.assertRegexpMatches,
2617 'saaas', r'aaaa')
2618
2619 def testAssertRaisesRegexp(self):
2620 class ExceptionMock(Exception):
2621 pass
2622
2623 def Stub():
2624 raise ExceptionMock('We expect')
2625
2626 self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub)
2627 self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub)
2628 self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub)
2629
2630 def testAssertNotRaisesRegexp(self):
2631 self.assertRaisesRegexp(
2632 self.failureException, '^Exception not raised$',
2633 self.assertRaisesRegexp, Exception, re.compile('x'),
2634 lambda: None)
2635 self.assertRaisesRegexp(
2636 self.failureException, '^Exception not raised$',
2637 self.assertRaisesRegexp, Exception, 'x',
2638 lambda: None)
2639 self.assertRaisesRegexp(
2640 self.failureException, '^Exception not raised$',
2641 self.assertRaisesRegexp, Exception, u'x',
2642 lambda: None)
2643
2644 def testAssertRaisesRegexpMismatch(self):
2645 def Stub():
2646 raise Exception('Unexpected')
2647
2648 self.assertRaisesRegexp(
2649 self.failureException,
2650 r'"\^Expected\$" does not match "Unexpected"',
2651 self.assertRaisesRegexp, Exception, '^Expected$',
2652 Stub)
2653 self.assertRaisesRegexp(
2654 self.failureException,
2655 r'"\^Expected\$" does not match "Unexpected"',
2656 self.assertRaisesRegexp, Exception, u'^Expected$',
2657 Stub)
2658 self.assertRaisesRegexp(
2659 self.failureException,
2660 r'"\^Expected\$" does not match "Unexpected"',
2661 self.assertRaisesRegexp, Exception,
2662 re.compile('^Expected$'), Stub)
2663
Gregory P. Smith7558d572009-03-31 19:03:28 +00002664 def testSynonymAssertMethodNames(self):
2665 """Test undocumented method name synonyms.
2666
2667 Please do not use these methods names in your own code.
2668
2669 This test confirms their continued existence and functionality
2670 in order to avoid breaking existing code.
2671 """
2672 self.assertNotEquals(3, 5)
2673 self.assertEquals(3, 3)
2674 self.assertAlmostEquals(2.0, 2.0)
2675 self.assertNotAlmostEquals(3.0, 5.0)
2676 self.assert_(True)
2677
2678 def testPendingDeprecationMethodNames(self):
2679 """Test fail* methods pending deprecation, they will warn in 3.2.
2680
2681 Do not use these methods. They will go away in 3.3.
2682 """
2683 self.failIfEqual(3, 5)
2684 self.failUnlessEqual(3, 3)
2685 self.failUnlessAlmostEqual(2.0, 2.0)
2686 self.failIfAlmostEqual(3.0, 5.0)
2687 self.failUnless(True)
2688 self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam')
2689 self.failIf(False)
2690
Benjamin Peterson692428e2009-03-23 21:50:21 +00002691
2692class Test_TestSkipping(TestCase):
2693
2694 def test_skipping(self):
2695 class Foo(unittest.TestCase):
2696 def test_skip_me(self):
Benjamin Peterson47d97382009-03-26 20:05:50 +00002697 self.skipTest("skip")
Benjamin Peterson692428e2009-03-23 21:50:21 +00002698 events = []
2699 result = LoggingResult(events)
2700 test = Foo("test_skip_me")
2701 test.run(result)
2702 self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
2703 self.assertEqual(result.skipped, [(test, "skip")])
2704
2705 # Try letting setUp skip the test now.
2706 class Foo(unittest.TestCase):
2707 def setUp(self):
Benjamin Peterson47d97382009-03-26 20:05:50 +00002708 self.skipTest("testing")
Benjamin Peterson692428e2009-03-23 21:50:21 +00002709 def test_nothing(self): pass
2710 events = []
2711 result = LoggingResult(events)
2712 test = Foo("test_nothing")
2713 test.run(result)
2714 self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
2715 self.assertEqual(result.skipped, [(test, "testing")])
2716 self.assertEqual(result.testsRun, 1)
2717
2718 def test_skipping_decorators(self):
2719 op_table = ((unittest.skipUnless, False, True),
2720 (unittest.skipIf, True, False))
2721 for deco, do_skip, dont_skip in op_table:
2722 class Foo(unittest.TestCase):
2723 @deco(do_skip, "testing")
2724 def test_skip(self): pass
2725
2726 @deco(dont_skip, "testing")
2727 def test_dont_skip(self): pass
2728 test_do_skip = Foo("test_skip")
2729 test_dont_skip = Foo("test_dont_skip")
2730 suite = unittest.ClassTestSuite([test_do_skip, test_dont_skip], Foo)
2731 events = []
2732 result = LoggingResult(events)
2733 suite.run(result)
2734 self.assertEqual(len(result.skipped), 1)
2735 expected = ['startTest', 'addSkip', 'stopTest',
2736 'startTest', 'addSuccess', 'stopTest']
2737 self.assertEqual(events, expected)
2738 self.assertEqual(result.testsRun, 2)
2739 self.assertEqual(result.skipped, [(test_do_skip, "testing")])
2740 self.assertTrue(result.wasSuccessful())
2741
2742 def test_skip_class(self):
2743 @unittest.skip("testing")
2744 class Foo(unittest.TestCase):
2745 def test_1(self):
2746 record.append(1)
2747 record = []
2748 result = unittest.TestResult()
2749 suite = unittest.ClassTestSuite([Foo("test_1")], Foo)
2750 suite.run(result)
2751 self.assertEqual(result.skipped, [(suite, "testing")])
2752 self.assertEqual(record, [])
2753
2754 def test_expected_failure(self):
2755 class Foo(unittest.TestCase):
2756 @unittest.expectedFailure
2757 def test_die(self):
2758 self.fail("help me!")
2759 events = []
2760 result = LoggingResult(events)
2761 test = Foo("test_die")
2762 test.run(result)
2763 self.assertEqual(events,
2764 ['startTest', 'addExpectedFailure', 'stopTest'])
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +00002765 self.assertEqual(result.expectedFailures[0][0], test)
Benjamin Peterson692428e2009-03-23 21:50:21 +00002766 self.assertTrue(result.wasSuccessful())
2767
2768 def test_unexpected_success(self):
2769 class Foo(unittest.TestCase):
2770 @unittest.expectedFailure
2771 def test_die(self):
2772 pass
2773 events = []
2774 result = LoggingResult(events)
2775 test = Foo("test_die")
2776 test.run(result)
2777 self.assertEqual(events,
2778 ['startTest', 'addUnexpectedSuccess', 'stopTest'])
2779 self.assertFalse(result.failures)
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +00002780 self.assertEqual(result.unexpectedSuccesses, [test])
Benjamin Peterson692428e2009-03-23 21:50:21 +00002781 self.assertTrue(result.wasSuccessful())
2782
2783
2784
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002785class Test_Assertions(TestCase):
2786 def test_AlmostEqual(self):
2787 self.failUnlessAlmostEqual(1.00000001, 1.0)
2788 self.failIfAlmostEqual(1.0000001, 1.0)
Gregory P. Smith28399852009-03-31 16:54:10 +00002789 self.assertRaises(self.failureException,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002790 self.failUnlessAlmostEqual, 1.0000001, 1.0)
Gregory P. Smith28399852009-03-31 16:54:10 +00002791 self.assertRaises(self.failureException,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002792 self.failIfAlmostEqual, 1.00000001, 1.0)
2793
2794 self.failUnlessAlmostEqual(1.1, 1.0, places=0)
Gregory P. Smith28399852009-03-31 16:54:10 +00002795 self.assertRaises(self.failureException,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002796 self.failUnlessAlmostEqual, 1.1, 1.0, places=1)
2797
2798 self.failUnlessAlmostEqual(0, .1+.1j, places=0)
2799 self.failIfAlmostEqual(0, .1+.1j, places=1)
Gregory P. Smith28399852009-03-31 16:54:10 +00002800 self.assertRaises(self.failureException,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002801 self.failUnlessAlmostEqual, 0, .1+.1j, places=1)
Gregory P. Smith28399852009-03-31 16:54:10 +00002802 self.assertRaises(self.failureException,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002803 self.failIfAlmostEqual, 0, .1+.1j, places=0)
2804
Antoine Pitrou697ca3d2008-12-28 14:09:36 +00002805 def test_assertRaises(self):
2806 def _raise(e):
2807 raise e
2808 self.assertRaises(KeyError, _raise, KeyError)
2809 self.assertRaises(KeyError, _raise, KeyError("key"))
2810 try:
2811 self.assertRaises(KeyError, lambda: None)
Gregory P. Smith28399852009-03-31 16:54:10 +00002812 except self.failureException as e:
Antoine Pitrou697ca3d2008-12-28 14:09:36 +00002813 self.assert_("KeyError not raised" in e, str(e))
2814 else:
2815 self.fail("assertRaises() didn't fail")
2816 try:
2817 self.assertRaises(KeyError, _raise, ValueError)
2818 except ValueError:
2819 pass
2820 else:
2821 self.fail("assertRaises() didn't let exception pass through")
2822 with self.assertRaises(KeyError):
2823 raise KeyError
2824 with self.assertRaises(KeyError):
2825 raise KeyError("key")
2826 try:
2827 with self.assertRaises(KeyError):
2828 pass
Gregory P. Smith28399852009-03-31 16:54:10 +00002829 except self.failureException as e:
Antoine Pitrou697ca3d2008-12-28 14:09:36 +00002830 self.assert_("KeyError not raised" in e, str(e))
2831 else:
2832 self.fail("assertRaises() didn't fail")
2833 try:
2834 with self.assertRaises(KeyError):
2835 raise ValueError
2836 except ValueError:
2837 pass
2838 else:
2839 self.fail("assertRaises() didn't let exception pass through")
2840
2841
Michael Foord345b2fe2009-04-02 03:20:38 +00002842class TestLongMessage(TestCase):
2843 """Test that the individual asserts honour longMessage.
2844 This actually tests all the message behaviour for
2845 asserts that use longMessage."""
2846
2847 def setUp(self):
2848 class TestableTestFalse(TestCase):
2849 longMessage = False
2850 failureException = self.failureException
2851
2852 def testTest(self):
2853 pass
2854
2855 class TestableTestTrue(TestCase):
2856 longMessage = True
2857 failureException = self.failureException
2858
2859 def testTest(self):
2860 pass
2861
2862 self.testableTrue = TestableTestTrue('testTest')
2863 self.testableFalse = TestableTestFalse('testTest')
2864
2865 def testDefault(self):
2866 self.assertFalse(TestCase.longMessage)
2867
2868 def test_formatMsg(self):
2869 self.assertEquals(self.testableFalse._formatMessage(None, "foo"), "foo")
2870 self.assertEquals(self.testableFalse._formatMessage("foo", "bar"), "foo")
2871
2872 self.assertEquals(self.testableTrue._formatMessage(None, "foo"), "foo")
2873 self.assertEquals(self.testableTrue._formatMessage("foo", "bar"), "bar : foo")
2874
2875 def assertMessages(self, methodName, args, errors):
2876 def getMethod(i):
2877 useTestableFalse = i < 2
2878 if useTestableFalse:
2879 test = self.testableFalse
2880 else:
2881 test = self.testableTrue
2882 return getattr(test, methodName)
2883
2884 for i, expected_regexp in enumerate(errors):
2885 testMethod = getMethod(i)
2886 kwargs = {}
2887 withMsg = i % 2
2888 if withMsg:
2889 kwargs = {"msg": "oops"}
2890
2891 with self.assertRaisesRegexp(self.failureException,
2892 expected_regexp=expected_regexp):
2893 testMethod(*args, **kwargs)
2894
2895 def testAssertTrue(self):
2896 self.assertMessages('assertTrue', (False,),
2897 ["^False is not True$", "^oops$", "^False is not True$",
2898 "^False is not True : oops$"])
2899
2900 def testAssertFalse(self):
2901 self.assertMessages('assertFalse', (True,),
2902 ["^True is not False$", "^oops$", "^True is not False$",
2903 "^True is not False : oops$"])
2904
2905 def testNotEqual(self):
2906 self.assertMessages('assertNotEqual', (1, 1),
2907 ["^1 == 1$", "^oops$", "^1 == 1$",
2908 "^1 == 1 : oops$"])
2909
2910 def testAlmostEqual(self):
2911 self.assertMessages('assertAlmostEqual', (1, 2),
2912 ["^1 != 2 within 7 places$", "^oops$",
2913 "^1 != 2 within 7 places$", "^1 != 2 within 7 places : oops$"])
2914
2915 def testNotAlmostEqual(self):
2916 self.assertMessages('assertNotAlmostEqual', (1, 1),
2917 ["^1 == 1 within 7 places$", "^oops$",
2918 "^1 == 1 within 7 places$", "^1 == 1 within 7 places : oops$"])
2919
2920 def test_baseAssertEqual(self):
2921 self.assertMessages('_baseAssertEqual', (1, 2),
2922 ["^1 != 2$", "^oops$", "^1 != 2$", "^1 != 2 : oops$"])
2923
2924 def testAssertSequenceEqual(self):
2925 # Error messages are multiline so not testing on full message
2926 # assertTupleEqual and assertListEqual delegate to this method
2927 self.assertMessages('assertSequenceEqual', ([], [None]),
2928 ["\+ \[None\]$", "^oops$", r"\+ \[None\]$",
2929 r"\+ \[None\] : oops$"])
2930
2931 def testAssertSetEqual(self):
2932 self.assertMessages('assertSetEqual', (set(), set([None])),
2933 ["None$", "^oops$", "None$",
2934 "None : oops$"])
2935
2936 def testAssertIn(self):
2937 self.assertMessages('assertIn', (None, []),
2938 ['^None not found in \[\]$', "^oops$",
2939 '^None not found in \[\]$',
2940 '^None not found in \[\] : oops$'])
2941
2942 def testAssertNotIn(self):
2943 self.assertMessages('assertNotIn', (None, [None]),
2944 ['^None unexpectedly found in \[None\]$', "^oops$",
2945 '^None unexpectedly found in \[None\]$',
2946 '^None unexpectedly found in \[None\] : oops$'])
2947
2948 def testAssertDictEqual(self):
2949 self.assertMessages('assertDictEqual', ({}, {'key': 'value'}),
2950 [r"\+ \{'key': 'value'\}$", "^oops$",
2951 "\+ \{'key': 'value'\}$",
2952 "\+ \{'key': 'value'\} : oops$"])
2953
2954 def testAssertDictContainsSubset(self):
2955 self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}),
2956 ["^Missing: 'key'$", "^oops$",
2957 "^Missing: 'key'$",
2958 "^Missing: 'key' : oops$"])
2959
2960 def testAssertSameElements(self):
2961 self.assertMessages('assertSameElements', ([], [None]),
2962 [r"\[None\]$", "^oops$",
2963 r"\[None\]$",
2964 r"\[None\] : oops$"])
2965
2966 def testAssertMultiLineEqual(self):
2967 self.assertMessages('assertMultiLineEqual', ("", "foo"),
2968 [r"\+ foo$", "^oops$",
2969 r"\+ foo$",
2970 r"\+ foo : oops$"])
2971
2972 def testAssertLess(self):
2973 self.assertMessages('assertLess', (2, 1),
2974 ["^2 not less than 1$", "^oops$",
2975 "^2 not less than 1$", "^2 not less than 1 : oops$"])
2976
2977 def testAssertLessEqual(self):
2978 self.assertMessages('assertLessEqual', (2, 1),
2979 ["^2 not less than or equal to 1$", "^oops$",
2980 "^2 not less than or equal to 1$",
2981 "^2 not less than or equal to 1 : oops$"])
2982
2983 def testAssertGreater(self):
2984 self.assertMessages('assertGreater', (1, 2),
2985 ["^1 not greater than 2$", "^oops$",
2986 "^1 not greater than 2$",
2987 "^1 not greater than 2 : oops$"])
2988
2989 def testAssertGreaterEqual(self):
2990 self.assertMessages('assertGreaterEqual', (1, 2),
2991 ["^1 not greater than or equal to 2$", "^oops$",
2992 "^1 not greater than or equal to 2$",
2993 "^1 not greater than or equal to 2 : oops$"])
2994
2995 def testAssertIsNone(self):
2996 self.assertMessages('assertIsNone', ('not None',),
2997 ["^'not None' is not None$", "^oops$",
2998 "^'not None' is not None$",
2999 "^'not None' is not None : oops$"])
3000
3001 def testAssertIsNotNone(self):
3002 self.assertMessages('assertIsNotNone', (None,),
3003 ["^unexpectedly None$", "^oops$",
3004 "^unexpectedly None$",
3005 "^unexpectedly None : oops$"])
3006
3007
Jim Fultonfafd8742004-08-28 15:22:12 +00003008######################################################################
3009## Main
3010######################################################################
3011
3012def test_main():
Georg Brandl15c5ce92007-03-07 09:09:40 +00003013 test_support.run_unittest(Test_TestCase, Test_TestLoader,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003014 Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
Michael Foord345b2fe2009-04-02 03:20:38 +00003015 Test_TestSkipping, Test_Assertions, TestLongMessage)
Jim Fultonfafd8742004-08-28 15:22:12 +00003016
Georg Brandl15c5ce92007-03-07 09:09:40 +00003017if __name__ == "__main__":
Tim Petersea5962f2007-03-12 18:07:52 +00003018 test_main()