blob: 46761dc01f05ed29da23c586f15b191a7c23a8ae [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
Benjamin Peterson4e4de332009-03-24 00:37:12 +000028 http://docs.python.org/library/unittest.html
Steve Purcell5ddd1a82001-03-22 08:45:36 +000029
Steve Purcell7e743842003-09-22 11:08:12 +000030Copyright (c) 1999-2003 Steve Purcell
Benjamin Peterson4e4de332009-03-24 00:37:12 +000031Copyright (c) 2003-2009 Python Software Foundation
Fred Drake02538202001-03-21 18:09:46 +000032This module is free software, and you may redistribute it and/or modify
33it under the same terms as Python itself, so long as this copyright message
34and disclaimer are retained in their original form.
35
36IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
37SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
38THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
39DAMAGE.
40
41THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
42LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
44AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
45SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Steve Purcell5ddd1a82001-03-22 08:45:36 +000046'''
Fred Drake02538202001-03-21 18:09:46 +000047
Fred Drake02538202001-03-21 18:09:46 +000048import time
49import sys
50import traceback
Fred Drake02538202001-03-21 18:09:46 +000051import os
Steve Purcell5ddd1a82001-03-22 08:45:36 +000052import types
Benjamin Peterson692428e2009-03-23 21:50:21 +000053import functools
Fred Drake02538202001-03-21 18:09:46 +000054
55##############################################################################
Steve Purcelld75e7e42003-09-15 11:01:21 +000056# Exported classes and functions
57##############################################################################
Benjamin Petersonc750d4d2009-03-24 00:39:24 +000058__all__ = ['TestResult', 'TestCase', 'TestSuite', 'ClassTestSuite',
59 'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
Benjamin Peterson03715482009-03-24 01:11:37 +000060 'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
Benjamin Petersonc750d4d2009-03-24 00:39:24 +000061 'expectedFailure']
Steve Purcelld75e7e42003-09-15 11:01:21 +000062
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##############################################################################
Steve Purcell7e743842003-09-22 11:08:12 +000070
Raymond Hettinger5930d8f2008-07-10 16:06:41 +000071def _CmpToKey(mycmp):
72 'Convert a cmp= function into a key= function'
73 class K(object):
74 def __init__(self, obj):
75 self.obj = obj
76 def __lt__(self, other):
77 return mycmp(self.obj, other.obj) == -1
78 return K
Steve Purcell7e743842003-09-22 11:08:12 +000079
80##############################################################################
Fred Drake02538202001-03-21 18:09:46 +000081# Test framework core
82##############################################################################
83
Steve Purcelldc391a62002-08-09 09:46:23 +000084def _strclass(cls):
85 return "%s.%s" % (cls.__module__, cls.__name__)
86
Benjamin Peterson692428e2009-03-23 21:50:21 +000087
88class SkipTest(Exception):
89 """
90 Raise this exception in a test to skip it.
91
92 Usually you can use TestResult.skip() or one of the skipping decorators
93 instead of raising this directly.
94 """
95 pass
96
97class _ExpectedFailure(Exception):
98 """
99 Raise this when a test is expected to fail.
100
101 This is an implementation detail.
102 """
103
104 def __init__(self, exc_info):
105 super(_ExpectedFailure, self).__init__()
106 self.exc_info = exc_info
107
108class _UnexpectedSuccess(Exception):
109 """
110 The test was supposed to fail, but it didn't!
111 """
112 pass
113
114def _id(obj):
115 return obj
116
117def skip(reason):
118 """
119 Unconditionally skip a test.
120 """
121 def decorator(test_item):
122 if isinstance(test_item, type) and issubclass(test_item, TestCase):
123 test_item.__unittest_skip__ = True
124 test_item.__unittest_skip_why__ = reason
125 return test_item
126 @functools.wraps(test_item)
127 def skip_wrapper(*args, **kwargs):
128 raise SkipTest(reason)
129 return skip_wrapper
130 return decorator
131
132def skipIf(condition, reason):
133 """
134 Skip a test if the condition is true.
135 """
136 if condition:
137 return skip(reason)
138 return _id
139
140def skipUnless(condition, reason):
141 """
142 Skip a test unless the condition is true.
143 """
144 if not condition:
145 return skip(reason)
146 return _id
147
148
149def expectedFailure(func):
150 @functools.wraps(func)
151 def wrapper(*args, **kwargs):
152 try:
153 func(*args, **kwargs)
154 except Exception:
155 raise _ExpectedFailure(sys.exc_info())
156 raise _UnexpectedSuccess
157 return wrapper
158
159
Steve Purcellb8d5f242003-12-06 13:03:13 +0000160__unittest = 1
161
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000162class TestResult(object):
Fred Drake02538202001-03-21 18:09:46 +0000163 """Holder for test result information.
164
165 Test results are automatically managed by the TestCase and TestSuite
166 classes, and do not need to be explicitly manipulated by writers of tests.
167
168 Each instance holds the total number of tests run, and collections of
169 failures and errors that occurred among those test runs. The collections
Steve Purcell7b065702001-09-06 08:24:40 +0000170 contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
Fred Drake656f9ec2001-09-06 19:13:14 +0000171 formatted traceback of the error that occurred.
Fred Drake02538202001-03-21 18:09:46 +0000172 """
173 def __init__(self):
174 self.failures = []
175 self.errors = []
176 self.testsRun = 0
Benjamin Peterson692428e2009-03-23 21:50:21 +0000177 self.skipped = []
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000178 self.expectedFailures = []
179 self.unexpectedSuccesses = []
Georg Brandl15c5ce92007-03-07 09:09:40 +0000180 self.shouldStop = False
Fred Drake02538202001-03-21 18:09:46 +0000181
182 def startTest(self, test):
183 "Called when the given test is about to be run"
184 self.testsRun = self.testsRun + 1
185
186 def stopTest(self, test):
187 "Called when the given test has been run"
188 pass
189
190 def addError(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000191 """Called when an error has occurred. 'err' is a tuple of values as
192 returned by sys.exc_info().
193 """
Steve Purcellb8d5f242003-12-06 13:03:13 +0000194 self.errors.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000195
196 def addFailure(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000197 """Called when an error has occurred. 'err' is a tuple of values as
198 returned by sys.exc_info()."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000199 self.failures.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000200
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000201 def addSuccess(self, test):
202 "Called when a test has completed successfully"
203 pass
204
Benjamin Peterson692428e2009-03-23 21:50:21 +0000205 def addSkip(self, test, reason):
206 """Called when a test is skipped."""
207 self.skipped.append((test, reason))
208
209 def addExpectedFailure(self, test, err):
210 """Called when an expected failure/error occured."""
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000211 self.expectedFailures.append(
Benjamin Peterson692428e2009-03-23 21:50:21 +0000212 (test, self._exc_info_to_string(err, test)))
213
214 def addUnexpectedSuccess(self, test):
215 """Called when a test was expected to fail, but succeed."""
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000216 self.unexpectedSuccesses.append(test)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000217
Fred Drake02538202001-03-21 18:09:46 +0000218 def wasSuccessful(self):
219 "Tells whether or not this result was a success"
220 return len(self.failures) == len(self.errors) == 0
221
222 def stop(self):
223 "Indicates that the tests should be aborted"
Steve Purcell7e743842003-09-22 11:08:12 +0000224 self.shouldStop = True
Tim Petersa19a1682001-03-29 04:36:09 +0000225
Steve Purcellb8d5f242003-12-06 13:03:13 +0000226 def _exc_info_to_string(self, err, test):
Steve Purcell7b065702001-09-06 08:24:40 +0000227 """Converts a sys.exc_info()-style tuple of values into a string."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000228 exctype, value, tb = err
229 # Skip test runner traceback levels
230 while tb and self._is_relevant_tb_level(tb):
231 tb = tb.tb_next
232 if exctype is test.failureException:
233 # Skip assert*() traceback levels
234 length = self._count_relevant_tb_levels(tb)
235 return ''.join(traceback.format_exception(exctype, value, tb, length))
236 return ''.join(traceback.format_exception(exctype, value, tb))
237
238 def _is_relevant_tb_level(self, tb):
Georg Brandl56af5fc2008-07-18 19:30:10 +0000239 return '__unittest' in tb.tb_frame.f_globals
Steve Purcellb8d5f242003-12-06 13:03:13 +0000240
241 def _count_relevant_tb_levels(self, tb):
242 length = 0
243 while tb and not self._is_relevant_tb_level(tb):
244 length += 1
245 tb = tb.tb_next
246 return length
Steve Purcell7b065702001-09-06 08:24:40 +0000247
Fred Drake02538202001-03-21 18:09:46 +0000248 def __repr__(self):
249 return "<%s run=%i errors=%i failures=%i>" % \
Steve Purcelldc391a62002-08-09 09:46:23 +0000250 (_strclass(self.__class__), self.testsRun, len(self.errors),
Fred Drake02538202001-03-21 18:09:46 +0000251 len(self.failures))
252
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000253
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000254class AssertRaisesContext(object):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000255
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000256 def __init__(self, expected, test_case):
257 self.expected = expected
258 self.failureException = test_case.failureException
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000259
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000260 def __enter__(self):
261 pass
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000262
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000263 def __exit__(self, exc_type, exc_value, traceback):
264 if exc_type is None:
265 try:
266 exc_name = self.expected.__name__
267 except AttributeError:
268 exc_name = str(self.expected)
269 raise self.failureException(
270 "{0} not raised".format(exc_name))
271 if issubclass(exc_type, self.expected):
272 return True
273 # Let unexpected exceptions skip through
274 return False
275
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000276
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000277class TestCase(object):
Fred Drake02538202001-03-21 18:09:46 +0000278 """A class whose instances are single test cases.
279
Fred Drake02538202001-03-21 18:09:46 +0000280 By default, the test code itself should be placed in a method named
281 'runTest'.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000282
Tim Petersa19a1682001-03-29 04:36:09 +0000283 If the fixture may be used for many test cases, create as
Fred Drake02538202001-03-21 18:09:46 +0000284 many test methods as are needed. When instantiating such a TestCase
285 subclass, specify in the constructor arguments the name of the test method
286 that the instance is to execute.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000287
Tim Petersa19a1682001-03-29 04:36:09 +0000288 Test authors should subclass TestCase for their own tests. Construction
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000289 and deconstruction of the test's environment ('fixture') can be
290 implemented by overriding the 'setUp' and 'tearDown' methods respectively.
291
292 If it is necessary to override the __init__ method, the base class
293 __init__ method must always be called. It is important that subclasses
294 should not change the signature of their __init__ method, since instances
295 of the classes are instantiated automatically by parts of the framework
296 in order to be run.
Fred Drake02538202001-03-21 18:09:46 +0000297 """
Steve Purcell15d89272001-04-12 09:05:01 +0000298
299 # This attribute determines which exception will be raised when
300 # the instance's assertion methods fail; test methods raising this
301 # exception will be deemed to have 'failed' rather than 'errored'
302
303 failureException = AssertionError
304
Fred Drake02538202001-03-21 18:09:46 +0000305 def __init__(self, methodName='runTest'):
306 """Create an instance of the class that will use the named test
307 method when executed. Raises a ValueError if the instance does
308 not have a method with the specified name.
309 """
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000310 self._testMethodName = methodName
Fred Drake02538202001-03-21 18:09:46 +0000311 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000312 testMethod = getattr(self, methodName)
Fred Drake02538202001-03-21 18:09:46 +0000313 except AttributeError:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000314 raise ValueError("no such test method in %s: %s" % \
315 (self.__class__, methodName))
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000316 self._testMethodDoc = testMethod.__doc__
Fred Drake02538202001-03-21 18:09:46 +0000317
318 def setUp(self):
319 "Hook method for setting up the test fixture before exercising it."
320 pass
321
322 def tearDown(self):
323 "Hook method for deconstructing the test fixture after testing it."
324 pass
325
326 def countTestCases(self):
327 return 1
328
329 def defaultTestResult(self):
330 return TestResult()
331
332 def shortDescription(self):
333 """Returns a one-line description of the test, or None if no
334 description has been provided.
335
336 The default implementation of this method returns the first line of
337 the specified test method's docstring.
338 """
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000339 doc = self._testMethodDoc
Steve Purcell7e743842003-09-22 11:08:12 +0000340 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000341
342 def id(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000343 return "%s.%s" % (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000344
Georg Brandl15c5ce92007-03-07 09:09:40 +0000345 def __eq__(self, other):
346 if type(self) is not type(other):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000347 return NotImplemented
Georg Brandl15c5ce92007-03-07 09:09:40 +0000348
349 return self._testMethodName == other._testMethodName
350
351 def __ne__(self, other):
352 return not self == other
353
354 def __hash__(self):
Collin Winter9453e5d2007-03-09 23:30:39 +0000355 return hash((type(self), self._testMethodName))
Georg Brandl15c5ce92007-03-07 09:09:40 +0000356
Fred Drake02538202001-03-21 18:09:46 +0000357 def __str__(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000358 return "%s (%s)" % (self._testMethodName, _strclass(self.__class__))
Fred Drake02538202001-03-21 18:09:46 +0000359
360 def __repr__(self):
361 return "<%s testMethod=%s>" % \
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000362 (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000363
364 def run(self, result=None):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000365 if result is None:
366 result = self.defaultTestResult()
Fred Drake02538202001-03-21 18:09:46 +0000367 result.startTest(self)
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000368 testMethod = getattr(self, self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000369 try:
370 try:
371 self.setUp()
Benjamin Peterson692428e2009-03-23 21:50:21 +0000372 except SkipTest as e:
373 result.addSkip(self, str(e))
374 return
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000375 except Exception:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000376 result.addError(self, self._exc_info())
Fred Drake02538202001-03-21 18:09:46 +0000377 return
378
Benjamin Peterson692428e2009-03-23 21:50:21 +0000379 success = False
Fred Drake02538202001-03-21 18:09:46 +0000380 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000381 testMethod()
Skip Montanaroae5c37b2003-07-13 15:18:12 +0000382 except self.failureException:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000383 result.addFailure(self, self._exc_info())
Benjamin Peterson692428e2009-03-23 21:50:21 +0000384 except _ExpectedFailure as e:
385 result.addExpectedFailure(self, e.exc_info)
386 except _UnexpectedSuccess:
387 result.addUnexpectedSuccess(self)
388 except SkipTest as e:
389 result.addSkip(self, str(e))
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000390 except Exception:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000391 result.addError(self, self._exc_info())
Benjamin Peterson692428e2009-03-23 21:50:21 +0000392 else:
393 success = True
Fred Drake02538202001-03-21 18:09:46 +0000394
395 try:
396 self.tearDown()
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000397 except Exception:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000398 result.addError(self, self._exc_info())
Benjamin Peterson692428e2009-03-23 21:50:21 +0000399 success = False
400 if success:
401 result.addSuccess(self)
Fred Drake02538202001-03-21 18:09:46 +0000402 finally:
403 result.stopTest(self)
404
Raymond Hettinger664347b2004-12-04 21:21:53 +0000405 def __call__(self, *args, **kwds):
406 return self.run(*args, **kwds)
Steve Purcell7e743842003-09-22 11:08:12 +0000407
Fred Drake02538202001-03-21 18:09:46 +0000408 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000409 """Run the test without collecting errors in a TestResult"""
Fred Drake02538202001-03-21 18:09:46 +0000410 self.setUp()
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000411 getattr(self, self._testMethodName)()
Fred Drake02538202001-03-21 18:09:46 +0000412 self.tearDown()
413
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000414 def _exc_info(self):
Steve Purcell15d89272001-04-12 09:05:01 +0000415 """Return a version of sys.exc_info() with the traceback frame
416 minimised; usually the top level of the traceback frame is not
417 needed.
Fred Drake02538202001-03-21 18:09:46 +0000418 """
Georg Brandl15c5ce92007-03-07 09:09:40 +0000419 return sys.exc_info()
Fred Drake02538202001-03-21 18:09:46 +0000420
Benjamin Peterson692428e2009-03-23 21:50:21 +0000421 def skip(self, reason):
422 """Skip this test."""
423 raise SkipTest(reason)
424
Steve Purcell15d89272001-04-12 09:05:01 +0000425 def fail(self, msg=None):
426 """Fail immediately, with the given message."""
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000427 raise self.failureException(msg)
Fred Drake02538202001-03-21 18:09:46 +0000428
429 def failIf(self, expr, msg=None):
430 "Fail the test if the expression is true."
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000431 if expr:
432 raise self.failureException(msg)
Fred Drake02538202001-03-21 18:09:46 +0000433
Steve Purcell15d89272001-04-12 09:05:01 +0000434 def failUnless(self, expr, msg=None):
435 """Fail the test unless the expression is true."""
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000436 if not expr:
437 raise self.failureException(msg)
Steve Purcell15d89272001-04-12 09:05:01 +0000438
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000439 def failUnlessRaises(self, excClass, callableObj=None, *args, **kwargs):
Steve Purcell15d89272001-04-12 09:05:01 +0000440 """Fail unless an exception of class excClass is thrown
Fred Drake02538202001-03-21 18:09:46 +0000441 by callableObj when invoked with arguments args and keyword
442 arguments kwargs. If a different type of exception is
443 thrown, it will not be caught, and the test case will be
444 deemed to have suffered an error, exactly as for an
445 unexpected exception.
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000446
447 If called with callableObj omitted or None, will return a
448 context object used like this::
449
450 with self.failUnlessRaises(some_error_class):
451 do_something()
Fred Drake02538202001-03-21 18:09:46 +0000452 """
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000453 context = AssertRaisesContext(excClass, self)
454 if callableObj is None:
455 return context
456 with context:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000457 callableObj(*args, **kwargs)
Fred Drake02538202001-03-21 18:09:46 +0000458
Steve Purcell15d89272001-04-12 09:05:01 +0000459 def failUnlessEqual(self, first, second, msg=None):
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000460 """Fail if the two objects are unequal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000461 operator.
462 """
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000463 if not first == second:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000464 raise self.failureException(msg or '%r != %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000465
Steve Purcell15d89272001-04-12 09:05:01 +0000466 def failIfEqual(self, first, second, msg=None):
467 """Fail if the two objects are equal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000468 operator.
469 """
Steve Purcell15d89272001-04-12 09:05:01 +0000470 if first == second:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000471 raise self.failureException(msg or '%r == %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000472
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000473 def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
474 """Fail if the two objects are unequal as determined by their
475 difference rounded to the given number of decimal places
476 (default 7) and comparing to zero.
477
Steve Purcell397b45d2003-10-26 10:41:03 +0000478 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000479 as significant digits (measured from the most signficant digit).
480 """
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000481 if round(abs(second-first), places) != 0:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000482 raise self.failureException(
483 msg or '%r != %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000484
485 def failIfAlmostEqual(self, first, second, places=7, msg=None):
486 """Fail if the two objects are equal as determined by their
487 difference rounded to the given number of decimal places
488 (default 7) and comparing to zero.
489
Steve Purcellcca34912003-10-26 16:38:16 +0000490 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000491 as significant digits (measured from the most signficant digit).
492 """
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000493 if round(abs(second-first), places) == 0:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000494 raise self.failureException(
495 msg or '%r == %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000496
Steve Purcell7e743842003-09-22 11:08:12 +0000497 # Synonyms for assertion methods
498
Steve Purcell15d89272001-04-12 09:05:01 +0000499 assertEqual = assertEquals = failUnlessEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000500
Steve Purcell15d89272001-04-12 09:05:01 +0000501 assertNotEqual = assertNotEquals = failIfEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000502
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000503 assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual
504
505 assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual
506
Steve Purcell15d89272001-04-12 09:05:01 +0000507 assertRaises = failUnlessRaises
508
Steve Purcell7e743842003-09-22 11:08:12 +0000509 assert_ = assertTrue = failUnless
510
511 assertFalse = failIf
Steve Purcell15d89272001-04-12 09:05:01 +0000512
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000513
Fred Drake02538202001-03-21 18:09:46 +0000514
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000515class TestSuite(object):
Fred Drake02538202001-03-21 18:09:46 +0000516 """A test suite is a composite test consisting of a number of TestCases.
517
518 For use, create an instance of TestSuite, then add test case instances.
519 When all tests have been added, the suite can be passed to a test
520 runner, such as TextTestRunner. It will run the individual test cases
521 in the order in which they were added, aggregating the results. When
522 subclassing, do not forget to call the base class constructor.
523 """
524 def __init__(self, tests=()):
525 self._tests = []
526 self.addTests(tests)
527
528 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000529 return "<%s tests=%s>" % (_strclass(self.__class__), self._tests)
Fred Drake02538202001-03-21 18:09:46 +0000530
Georg Brandl15c5ce92007-03-07 09:09:40 +0000531 def __eq__(self, other):
Benjamin Peterson692428e2009-03-23 21:50:21 +0000532 if not isinstance(other, self.__class__):
533 return NotImplemented
Georg Brandl15c5ce92007-03-07 09:09:40 +0000534 return self._tests == other._tests
535
536 def __ne__(self, other):
537 return not self == other
538
Nick Coghlan48361f52008-08-11 15:45:58 +0000539 # Can't guarantee hash invariant, so flag as unhashable
540 __hash__ = None
541
Jim Fultonfafd8742004-08-28 15:22:12 +0000542 def __iter__(self):
543 return iter(self._tests)
544
Fred Drake02538202001-03-21 18:09:46 +0000545 def countTestCases(self):
546 cases = 0
547 for test in self._tests:
Steve Purcell7e743842003-09-22 11:08:12 +0000548 cases += test.countTestCases()
Fred Drake02538202001-03-21 18:09:46 +0000549 return cases
550
551 def addTest(self, test):
Georg Brandld9e50262007-03-07 11:54:49 +0000552 # sanity checks
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000553 if not hasattr(test, '__call__'):
Georg Brandld9e50262007-03-07 11:54:49 +0000554 raise TypeError("the test to add must be callable")
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000555 if isinstance(test, type) and issubclass(test, (TestCase, TestSuite)):
Georg Brandld9e50262007-03-07 11:54:49 +0000556 raise TypeError("TestCases and TestSuites must be instantiated "
557 "before passing them to addTest()")
Fred Drake02538202001-03-21 18:09:46 +0000558 self._tests.append(test)
559
560 def addTests(self, tests):
Georg Brandld9e50262007-03-07 11:54:49 +0000561 if isinstance(tests, basestring):
562 raise TypeError("tests must be an iterable of tests, not a string")
Fred Drake02538202001-03-21 18:09:46 +0000563 for test in tests:
564 self.addTest(test)
565
566 def run(self, result):
Fred Drake02538202001-03-21 18:09:46 +0000567 for test in self._tests:
568 if result.shouldStop:
569 break
570 test(result)
571 return result
572
Raymond Hettinger664347b2004-12-04 21:21:53 +0000573 def __call__(self, *args, **kwds):
574 return self.run(*args, **kwds)
575
Fred Drake02538202001-03-21 18:09:46 +0000576 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000577 """Run the tests without collecting errors in a TestResult"""
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000578 for test in self._tests:
579 test.debug()
Fred Drake02538202001-03-21 18:09:46 +0000580
581
Benjamin Peterson692428e2009-03-23 21:50:21 +0000582class ClassTestSuite(TestSuite):
583 """
584 Suite of tests derived from a single TestCase class.
585 """
586
587 def __init__(self, tests, class_collected_from):
588 super(ClassTestSuite, self).__init__(tests)
589 self.collected_from = class_collected_from
590
591 def id(self):
592 module = getattr(self.collected_from, "__module__", None)
593 if module is not None:
594 return "{0}.{1}".format(module, self.collected_from.__name__)
595 return self.collected_from.__name__
596
597 def run(self, result):
598 if getattr(self.collected_from, "__unittest_skip__", False):
599 # ClassTestSuite result pretends to be a TestCase enough to be
600 # reported.
601 result.startTest(self)
602 try:
603 result.addSkip(self, self.collected_from.__unittest_skip_why__)
604 finally:
605 result.stopTest(self)
606 else:
607 result = super(ClassTestSuite, self).run(result)
608 return result
609
610 shortDescription = id
611
612
Fred Drake02538202001-03-21 18:09:46 +0000613class FunctionTestCase(TestCase):
614 """A test case that wraps a test function.
615
616 This is useful for slipping pre-existing test functions into the
Georg Brandl15c5ce92007-03-07 09:09:40 +0000617 unittest framework. Optionally, set-up and tidy-up functions can be
Fred Drake02538202001-03-21 18:09:46 +0000618 supplied. As with TestCase, the tidy-up ('tearDown') function will
619 always be called if the set-up ('setUp') function ran successfully.
620 """
621
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000622 def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
623 super(FunctionTestCase, self).__init__()
Fred Drake02538202001-03-21 18:09:46 +0000624 self.__setUpFunc = setUp
625 self.__tearDownFunc = tearDown
626 self.__testFunc = testFunc
627 self.__description = description
628
629 def setUp(self):
630 if self.__setUpFunc is not None:
631 self.__setUpFunc()
632
633 def tearDown(self):
634 if self.__tearDownFunc is not None:
635 self.__tearDownFunc()
636
637 def runTest(self):
638 self.__testFunc()
639
640 def id(self):
641 return self.__testFunc.__name__
642
Georg Brandl15c5ce92007-03-07 09:09:40 +0000643 def __eq__(self, other):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000644 if not isinstance(other, self.__class__):
645 return NotImplemented
Georg Brandl15c5ce92007-03-07 09:09:40 +0000646
647 return self.__setUpFunc == other.__setUpFunc and \
648 self.__tearDownFunc == other.__tearDownFunc and \
649 self.__testFunc == other.__testFunc and \
650 self.__description == other.__description
651
652 def __ne__(self, other):
653 return not self == other
654
655 def __hash__(self):
Collin Winter9453e5d2007-03-09 23:30:39 +0000656 return hash((type(self), self.__setUpFunc, self.__tearDownFunc,
657 self.__testFunc, self.__description))
Georg Brandl15c5ce92007-03-07 09:09:40 +0000658
Fred Drake02538202001-03-21 18:09:46 +0000659 def __str__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000660 return "%s (%s)" % (_strclass(self.__class__), self.__testFunc.__name__)
Fred Drake02538202001-03-21 18:09:46 +0000661
662 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000663 return "<%s testFunc=%s>" % (_strclass(self.__class__), self.__testFunc)
Fred Drake02538202001-03-21 18:09:46 +0000664
665 def shortDescription(self):
666 if self.__description is not None: return self.__description
667 doc = self.__testFunc.__doc__
Steve Purcell7e743842003-09-22 11:08:12 +0000668 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000669
670
671
672##############################################################################
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000673# Locating and loading tests
Fred Drake02538202001-03-21 18:09:46 +0000674##############################################################################
675
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000676class TestLoader(object):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000677 """
678 This class is responsible for loading tests according to various criteria
679 and returning them wrapped in a TestSuite
Fred Drake02538202001-03-21 18:09:46 +0000680 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000681 testMethodPrefix = 'test'
682 sortTestMethodsUsing = cmp
683 suiteClass = TestSuite
Benjamin Peterson692428e2009-03-23 21:50:21 +0000684 classSuiteClass = ClassTestSuite
Fred Drake02538202001-03-21 18:09:46 +0000685
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000686 def loadTestsFromTestCase(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000687 """Return a suite of all tests cases contained in testCaseClass"""
Johannes Gijsbersd7b6ad42004-11-07 15:46:25 +0000688 if issubclass(testCaseClass, TestSuite):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000689 raise TypeError("Test cases should not be derived from TestSuite." \
690 " Maybe you meant to derive from TestCase?")
Steve Purcell7e743842003-09-22 11:08:12 +0000691 testCaseNames = self.getTestCaseNames(testCaseClass)
692 if not testCaseNames and hasattr(testCaseClass, 'runTest'):
693 testCaseNames = ['runTest']
Benjamin Peterson692428e2009-03-23 21:50:21 +0000694 suite = self.classSuiteClass(map(testCaseClass, testCaseNames),
695 testCaseClass)
696 return suite
Fred Drake02538202001-03-21 18:09:46 +0000697
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000698 def loadTestsFromModule(self, module):
Steve Purcell15d89272001-04-12 09:05:01 +0000699 """Return a suite of all tests cases contained in the given module"""
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000700 tests = []
701 for name in dir(module):
702 obj = getattr(module, name)
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000703 if isinstance(obj, type) and issubclass(obj, TestCase):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000704 tests.append(self.loadTestsFromTestCase(obj))
705 return self.suiteClass(tests)
Fred Drake02538202001-03-21 18:09:46 +0000706
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000707 def loadTestsFromName(self, name, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000708 """Return a suite of all tests cases given a string specifier.
709
710 The name may resolve either to a module, a test case class, a
711 test method within a test case class, or a callable object which
712 returns a TestCase or TestSuite instance.
Tim Peters613b2222001-04-13 05:37:27 +0000713
Steve Purcell15d89272001-04-12 09:05:01 +0000714 The method optionally resolves the names relative to a given module.
715 """
Steve Purcell7e743842003-09-22 11:08:12 +0000716 parts = name.split('.')
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000717 if module is None:
Steve Purcell7e743842003-09-22 11:08:12 +0000718 parts_copy = parts[:]
719 while parts_copy:
720 try:
721 module = __import__('.'.join(parts_copy))
722 break
723 except ImportError:
724 del parts_copy[-1]
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000725 if not parts_copy:
726 raise
Armin Rigo1b3c04b2003-10-24 17:15:29 +0000727 parts = parts[1:]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000728 obj = module
729 for part in parts:
Steve Purcell7e743842003-09-22 11:08:12 +0000730 parent, obj = obj, getattr(obj, part)
Fred Drake02538202001-03-21 18:09:46 +0000731
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000732 if isinstance(obj, types.ModuleType):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000733 return self.loadTestsFromModule(obj)
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000734 elif isinstance(obj, type) and issubclass(obj, TestCase):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000735 return self.loadTestsFromTestCase(obj)
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000736 elif (isinstance(obj, types.UnboundMethodType) and
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000737 isinstance(parent, type) and
Georg Brandl15c5ce92007-03-07 09:09:40 +0000738 issubclass(parent, TestCase)):
739 return TestSuite([parent(obj.__name__)])
Steve Purcell397b45d2003-10-26 10:41:03 +0000740 elif isinstance(obj, TestSuite):
Steve Purcell7e743842003-09-22 11:08:12 +0000741 return obj
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000742 elif hasattr(obj, '__call__'):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000743 test = obj()
Georg Brandl15c5ce92007-03-07 09:09:40 +0000744 if isinstance(test, TestSuite):
745 return test
746 elif isinstance(test, TestCase):
747 return TestSuite([test])
748 else:
749 raise TypeError("calling %s returned %s, not a test" %
750 (obj, test))
Fred Drake02538202001-03-21 18:09:46 +0000751 else:
Georg Brandl15c5ce92007-03-07 09:09:40 +0000752 raise TypeError("don't know how to make test from: %s" % obj)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000753
754 def loadTestsFromNames(self, names, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000755 """Return a suite of all tests cases found using the given sequence
756 of string specifiers. See 'loadTestsFromName()'.
757 """
Steve Purcell7e743842003-09-22 11:08:12 +0000758 suites = [self.loadTestsFromName(name, module) for name in names]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000759 return self.suiteClass(suites)
760
761 def getTestCaseNames(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000762 """Return a sorted sequence of method names found within testCaseClass
763 """
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000764 def isTestMethod(attrname, testCaseClass=testCaseClass,
765 prefix=self.testMethodPrefix):
766 return attrname.startswith(prefix) and \
767 hasattr(getattr(testCaseClass, attrname), '__call__')
Steve Purcell7e743842003-09-22 11:08:12 +0000768 testFnNames = filter(isTestMethod, dir(testCaseClass))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000769 if self.sortTestMethodsUsing:
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000770 testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000771 return testFnNames
772
773
774
775defaultTestLoader = TestLoader()
776
777
778##############################################################################
779# Patches for old functions: these functions should be considered obsolete
780##############################################################################
781
782def _makeLoader(prefix, sortUsing, suiteClass=None):
783 loader = TestLoader()
784 loader.sortTestMethodsUsing = sortUsing
785 loader.testMethodPrefix = prefix
786 if suiteClass: loader.suiteClass = suiteClass
787 return loader
788
789def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp):
790 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
791
792def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
793 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
794
795def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
796 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module)
Fred Drake02538202001-03-21 18:09:46 +0000797
798
799##############################################################################
800# Text UI
801##############################################################################
802
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000803class _WritelnDecorator(object):
Fred Drake02538202001-03-21 18:09:46 +0000804 """Used to decorate file-like objects with a handy 'writeln' method"""
805 def __init__(self,stream):
806 self.stream = stream
Fred Drake02538202001-03-21 18:09:46 +0000807
808 def __getattr__(self, attr):
809 return getattr(self.stream,attr)
810
Raymond Hettinger91dd19d2003-09-13 02:58:00 +0000811 def writeln(self, arg=None):
812 if arg: self.write(arg)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000813 self.write('\n') # text-mode streams translate to \r\n if needed
Tim Petersa19a1682001-03-29 04:36:09 +0000814
Fred Drake02538202001-03-21 18:09:46 +0000815
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000816class _TextTestResult(TestResult):
Fred Drake02538202001-03-21 18:09:46 +0000817 """A test result class that can print formatted text results to a stream.
818
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000819 Used by TextTestRunner.
Fred Drake02538202001-03-21 18:09:46 +0000820 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000821 separator1 = '=' * 70
822 separator2 = '-' * 70
Fred Drake02538202001-03-21 18:09:46 +0000823
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000824 def __init__(self, stream, descriptions, verbosity):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000825 super(_TextTestResult, self).__init__()
Fred Drake02538202001-03-21 18:09:46 +0000826 self.stream = stream
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000827 self.showAll = verbosity > 1
828 self.dots = verbosity == 1
Fred Drake02538202001-03-21 18:09:46 +0000829 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000830
831 def getDescription(self, test):
832 if self.descriptions:
833 return test.shortDescription() or str(test)
834 else:
835 return str(test)
836
Fred Drake02538202001-03-21 18:09:46 +0000837 def startTest(self, test):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000838 super(_TextTestResult, self).startTest(test)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000839 if self.showAll:
840 self.stream.write(self.getDescription(test))
841 self.stream.write(" ... ")
Georg Brandld0632402008-05-11 15:17:41 +0000842 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000843
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000844 def addSuccess(self, test):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000845 super(_TextTestResult, self).addSuccess(test)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000846 if self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000847 self.stream.writeln("ok")
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000848 elif self.dots:
849 self.stream.write('.')
Georg Brandld0632402008-05-11 15:17:41 +0000850 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000851
852 def addError(self, test, err):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000853 super(_TextTestResult, self).addError(test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000854 if self.showAll:
855 self.stream.writeln("ERROR")
856 elif self.dots:
857 self.stream.write('E')
Georg Brandld0632402008-05-11 15:17:41 +0000858 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000859
860 def addFailure(self, test, err):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000861 super(_TextTestResult, self).addFailure(test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000862 if self.showAll:
863 self.stream.writeln("FAIL")
864 elif self.dots:
865 self.stream.write('F')
Georg Brandld0632402008-05-11 15:17:41 +0000866 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000867
Benjamin Peterson692428e2009-03-23 21:50:21 +0000868 def addSkip(self, test, reason):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000869 super(_TextTestResult, self).addSkip(test, reason)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000870 if self.showAll:
871 self.stream.writeln("skipped {0!r}".format(reason))
872 elif self.dots:
873 self.stream.write("s")
874 self.stream.flush()
875
876 def addExpectedFailure(self, test, err):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000877 super(_TextTestResult, self).addExpectedFailure(test, err)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000878 if self.showAll:
879 self.stream.writeln("expected failure")
880 elif self.dots:
881 self.stream.write(".")
882 self.stream.flush()
883
884 def addUnexpectedSuccess(self, test):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000885 super(_TextTestResult, self).addUnexpectedSuccess(test)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000886 if self.showAll:
887 self.stream.writeln("unexpected success")
888 elif self.dots:
889 self.stream.write(".")
890 self.stream.flush()
891
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000892 def printErrors(self):
893 if self.dots or self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000894 self.stream.writeln()
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000895 self.printErrorList('ERROR', self.errors)
896 self.printErrorList('FAIL', self.failures)
897
898 def printErrorList(self, flavour, errors):
899 for test, err in errors:
900 self.stream.writeln(self.separator1)
901 self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
902 self.stream.writeln(self.separator2)
Steve Purcell7b065702001-09-06 08:24:40 +0000903 self.stream.writeln("%s" % err)
Fred Drake02538202001-03-21 18:09:46 +0000904
905
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000906class TextTestRunner(object):
Fred Drake02538202001-03-21 18:09:46 +0000907 """A test runner class that displays results in textual form.
Tim Petersa19a1682001-03-29 04:36:09 +0000908
Fred Drake02538202001-03-21 18:09:46 +0000909 It prints out the names of tests as they are run, errors as they
910 occur, and a summary of the results at the end of the test run.
911 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000912 def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1):
Fred Drake02538202001-03-21 18:09:46 +0000913 self.stream = _WritelnDecorator(stream)
914 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000915 self.verbosity = verbosity
916
917 def _makeResult(self):
918 return _TextTestResult(self.stream, self.descriptions, self.verbosity)
Fred Drake02538202001-03-21 18:09:46 +0000919
920 def run(self, test):
921 "Run the given test case or test suite."
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000922 result = self._makeResult()
Fred Drake02538202001-03-21 18:09:46 +0000923 startTime = time.time()
924 test(result)
925 stopTime = time.time()
Steve Purcell397b45d2003-10-26 10:41:03 +0000926 timeTaken = stopTime - startTime
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000927 result.printErrors()
928 self.stream.writeln(result.separator2)
Fred Drake02538202001-03-21 18:09:46 +0000929 run = result.testsRun
930 self.stream.writeln("Ran %d test%s in %.3fs" %
Neal Norwitz76165042002-05-31 14:15:11 +0000931 (run, run != 1 and "s" or "", timeTaken))
Fred Drake02538202001-03-21 18:09:46 +0000932 self.stream.writeln()
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000933 results = map(len, (result.expectedFailures,
934 result.unexpectedSuccesses,
Benjamin Peterson692428e2009-03-23 21:50:21 +0000935 result.skipped))
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000936 expectedFails, unexpectedSuccesses, skipped = results
Benjamin Peterson692428e2009-03-23 21:50:21 +0000937 infos = []
Fred Drake02538202001-03-21 18:09:46 +0000938 if not result.wasSuccessful():
Benjamin Peterson692428e2009-03-23 21:50:21 +0000939 self.stream.write("FAILED")
Fred Drake02538202001-03-21 18:09:46 +0000940 failed, errored = map(len, (result.failures, result.errors))
941 if failed:
Benjamin Peterson692428e2009-03-23 21:50:21 +0000942 infos.append("failures=%d" % failed)
Fred Drake02538202001-03-21 18:09:46 +0000943 if errored:
Benjamin Peterson692428e2009-03-23 21:50:21 +0000944 infos.append("errors=%d" % errored)
Fred Drake02538202001-03-21 18:09:46 +0000945 else:
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000946 self.stream.writeln("OK")
Benjamin Peterson692428e2009-03-23 21:50:21 +0000947 if skipped:
948 infos.append("skipped=%d" % skipped)
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000949 if expectedFails:
950 infos.append("expected failures=%d" % expectedFails)
951 if unexpectedSuccesses:
952 infos.append("unexpected successes=%d" % unexpectedSuccesses)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000953 if infos:
954 self.stream.writeln(" (%s)" % (", ".join(infos),))
Fred Drake02538202001-03-21 18:09:46 +0000955 return result
Tim Petersa19a1682001-03-29 04:36:09 +0000956
Fred Drake02538202001-03-21 18:09:46 +0000957
Fred Drake02538202001-03-21 18:09:46 +0000958
959##############################################################################
960# Facilities for running tests from the command line
961##############################################################################
962
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000963class TestProgram(object):
Fred Drake02538202001-03-21 18:09:46 +0000964 """A command-line program that runs a set of tests; this is primarily
965 for making test modules conveniently executable.
966 """
967 USAGE = """\
Steve Purcell17a781b2001-04-09 15:37:31 +0000968Usage: %(progName)s [options] [test] [...]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000969
970Options:
971 -h, --help Show this message
972 -v, --verbose Verbose output
973 -q, --quiet Minimal output
Fred Drake02538202001-03-21 18:09:46 +0000974
975Examples:
976 %(progName)s - run default set of tests
977 %(progName)s MyTestSuite - run suite 'MyTestSuite'
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000978 %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
979 %(progName)s MyTestCase - run all 'test*' test methods
Fred Drake02538202001-03-21 18:09:46 +0000980 in MyTestCase
981"""
982 def __init__(self, module='__main__', defaultTest=None,
Georg Brandld0a96252007-03-07 09:21:06 +0000983 argv=None, testRunner=TextTestRunner,
984 testLoader=defaultTestLoader):
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000985 if isinstance(module, basestring):
Fred Drake02538202001-03-21 18:09:46 +0000986 self.module = __import__(module)
Steve Purcell7e743842003-09-22 11:08:12 +0000987 for part in module.split('.')[1:]:
Fred Drake02538202001-03-21 18:09:46 +0000988 self.module = getattr(self.module, part)
989 else:
990 self.module = module
991 if argv is None:
992 argv = sys.argv
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000993 self.verbosity = 1
Fred Drake02538202001-03-21 18:09:46 +0000994 self.defaultTest = defaultTest
995 self.testRunner = testRunner
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000996 self.testLoader = testLoader
Fred Drake02538202001-03-21 18:09:46 +0000997 self.progName = os.path.basename(argv[0])
998 self.parseArgs(argv)
Fred Drake02538202001-03-21 18:09:46 +0000999 self.runTests()
1000
1001 def usageExit(self, msg=None):
Benjamin Petersona7d441d2009-03-24 00:35:20 +00001002 if msg:
1003 print msg
Fred Drake02538202001-03-21 18:09:46 +00001004 print self.USAGE % self.__dict__
1005 sys.exit(2)
1006
1007 def parseArgs(self, argv):
1008 import getopt
Benjamin Peterson692428e2009-03-23 21:50:21 +00001009 long_opts = ['help','verbose','quiet']
Fred Drake02538202001-03-21 18:09:46 +00001010 try:
Benjamin Peterson692428e2009-03-23 21:50:21 +00001011 options, args = getopt.getopt(argv[1:], 'hHvq', long_opts)
Fred Drake02538202001-03-21 18:09:46 +00001012 for opt, value in options:
1013 if opt in ('-h','-H','--help'):
1014 self.usageExit()
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001015 if opt in ('-q','--quiet'):
1016 self.verbosity = 0
1017 if opt in ('-v','--verbose'):
1018 self.verbosity = 2
Fred Drake02538202001-03-21 18:09:46 +00001019 if len(args) == 0 and self.defaultTest is None:
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001020 self.test = self.testLoader.loadTestsFromModule(self.module)
1021 return
Fred Drake02538202001-03-21 18:09:46 +00001022 if len(args) > 0:
1023 self.testNames = args
1024 else:
1025 self.testNames = (self.defaultTest,)
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001026 self.createTests()
Fred Drake02538202001-03-21 18:09:46 +00001027 except getopt.error, msg:
1028 self.usageExit(msg)
1029
1030 def createTests(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001031 self.test = self.testLoader.loadTestsFromNames(self.testNames,
1032 self.module)
Fred Drake02538202001-03-21 18:09:46 +00001033
1034 def runTests(self):
Georg Brandld0a96252007-03-07 09:21:06 +00001035 if isinstance(self.testRunner, (type, types.ClassType)):
1036 try:
1037 testRunner = self.testRunner(verbosity=self.verbosity)
1038 except TypeError:
1039 # didn't accept the verbosity argument
1040 testRunner = self.testRunner()
1041 else:
1042 # it is assumed to be a TestRunner instance
1043 testRunner = self.testRunner
1044 result = testRunner.run(self.test)
Tim Petersa19a1682001-03-29 04:36:09 +00001045 sys.exit(not result.wasSuccessful())
Fred Drake02538202001-03-21 18:09:46 +00001046
1047main = TestProgram
1048
1049
1050##############################################################################
1051# Executing this module from the command line
1052##############################################################################
1053
1054if __name__ == "__main__":
1055 main(module=None)