blob: 12017dd356d6d3b807ea6bd3f44e9e6fc64fd93e [file] [log] [blame]
Fred Drake02538202001-03-21 18:09:46 +00001#!/usr/bin/env python
Steve Purcell5ddd1a82001-03-22 08:45:36 +00002'''
Fred Drake02538202001-03-21 18:09:46 +00003Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's
4Smalltalk testing framework.
5
Fred Drake02538202001-03-21 18:09:46 +00006This module contains the core framework classes that form the basis of
7specific test cases and suites (TestCase, TestSuite etc.), and also a
8text-based utility class for running the tests and reporting the results
Jeremy Hyltonefef5da2001-10-22 18:14:15 +00009 (TextTestRunner).
Fred Drake02538202001-03-21 18:09:46 +000010
Steve Purcell5ddd1a82001-03-22 08:45:36 +000011Simple usage:
12
13 import unittest
14
15 class IntegerArithmenticTestCase(unittest.TestCase):
16 def testAdd(self): ## test method names begin 'test*'
17 self.assertEquals((1 + 2), 3)
18 self.assertEquals(0 + 1, 1)
Steve Purcell7b065702001-09-06 08:24:40 +000019 def testMultiply(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +000020 self.assertEquals((0 * 10), 0)
21 self.assertEquals((5 * 8), 40)
22
23 if __name__ == '__main__':
24 unittest.main()
25
26Further information is available in the bundled documentation, and from
27
Guido van Rossumd8faa362007-04-27 19:54:29 +000028 http://docs.python.org/lib/module-unittest.html
Steve Purcell5ddd1a82001-03-22 08:45:36 +000029
Steve Purcell7e743842003-09-22 11:08:12 +000030Copyright (c) 1999-2003 Steve Purcell
Fred Drake02538202001-03-21 18:09:46 +000031This module is free software, and you may redistribute it and/or modify
32it under the same terms as Python itself, so long as this copyright message
33and disclaimer are retained in their original form.
34
35IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
36SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
37THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
38DAMAGE.
39
40THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
41LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
42PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
43AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
44SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Steve Purcell5ddd1a82001-03-22 08:45:36 +000045'''
Fred Drake02538202001-03-21 18:09:46 +000046
Steve Purcell5ddd1a82001-03-22 08:45:36 +000047__author__ = "Steve Purcell"
48__email__ = "stephen_purcell at yahoo dot com"
Steve Purcellb8d5f242003-12-06 13:03:13 +000049__version__ = "#Revision: 1.63 $"[11:-2]
Fred Drake02538202001-03-21 18:09:46 +000050
51import time
52import sys
53import traceback
Fred Drake02538202001-03-21 18:09:46 +000054import os
Steve Purcell5ddd1a82001-03-22 08:45:36 +000055import types
Fred Drake02538202001-03-21 18:09:46 +000056
57##############################################################################
Steve Purcelld75e7e42003-09-15 11:01:21 +000058# Exported classes and functions
59##############################################################################
60__all__ = ['TestResult', 'TestCase', 'TestSuite', 'TextTestRunner',
61 'TestLoader', 'FunctionTestCase', 'main', 'defaultTestLoader']
62
Steve Purcell7e743842003-09-22 11:08:12 +000063# Expose obsolete functions for backwards compatibility
Steve Purcelld75e7e42003-09-15 11:01:21 +000064__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
65
66
67##############################################################################
Steve Purcell7e743842003-09-22 11:08:12 +000068# Backward compatibility
69##############################################################################
70if sys.version_info[:2] < (2, 2):
71 False, True = 0, 1
72 def isinstance(obj, clsinfo):
73 import __builtin__
Raymond Hettingerf7153662005-02-07 14:16:21 +000074 if type(clsinfo) in (tuple, list):
Steve Purcell7e743842003-09-22 11:08:12 +000075 for cls in clsinfo:
76 if cls is type: cls = types.ClassType
77 if __builtin__.isinstance(obj, cls):
78 return 1
79 return 0
80 else: return __builtin__.isinstance(obj, clsinfo)
81
82
83##############################################################################
Fred Drake02538202001-03-21 18:09:46 +000084# Test framework core
85##############################################################################
86
Steve Purcelldc391a62002-08-09 09:46:23 +000087def _strclass(cls):
88 return "%s.%s" % (cls.__module__, cls.__name__)
89
Steve Purcellb8d5f242003-12-06 13:03:13 +000090__unittest = 1
91
Fred Drake02538202001-03-21 18:09:46 +000092class TestResult:
93 """Holder for test result information.
94
95 Test results are automatically managed by the TestCase and TestSuite
96 classes, and do not need to be explicitly manipulated by writers of tests.
97
98 Each instance holds the total number of tests run, and collections of
99 failures and errors that occurred among those test runs. The collections
Steve Purcell7b065702001-09-06 08:24:40 +0000100 contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
Fred Drake656f9ec2001-09-06 19:13:14 +0000101 formatted traceback of the error that occurred.
Fred Drake02538202001-03-21 18:09:46 +0000102 """
103 def __init__(self):
104 self.failures = []
105 self.errors = []
106 self.testsRun = 0
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107 self.shouldStop = False
Fred Drake02538202001-03-21 18:09:46 +0000108
109 def startTest(self, test):
110 "Called when the given test is about to be run"
111 self.testsRun = self.testsRun + 1
112
113 def stopTest(self, test):
114 "Called when the given test has been run"
115 pass
116
117 def addError(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000118 """Called when an error has occurred. 'err' is a tuple of values as
119 returned by sys.exc_info().
120 """
Steve Purcellb8d5f242003-12-06 13:03:13 +0000121 self.errors.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000122
123 def addFailure(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000124 """Called when an error has occurred. 'err' is a tuple of values as
125 returned by sys.exc_info()."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000126 self.failures.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000127
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000128 def addSuccess(self, test):
129 "Called when a test has completed successfully"
130 pass
131
Fred Drake02538202001-03-21 18:09:46 +0000132 def wasSuccessful(self):
133 "Tells whether or not this result was a success"
134 return len(self.failures) == len(self.errors) == 0
135
136 def stop(self):
137 "Indicates that the tests should be aborted"
Steve Purcell7e743842003-09-22 11:08:12 +0000138 self.shouldStop = True
Tim Petersa19a1682001-03-29 04:36:09 +0000139
Steve Purcellb8d5f242003-12-06 13:03:13 +0000140 def _exc_info_to_string(self, err, test):
Steve Purcell7b065702001-09-06 08:24:40 +0000141 """Converts a sys.exc_info()-style tuple of values into a string."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000142 exctype, value, tb = err
143 # Skip test runner traceback levels
144 while tb and self._is_relevant_tb_level(tb):
145 tb = tb.tb_next
146 if exctype is test.failureException:
147 # Skip assert*() traceback levels
148 length = self._count_relevant_tb_levels(tb)
149 return ''.join(traceback.format_exception(exctype, value, tb, length))
150 return ''.join(traceback.format_exception(exctype, value, tb))
151
152 def _is_relevant_tb_level(self, tb):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000153 return '__unittest' in tb.tb_frame.f_globals
Steve Purcellb8d5f242003-12-06 13:03:13 +0000154
155 def _count_relevant_tb_levels(self, tb):
156 length = 0
157 while tb and not self._is_relevant_tb_level(tb):
158 length += 1
159 tb = tb.tb_next
160 return length
Steve Purcell7b065702001-09-06 08:24:40 +0000161
Fred Drake02538202001-03-21 18:09:46 +0000162 def __repr__(self):
163 return "<%s run=%i errors=%i failures=%i>" % \
Steve Purcelldc391a62002-08-09 09:46:23 +0000164 (_strclass(self.__class__), self.testsRun, len(self.errors),
Fred Drake02538202001-03-21 18:09:46 +0000165 len(self.failures))
166
Fred Drake02538202001-03-21 18:09:46 +0000167class TestCase:
168 """A class whose instances are single test cases.
169
Fred Drake02538202001-03-21 18:09:46 +0000170 By default, the test code itself should be placed in a method named
171 'runTest'.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000172
Tim Petersa19a1682001-03-29 04:36:09 +0000173 If the fixture may be used for many test cases, create as
Fred Drake02538202001-03-21 18:09:46 +0000174 many test methods as are needed. When instantiating such a TestCase
175 subclass, specify in the constructor arguments the name of the test method
176 that the instance is to execute.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000177
Tim Petersa19a1682001-03-29 04:36:09 +0000178 Test authors should subclass TestCase for their own tests. Construction
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000179 and deconstruction of the test's environment ('fixture') can be
180 implemented by overriding the 'setUp' and 'tearDown' methods respectively.
181
182 If it is necessary to override the __init__ method, the base class
183 __init__ method must always be called. It is important that subclasses
184 should not change the signature of their __init__ method, since instances
185 of the classes are instantiated automatically by parts of the framework
186 in order to be run.
Fred Drake02538202001-03-21 18:09:46 +0000187 """
Steve Purcell15d89272001-04-12 09:05:01 +0000188
189 # This attribute determines which exception will be raised when
190 # the instance's assertion methods fail; test methods raising this
191 # exception will be deemed to have 'failed' rather than 'errored'
192
193 failureException = AssertionError
194
Fred Drake02538202001-03-21 18:09:46 +0000195 def __init__(self, methodName='runTest'):
196 """Create an instance of the class that will use the named test
197 method when executed. Raises a ValueError if the instance does
198 not have a method with the specified name.
199 """
200 try:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000201 self._testMethodName = methodName
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000202 testMethod = getattr(self, methodName)
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000203 self._testMethodDoc = testMethod.__doc__
Fred Drake02538202001-03-21 18:09:46 +0000204 except AttributeError:
205 raise ValueError, "no such test method in %s: %s" % \
206 (self.__class__, methodName)
207
208 def setUp(self):
209 "Hook method for setting up the test fixture before exercising it."
210 pass
211
212 def tearDown(self):
213 "Hook method for deconstructing the test fixture after testing it."
214 pass
215
216 def countTestCases(self):
217 return 1
218
219 def defaultTestResult(self):
220 return TestResult()
221
222 def shortDescription(self):
223 """Returns a one-line description of the test, or None if no
224 description has been provided.
225
226 The default implementation of this method returns the first line of
227 the specified test method's docstring.
228 """
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000229 doc = self._testMethodDoc
Steve Purcell7e743842003-09-22 11:08:12 +0000230 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000231
232 def id(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000233 return "%s.%s" % (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000234
Guido van Rossumd8faa362007-04-27 19:54:29 +0000235 def __eq__(self, other):
236 if type(self) is not type(other):
237 return False
238
239 return self._testMethodName == other._testMethodName
240
241 def __ne__(self, other):
242 return not self == other
243
244 def __hash__(self):
245 return hash((type(self), self._testMethodName))
246
Fred Drake02538202001-03-21 18:09:46 +0000247 def __str__(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000248 return "%s (%s)" % (self._testMethodName, _strclass(self.__class__))
Fred Drake02538202001-03-21 18:09:46 +0000249
250 def __repr__(self):
251 return "<%s testMethod=%s>" % \
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000252 (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000253
254 def run(self, result=None):
Fred Drake02538202001-03-21 18:09:46 +0000255 if result is None: result = self.defaultTestResult()
256 result.startTest(self)
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000257 testMethod = getattr(self, self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000258 try:
259 try:
260 self.setUp()
Guido van Rossum202dd1e2001-12-07 03:39:34 +0000261 except KeyboardInterrupt:
262 raise
Fred Drake02538202001-03-21 18:09:46 +0000263 except:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000264 result.addError(self, self._exc_info())
Fred Drake02538202001-03-21 18:09:46 +0000265 return
266
Steve Purcell7e743842003-09-22 11:08:12 +0000267 ok = False
Fred Drake02538202001-03-21 18:09:46 +0000268 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000269 testMethod()
Steve Purcell7e743842003-09-22 11:08:12 +0000270 ok = True
Skip Montanaroae5c37b2003-07-13 15:18:12 +0000271 except self.failureException:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000272 result.addFailure(self, self._exc_info())
Guido van Rossum202dd1e2001-12-07 03:39:34 +0000273 except KeyboardInterrupt:
274 raise
Fred Drake02538202001-03-21 18:09:46 +0000275 except:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000276 result.addError(self, self._exc_info())
Fred Drake02538202001-03-21 18:09:46 +0000277
278 try:
279 self.tearDown()
Guido van Rossum202dd1e2001-12-07 03:39:34 +0000280 except KeyboardInterrupt:
281 raise
Fred Drake02538202001-03-21 18:09:46 +0000282 except:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000283 result.addError(self, self._exc_info())
Steve Purcell7e743842003-09-22 11:08:12 +0000284 ok = False
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000285 if ok: result.addSuccess(self)
Fred Drake02538202001-03-21 18:09:46 +0000286 finally:
287 result.stopTest(self)
288
Raymond Hettinger664347b2004-12-04 21:21:53 +0000289 def __call__(self, *args, **kwds):
290 return self.run(*args, **kwds)
Steve Purcell7e743842003-09-22 11:08:12 +0000291
Fred Drake02538202001-03-21 18:09:46 +0000292 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000293 """Run the test without collecting errors in a TestResult"""
Fred Drake02538202001-03-21 18:09:46 +0000294 self.setUp()
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000295 getattr(self, self._testMethodName)()
Fred Drake02538202001-03-21 18:09:46 +0000296 self.tearDown()
297
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000298 def _exc_info(self):
Steve Purcell15d89272001-04-12 09:05:01 +0000299 """Return a version of sys.exc_info() with the traceback frame
300 minimised; usually the top level of the traceback frame is not
301 needed.
Fred Drake02538202001-03-21 18:09:46 +0000302 """
Guido van Rossumd8faa362007-04-27 19:54:29 +0000303 return sys.exc_info()
Fred Drake02538202001-03-21 18:09:46 +0000304
Steve Purcell15d89272001-04-12 09:05:01 +0000305 def fail(self, msg=None):
306 """Fail immediately, with the given message."""
307 raise self.failureException, msg
Fred Drake02538202001-03-21 18:09:46 +0000308
309 def failIf(self, expr, msg=None):
310 "Fail the test if the expression is true."
Steve Purcell15d89272001-04-12 09:05:01 +0000311 if expr: raise self.failureException, msg
Fred Drake02538202001-03-21 18:09:46 +0000312
Steve Purcell15d89272001-04-12 09:05:01 +0000313 def failUnless(self, expr, msg=None):
314 """Fail the test unless the expression is true."""
315 if not expr: raise self.failureException, msg
316
317 def failUnlessRaises(self, excClass, callableObj, *args, **kwargs):
318 """Fail unless an exception of class excClass is thrown
Fred Drake02538202001-03-21 18:09:46 +0000319 by callableObj when invoked with arguments args and keyword
320 arguments kwargs. If a different type of exception is
321 thrown, it will not be caught, and the test case will be
322 deemed to have suffered an error, exactly as for an
323 unexpected exception.
324 """
325 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000326 callableObj(*args, **kwargs)
Fred Drake02538202001-03-21 18:09:46 +0000327 except excClass:
328 return
329 else:
330 if hasattr(excClass,'__name__'): excName = excClass.__name__
331 else: excName = str(excClass)
Steve Purcell7e743842003-09-22 11:08:12 +0000332 raise self.failureException, "%s not raised" % excName
Fred Drake02538202001-03-21 18:09:46 +0000333
Steve Purcell15d89272001-04-12 09:05:01 +0000334 def failUnlessEqual(self, first, second, msg=None):
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000335 """Fail if the two objects are unequal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000336 operator.
337 """
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000338 if not first == second:
Steve Purcellca9aaf32001-12-17 10:13:17 +0000339 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000340 (msg or '%r != %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000341
Steve Purcell15d89272001-04-12 09:05:01 +0000342 def failIfEqual(self, first, second, msg=None):
343 """Fail if the two objects are equal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000344 operator.
345 """
Steve Purcell15d89272001-04-12 09:05:01 +0000346 if first == second:
Steve Purcellca9aaf32001-12-17 10:13:17 +0000347 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000348 (msg or '%r == %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000349
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000350 def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
351 """Fail if the two objects are unequal as determined by their
352 difference rounded to the given number of decimal places
353 (default 7) and comparing to zero.
354
Steve Purcell397b45d2003-10-26 10:41:03 +0000355 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000356 as significant digits (measured from the most signficant digit).
357 """
358 if round(second-first, places) != 0:
359 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000360 (msg or '%r != %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000361
362 def failIfAlmostEqual(self, first, second, places=7, msg=None):
363 """Fail if the two objects are equal as determined by their
364 difference rounded to the given number of decimal places
365 (default 7) and comparing to zero.
366
Steve Purcellcca34912003-10-26 16:38:16 +0000367 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000368 as significant digits (measured from the most signficant digit).
369 """
370 if round(second-first, places) == 0:
371 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000372 (msg or '%r == %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000373
Steve Purcell7e743842003-09-22 11:08:12 +0000374 # Synonyms for assertion methods
375
Steve Purcell15d89272001-04-12 09:05:01 +0000376 assertEqual = assertEquals = failUnlessEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000377
Steve Purcell15d89272001-04-12 09:05:01 +0000378 assertNotEqual = assertNotEquals = failIfEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000379
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000380 assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual
381
382 assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual
383
Steve Purcell15d89272001-04-12 09:05:01 +0000384 assertRaises = failUnlessRaises
385
Steve Purcell7e743842003-09-22 11:08:12 +0000386 assert_ = assertTrue = failUnless
387
388 assertFalse = failIf
Steve Purcell15d89272001-04-12 09:05:01 +0000389
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000390
Fred Drake02538202001-03-21 18:09:46 +0000391
392class TestSuite:
393 """A test suite is a composite test consisting of a number of TestCases.
394
395 For use, create an instance of TestSuite, then add test case instances.
396 When all tests have been added, the suite can be passed to a test
397 runner, such as TextTestRunner. It will run the individual test cases
398 in the order in which they were added, aggregating the results. When
399 subclassing, do not forget to call the base class constructor.
400 """
401 def __init__(self, tests=()):
402 self._tests = []
403 self.addTests(tests)
404
405 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000406 return "<%s tests=%s>" % (_strclass(self.__class__), self._tests)
Fred Drake02538202001-03-21 18:09:46 +0000407
408 __str__ = __repr__
409
Guido van Rossumd8faa362007-04-27 19:54:29 +0000410 def __eq__(self, other):
411 if type(self) is not type(other):
412 return False
413 return self._tests == other._tests
414
415 def __ne__(self, other):
416 return not self == other
417
Jim Fultonfafd8742004-08-28 15:22:12 +0000418 def __iter__(self):
419 return iter(self._tests)
420
Fred Drake02538202001-03-21 18:09:46 +0000421 def countTestCases(self):
422 cases = 0
423 for test in self._tests:
Steve Purcell7e743842003-09-22 11:08:12 +0000424 cases += test.countTestCases()
Fred Drake02538202001-03-21 18:09:46 +0000425 return cases
426
427 def addTest(self, test):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000428 # sanity checks
429 if not callable(test):
430 raise TypeError("the test to add must be callable")
431 if (isinstance(test, (type, types.ClassType)) and
432 issubclass(test, (TestCase, TestSuite))):
433 raise TypeError("TestCases and TestSuites must be instantiated "
434 "before passing them to addTest()")
Fred Drake02538202001-03-21 18:09:46 +0000435 self._tests.append(test)
436
437 def addTests(self, tests):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000438 if isinstance(tests, basestring):
439 raise TypeError("tests must be an iterable of tests, not a string")
Fred Drake02538202001-03-21 18:09:46 +0000440 for test in tests:
441 self.addTest(test)
442
443 def run(self, result):
Fred Drake02538202001-03-21 18:09:46 +0000444 for test in self._tests:
445 if result.shouldStop:
446 break
447 test(result)
448 return result
449
Raymond Hettinger664347b2004-12-04 21:21:53 +0000450 def __call__(self, *args, **kwds):
451 return self.run(*args, **kwds)
452
Fred Drake02538202001-03-21 18:09:46 +0000453 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000454 """Run the tests without collecting errors in a TestResult"""
Fred Drake02538202001-03-21 18:09:46 +0000455 for test in self._tests: test.debug()
Fred Drake02538202001-03-21 18:09:46 +0000456
457
458class FunctionTestCase(TestCase):
459 """A test case that wraps a test function.
460
461 This is useful for slipping pre-existing test functions into the
Guido van Rossumd8faa362007-04-27 19:54:29 +0000462 unittest framework. Optionally, set-up and tidy-up functions can be
Fred Drake02538202001-03-21 18:09:46 +0000463 supplied. As with TestCase, the tidy-up ('tearDown') function will
464 always be called if the set-up ('setUp') function ran successfully.
465 """
466
467 def __init__(self, testFunc, setUp=None, tearDown=None,
468 description=None):
469 TestCase.__init__(self)
470 self.__setUpFunc = setUp
471 self.__tearDownFunc = tearDown
472 self.__testFunc = testFunc
473 self.__description = description
474
475 def setUp(self):
476 if self.__setUpFunc is not None:
477 self.__setUpFunc()
478
479 def tearDown(self):
480 if self.__tearDownFunc is not None:
481 self.__tearDownFunc()
482
483 def runTest(self):
484 self.__testFunc()
485
486 def id(self):
487 return self.__testFunc.__name__
488
Guido van Rossumd8faa362007-04-27 19:54:29 +0000489 def __eq__(self, other):
490 if type(self) is not type(other):
491 return False
492
493 return self.__setUpFunc == other.__setUpFunc and \
494 self.__tearDownFunc == other.__tearDownFunc and \
495 self.__testFunc == other.__testFunc and \
496 self.__description == other.__description
497
498 def __ne__(self, other):
499 return not self == other
500
501 def __hash__(self):
502 return hash((type(self), self.__setUpFunc, self.__tearDownFunc,
503 self.__testFunc, self.__description))
504
Fred Drake02538202001-03-21 18:09:46 +0000505 def __str__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000506 return "%s (%s)" % (_strclass(self.__class__), self.__testFunc.__name__)
Fred Drake02538202001-03-21 18:09:46 +0000507
508 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000509 return "<%s testFunc=%s>" % (_strclass(self.__class__), self.__testFunc)
Fred Drake02538202001-03-21 18:09:46 +0000510
511 def shortDescription(self):
512 if self.__description is not None: return self.__description
513 doc = self.__testFunc.__doc__
Steve Purcell7e743842003-09-22 11:08:12 +0000514 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000515
516
517
518##############################################################################
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000519# Locating and loading tests
Fred Drake02538202001-03-21 18:09:46 +0000520##############################################################################
521
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000522class TestLoader:
523 """This class is responsible for loading tests according to various
Guido van Rossumd8faa362007-04-27 19:54:29 +0000524 criteria and returning them wrapped in a TestSuite
Fred Drake02538202001-03-21 18:09:46 +0000525 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000526 testMethodPrefix = 'test'
527 sortTestMethodsUsing = cmp
528 suiteClass = TestSuite
Fred Drake02538202001-03-21 18:09:46 +0000529
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000530 def loadTestsFromTestCase(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000531 """Return a suite of all tests cases contained in testCaseClass"""
Johannes Gijsbersd7b6ad42004-11-07 15:46:25 +0000532 if issubclass(testCaseClass, TestSuite):
533 raise TypeError("Test cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?")
Steve Purcell7e743842003-09-22 11:08:12 +0000534 testCaseNames = self.getTestCaseNames(testCaseClass)
535 if not testCaseNames and hasattr(testCaseClass, 'runTest'):
536 testCaseNames = ['runTest']
537 return self.suiteClass(map(testCaseClass, testCaseNames))
Fred Drake02538202001-03-21 18:09:46 +0000538
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000539 def loadTestsFromModule(self, module):
Steve Purcell15d89272001-04-12 09:05:01 +0000540 """Return a suite of all tests cases contained in the given module"""
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000541 tests = []
542 for name in dir(module):
543 obj = getattr(module, name)
Guido van Rossum67911372002-09-30 19:25:56 +0000544 if (isinstance(obj, (type, types.ClassType)) and
545 issubclass(obj, TestCase)):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000546 tests.append(self.loadTestsFromTestCase(obj))
547 return self.suiteClass(tests)
Fred Drake02538202001-03-21 18:09:46 +0000548
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000549 def loadTestsFromName(self, name, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000550 """Return a suite of all tests cases given a string specifier.
551
552 The name may resolve either to a module, a test case class, a
553 test method within a test case class, or a callable object which
554 returns a TestCase or TestSuite instance.
Tim Peters613b2222001-04-13 05:37:27 +0000555
Steve Purcell15d89272001-04-12 09:05:01 +0000556 The method optionally resolves the names relative to a given module.
557 """
Steve Purcell7e743842003-09-22 11:08:12 +0000558 parts = name.split('.')
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000559 if module is None:
Steve Purcell7e743842003-09-22 11:08:12 +0000560 parts_copy = parts[:]
561 while parts_copy:
562 try:
563 module = __import__('.'.join(parts_copy))
564 break
565 except ImportError:
566 del parts_copy[-1]
567 if not parts_copy: raise
Armin Rigo1b3c04b2003-10-24 17:15:29 +0000568 parts = parts[1:]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000569 obj = module
570 for part in parts:
Steve Purcell7e743842003-09-22 11:08:12 +0000571 parent, obj = obj, getattr(obj, part)
Fred Drake02538202001-03-21 18:09:46 +0000572
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000573 if type(obj) == types.ModuleType:
574 return self.loadTestsFromModule(obj)
Guido van Rossum67911372002-09-30 19:25:56 +0000575 elif (isinstance(obj, (type, types.ClassType)) and
Steve Purcell397b45d2003-10-26 10:41:03 +0000576 issubclass(obj, TestCase)):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000577 return self.loadTestsFromTestCase(obj)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000578 elif (type(obj) == types.UnboundMethodType and
579 isinstance(parent, (type, types.ClassType)) and
580 issubclass(parent, TestCase)):
581 return TestSuite([parent(obj.__name__)])
Steve Purcell397b45d2003-10-26 10:41:03 +0000582 elif isinstance(obj, TestSuite):
Steve Purcell7e743842003-09-22 11:08:12 +0000583 return obj
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000584 elif callable(obj):
585 test = obj()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000586 if isinstance(test, TestSuite):
587 return test
588 elif isinstance(test, TestCase):
589 return TestSuite([test])
590 else:
591 raise TypeError("calling %s returned %s, not a test" %
592 (obj, test))
Fred Drake02538202001-03-21 18:09:46 +0000593 else:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000594 raise TypeError("don't know how to make test from: %s" % obj)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000595
596 def loadTestsFromNames(self, names, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000597 """Return a suite of all tests cases found using the given sequence
598 of string specifiers. See 'loadTestsFromName()'.
599 """
Steve Purcell7e743842003-09-22 11:08:12 +0000600 suites = [self.loadTestsFromName(name, module) for name in names]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000601 return self.suiteClass(suites)
602
603 def getTestCaseNames(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000604 """Return a sorted sequence of method names found within testCaseClass
605 """
Steve Purcell7e743842003-09-22 11:08:12 +0000606 def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix):
Steve Purcell31982752003-09-23 08:41:53 +0000607 return attrname.startswith(prefix) and callable(getattr(testCaseClass, attrname))
Steve Purcell7e743842003-09-22 11:08:12 +0000608 testFnNames = filter(isTestMethod, dir(testCaseClass))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000609 if self.sortTestMethodsUsing:
610 testFnNames.sort(self.sortTestMethodsUsing)
611 return testFnNames
612
613
614
615defaultTestLoader = TestLoader()
616
617
618##############################################################################
619# Patches for old functions: these functions should be considered obsolete
620##############################################################################
621
622def _makeLoader(prefix, sortUsing, suiteClass=None):
623 loader = TestLoader()
624 loader.sortTestMethodsUsing = sortUsing
625 loader.testMethodPrefix = prefix
626 if suiteClass: loader.suiteClass = suiteClass
627 return loader
628
629def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp):
630 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
631
632def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
633 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
634
635def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
636 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module)
Fred Drake02538202001-03-21 18:09:46 +0000637
638
639##############################################################################
640# Text UI
641##############################################################################
642
643class _WritelnDecorator:
644 """Used to decorate file-like objects with a handy 'writeln' method"""
645 def __init__(self,stream):
646 self.stream = stream
Fred Drake02538202001-03-21 18:09:46 +0000647
648 def __getattr__(self, attr):
649 return getattr(self.stream,attr)
650
Raymond Hettinger91dd19d2003-09-13 02:58:00 +0000651 def writeln(self, arg=None):
652 if arg: self.write(arg)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000653 self.write('\n') # text-mode streams translate to \r\n if needed
Tim Petersa19a1682001-03-29 04:36:09 +0000654
Fred Drake02538202001-03-21 18:09:46 +0000655
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000656class _TextTestResult(TestResult):
Fred Drake02538202001-03-21 18:09:46 +0000657 """A test result class that can print formatted text results to a stream.
658
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000659 Used by TextTestRunner.
Fred Drake02538202001-03-21 18:09:46 +0000660 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000661 separator1 = '=' * 70
662 separator2 = '-' * 70
Fred Drake02538202001-03-21 18:09:46 +0000663
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000664 def __init__(self, stream, descriptions, verbosity):
Fred Drake02538202001-03-21 18:09:46 +0000665 TestResult.__init__(self)
666 self.stream = stream
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000667 self.showAll = verbosity > 1
668 self.dots = verbosity == 1
Fred Drake02538202001-03-21 18:09:46 +0000669 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000670
671 def getDescription(self, test):
672 if self.descriptions:
673 return test.shortDescription() or str(test)
674 else:
675 return str(test)
676
Fred Drake02538202001-03-21 18:09:46 +0000677 def startTest(self, test):
678 TestResult.startTest(self, test)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000679 if self.showAll:
680 self.stream.write(self.getDescription(test))
681 self.stream.write(" ... ")
Fred Drake02538202001-03-21 18:09:46 +0000682
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000683 def addSuccess(self, test):
684 TestResult.addSuccess(self, test)
685 if self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000686 self.stream.writeln("ok")
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000687 elif self.dots:
688 self.stream.write('.')
Fred Drake02538202001-03-21 18:09:46 +0000689
690 def addError(self, test, err):
691 TestResult.addError(self, test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000692 if self.showAll:
693 self.stream.writeln("ERROR")
694 elif self.dots:
695 self.stream.write('E')
Fred Drake02538202001-03-21 18:09:46 +0000696
697 def addFailure(self, test, err):
698 TestResult.addFailure(self, test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000699 if self.showAll:
700 self.stream.writeln("FAIL")
701 elif self.dots:
702 self.stream.write('F')
Fred Drake02538202001-03-21 18:09:46 +0000703
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000704 def printErrors(self):
705 if self.dots or self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000706 self.stream.writeln()
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000707 self.printErrorList('ERROR', self.errors)
708 self.printErrorList('FAIL', self.failures)
709
710 def printErrorList(self, flavour, errors):
711 for test, err in errors:
712 self.stream.writeln(self.separator1)
713 self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
714 self.stream.writeln(self.separator2)
Steve Purcell7b065702001-09-06 08:24:40 +0000715 self.stream.writeln("%s" % err)
Fred Drake02538202001-03-21 18:09:46 +0000716
717
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000718class TextTestRunner:
Fred Drake02538202001-03-21 18:09:46 +0000719 """A test runner class that displays results in textual form.
Tim Petersa19a1682001-03-29 04:36:09 +0000720
Fred Drake02538202001-03-21 18:09:46 +0000721 It prints out the names of tests as they are run, errors as they
722 occur, and a summary of the results at the end of the test run.
723 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000724 def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1):
Fred Drake02538202001-03-21 18:09:46 +0000725 self.stream = _WritelnDecorator(stream)
726 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000727 self.verbosity = verbosity
728
729 def _makeResult(self):
730 return _TextTestResult(self.stream, self.descriptions, self.verbosity)
Fred Drake02538202001-03-21 18:09:46 +0000731
732 def run(self, test):
733 "Run the given test case or test suite."
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000734 result = self._makeResult()
Fred Drake02538202001-03-21 18:09:46 +0000735 startTime = time.time()
736 test(result)
737 stopTime = time.time()
Steve Purcell397b45d2003-10-26 10:41:03 +0000738 timeTaken = stopTime - startTime
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000739 result.printErrors()
740 self.stream.writeln(result.separator2)
Fred Drake02538202001-03-21 18:09:46 +0000741 run = result.testsRun
742 self.stream.writeln("Ran %d test%s in %.3fs" %
Neal Norwitz76165042002-05-31 14:15:11 +0000743 (run, run != 1 and "s" or "", timeTaken))
Fred Drake02538202001-03-21 18:09:46 +0000744 self.stream.writeln()
745 if not result.wasSuccessful():
746 self.stream.write("FAILED (")
747 failed, errored = map(len, (result.failures, result.errors))
748 if failed:
749 self.stream.write("failures=%d" % failed)
750 if errored:
751 if failed: self.stream.write(", ")
752 self.stream.write("errors=%d" % errored)
753 self.stream.writeln(")")
754 else:
755 self.stream.writeln("OK")
756 return result
Tim Petersa19a1682001-03-29 04:36:09 +0000757
Fred Drake02538202001-03-21 18:09:46 +0000758
Fred Drake02538202001-03-21 18:09:46 +0000759
760##############################################################################
761# Facilities for running tests from the command line
762##############################################################################
763
764class TestProgram:
765 """A command-line program that runs a set of tests; this is primarily
766 for making test modules conveniently executable.
767 """
768 USAGE = """\
Steve Purcell17a781b2001-04-09 15:37:31 +0000769Usage: %(progName)s [options] [test] [...]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000770
771Options:
772 -h, --help Show this message
773 -v, --verbose Verbose output
774 -q, --quiet Minimal output
Fred Drake02538202001-03-21 18:09:46 +0000775
776Examples:
777 %(progName)s - run default set of tests
778 %(progName)s MyTestSuite - run suite 'MyTestSuite'
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000779 %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
780 %(progName)s MyTestCase - run all 'test*' test methods
Fred Drake02538202001-03-21 18:09:46 +0000781 in MyTestCase
782"""
783 def __init__(self, module='__main__', defaultTest=None,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000784 argv=None, testRunner=TextTestRunner,
785 testLoader=defaultTestLoader):
Fred Drake02538202001-03-21 18:09:46 +0000786 if type(module) == type(''):
787 self.module = __import__(module)
Steve Purcell7e743842003-09-22 11:08:12 +0000788 for part in module.split('.')[1:]:
Fred Drake02538202001-03-21 18:09:46 +0000789 self.module = getattr(self.module, part)
790 else:
791 self.module = module
792 if argv is None:
793 argv = sys.argv
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000794 self.verbosity = 1
Fred Drake02538202001-03-21 18:09:46 +0000795 self.defaultTest = defaultTest
796 self.testRunner = testRunner
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000797 self.testLoader = testLoader
Fred Drake02538202001-03-21 18:09:46 +0000798 self.progName = os.path.basename(argv[0])
799 self.parseArgs(argv)
Fred Drake02538202001-03-21 18:09:46 +0000800 self.runTests()
801
802 def usageExit(self, msg=None):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000803 if msg: print(msg)
804 print(self.USAGE % self.__dict__)
Fred Drake02538202001-03-21 18:09:46 +0000805 sys.exit(2)
806
807 def parseArgs(self, argv):
808 import getopt
809 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000810 options, args = getopt.getopt(argv[1:], 'hHvq',
811 ['help','verbose','quiet'])
Fred Drake02538202001-03-21 18:09:46 +0000812 for opt, value in options:
813 if opt in ('-h','-H','--help'):
814 self.usageExit()
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000815 if opt in ('-q','--quiet'):
816 self.verbosity = 0
817 if opt in ('-v','--verbose'):
818 self.verbosity = 2
Fred Drake02538202001-03-21 18:09:46 +0000819 if len(args) == 0 and self.defaultTest is None:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000820 self.test = self.testLoader.loadTestsFromModule(self.module)
821 return
Fred Drake02538202001-03-21 18:09:46 +0000822 if len(args) > 0:
823 self.testNames = args
824 else:
825 self.testNames = (self.defaultTest,)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000826 self.createTests()
Guido van Rossumb940e112007-01-10 16:19:56 +0000827 except getopt.error as msg:
Fred Drake02538202001-03-21 18:09:46 +0000828 self.usageExit(msg)
829
830 def createTests(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000831 self.test = self.testLoader.loadTestsFromNames(self.testNames,
832 self.module)
Fred Drake02538202001-03-21 18:09:46 +0000833
834 def runTests(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000835 if isinstance(self.testRunner, (type, types.ClassType)):
836 try:
837 testRunner = self.testRunner(verbosity=self.verbosity)
838 except TypeError:
839 # didn't accept the verbosity argument
840 testRunner = self.testRunner()
841 else:
842 # it is assumed to be a TestRunner instance
843 testRunner = self.testRunner
844 result = testRunner.run(self.test)
Tim Petersa19a1682001-03-29 04:36:09 +0000845 sys.exit(not result.wasSuccessful())
Fred Drake02538202001-03-21 18:09:46 +0000846
847main = TestProgram
848
849
850##############################################################################
851# Executing this module from the command line
852##############################################################################
853
854if __name__ == "__main__":
855 main(module=None)