blob: c67ec0633c8c80bf7b4008a558a860bfcf1286c9 [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##############################################################################
58__all__ = ['TestResult', 'TestCase', 'TestSuite', 'TextTestRunner',
59 'TestLoader', 'FunctionTestCase', 'main', 'defaultTestLoader']
60
Steve Purcell7e743842003-09-22 11:08:12 +000061# Expose obsolete functions for backwards compatibility
Steve Purcelld75e7e42003-09-15 11:01:21 +000062__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
63
64
65##############################################################################
Steve Purcell7e743842003-09-22 11:08:12 +000066# Backward compatibility
67##############################################################################
Steve Purcell7e743842003-09-22 11:08:12 +000068
Raymond Hettinger5930d8f2008-07-10 16:06:41 +000069def _CmpToKey(mycmp):
70 'Convert a cmp= function into a key= function'
71 class K(object):
72 def __init__(self, obj):
73 self.obj = obj
74 def __lt__(self, other):
75 return mycmp(self.obj, other.obj) == -1
76 return K
Steve Purcell7e743842003-09-22 11:08:12 +000077
78##############################################################################
Fred Drake02538202001-03-21 18:09:46 +000079# Test framework core
80##############################################################################
81
Steve Purcelldc391a62002-08-09 09:46:23 +000082def _strclass(cls):
83 return "%s.%s" % (cls.__module__, cls.__name__)
84
Benjamin Peterson692428e2009-03-23 21:50:21 +000085
86class SkipTest(Exception):
87 """
88 Raise this exception in a test to skip it.
89
90 Usually you can use TestResult.skip() or one of the skipping decorators
91 instead of raising this directly.
92 """
93 pass
94
95class _ExpectedFailure(Exception):
96 """
97 Raise this when a test is expected to fail.
98
99 This is an implementation detail.
100 """
101
102 def __init__(self, exc_info):
103 super(_ExpectedFailure, self).__init__()
104 self.exc_info = exc_info
105
106class _UnexpectedSuccess(Exception):
107 """
108 The test was supposed to fail, but it didn't!
109 """
110 pass
111
112def _id(obj):
113 return obj
114
115def skip(reason):
116 """
117 Unconditionally skip a test.
118 """
119 def decorator(test_item):
120 if isinstance(test_item, type) and issubclass(test_item, TestCase):
121 test_item.__unittest_skip__ = True
122 test_item.__unittest_skip_why__ = reason
123 return test_item
124 @functools.wraps(test_item)
125 def skip_wrapper(*args, **kwargs):
126 raise SkipTest(reason)
127 return skip_wrapper
128 return decorator
129
130def skipIf(condition, reason):
131 """
132 Skip a test if the condition is true.
133 """
134 if condition:
135 return skip(reason)
136 return _id
137
138def skipUnless(condition, reason):
139 """
140 Skip a test unless the condition is true.
141 """
142 if not condition:
143 return skip(reason)
144 return _id
145
146
147def expectedFailure(func):
148 @functools.wraps(func)
149 def wrapper(*args, **kwargs):
150 try:
151 func(*args, **kwargs)
152 except Exception:
153 raise _ExpectedFailure(sys.exc_info())
154 raise _UnexpectedSuccess
155 return wrapper
156
157
Steve Purcellb8d5f242003-12-06 13:03:13 +0000158__unittest = 1
159
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000160class TestResult(object):
Fred Drake02538202001-03-21 18:09:46 +0000161 """Holder for test result information.
162
163 Test results are automatically managed by the TestCase and TestSuite
164 classes, and do not need to be explicitly manipulated by writers of tests.
165
166 Each instance holds the total number of tests run, and collections of
167 failures and errors that occurred among those test runs. The collections
Steve Purcell7b065702001-09-06 08:24:40 +0000168 contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
Fred Drake656f9ec2001-09-06 19:13:14 +0000169 formatted traceback of the error that occurred.
Fred Drake02538202001-03-21 18:09:46 +0000170 """
171 def __init__(self):
172 self.failures = []
173 self.errors = []
174 self.testsRun = 0
Benjamin Peterson692428e2009-03-23 21:50:21 +0000175 self.skipped = []
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000176 self.expectedFailures = []
177 self.unexpectedSuccesses = []
Georg Brandl15c5ce92007-03-07 09:09:40 +0000178 self.shouldStop = False
Fred Drake02538202001-03-21 18:09:46 +0000179
180 def startTest(self, test):
181 "Called when the given test is about to be run"
182 self.testsRun = self.testsRun + 1
183
184 def stopTest(self, test):
185 "Called when the given test has been run"
186 pass
187
188 def addError(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000189 """Called when an error has occurred. 'err' is a tuple of values as
190 returned by sys.exc_info().
191 """
Steve Purcellb8d5f242003-12-06 13:03:13 +0000192 self.errors.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000193
194 def addFailure(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000195 """Called when an error has occurred. 'err' is a tuple of values as
196 returned by sys.exc_info()."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000197 self.failures.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000198
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000199 def addSuccess(self, test):
200 "Called when a test has completed successfully"
201 pass
202
Benjamin Peterson692428e2009-03-23 21:50:21 +0000203 def addSkip(self, test, reason):
204 """Called when a test is skipped."""
205 self.skipped.append((test, reason))
206
207 def addExpectedFailure(self, test, err):
208 """Called when an expected failure/error occured."""
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000209 self.expectedFailures.append(
Benjamin Peterson692428e2009-03-23 21:50:21 +0000210 (test, self._exc_info_to_string(err, test)))
211
212 def addUnexpectedSuccess(self, test):
213 """Called when a test was expected to fail, but succeed."""
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000214 self.unexpectedSuccesses.append(test)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000215
Fred Drake02538202001-03-21 18:09:46 +0000216 def wasSuccessful(self):
217 "Tells whether or not this result was a success"
218 return len(self.failures) == len(self.errors) == 0
219
220 def stop(self):
221 "Indicates that the tests should be aborted"
Steve Purcell7e743842003-09-22 11:08:12 +0000222 self.shouldStop = True
Tim Petersa19a1682001-03-29 04:36:09 +0000223
Steve Purcellb8d5f242003-12-06 13:03:13 +0000224 def _exc_info_to_string(self, err, test):
Steve Purcell7b065702001-09-06 08:24:40 +0000225 """Converts a sys.exc_info()-style tuple of values into a string."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000226 exctype, value, tb = err
227 # Skip test runner traceback levels
228 while tb and self._is_relevant_tb_level(tb):
229 tb = tb.tb_next
230 if exctype is test.failureException:
231 # Skip assert*() traceback levels
232 length = self._count_relevant_tb_levels(tb)
233 return ''.join(traceback.format_exception(exctype, value, tb, length))
234 return ''.join(traceback.format_exception(exctype, value, tb))
235
236 def _is_relevant_tb_level(self, tb):
Georg Brandl56af5fc2008-07-18 19:30:10 +0000237 return '__unittest' in tb.tb_frame.f_globals
Steve Purcellb8d5f242003-12-06 13:03:13 +0000238
239 def _count_relevant_tb_levels(self, tb):
240 length = 0
241 while tb and not self._is_relevant_tb_level(tb):
242 length += 1
243 tb = tb.tb_next
244 return length
Steve Purcell7b065702001-09-06 08:24:40 +0000245
Fred Drake02538202001-03-21 18:09:46 +0000246 def __repr__(self):
247 return "<%s run=%i errors=%i failures=%i>" % \
Steve Purcelldc391a62002-08-09 09:46:23 +0000248 (_strclass(self.__class__), self.testsRun, len(self.errors),
Fred Drake02538202001-03-21 18:09:46 +0000249 len(self.failures))
250
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000251
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000252class AssertRaisesContext(object):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000253
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000254 def __init__(self, expected, test_case):
255 self.expected = expected
256 self.failureException = test_case.failureException
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000257
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000258 def __enter__(self):
259 pass
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000260
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000261 def __exit__(self, exc_type, exc_value, traceback):
262 if exc_type is None:
263 try:
264 exc_name = self.expected.__name__
265 except AttributeError:
266 exc_name = str(self.expected)
267 raise self.failureException(
268 "{0} not raised".format(exc_name))
269 if issubclass(exc_type, self.expected):
270 return True
271 # Let unexpected exceptions skip through
272 return False
273
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000274
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000275class TestCase(object):
Fred Drake02538202001-03-21 18:09:46 +0000276 """A class whose instances are single test cases.
277
Fred Drake02538202001-03-21 18:09:46 +0000278 By default, the test code itself should be placed in a method named
279 'runTest'.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000280
Tim Petersa19a1682001-03-29 04:36:09 +0000281 If the fixture may be used for many test cases, create as
Fred Drake02538202001-03-21 18:09:46 +0000282 many test methods as are needed. When instantiating such a TestCase
283 subclass, specify in the constructor arguments the name of the test method
284 that the instance is to execute.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000285
Tim Petersa19a1682001-03-29 04:36:09 +0000286 Test authors should subclass TestCase for their own tests. Construction
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000287 and deconstruction of the test's environment ('fixture') can be
288 implemented by overriding the 'setUp' and 'tearDown' methods respectively.
289
290 If it is necessary to override the __init__ method, the base class
291 __init__ method must always be called. It is important that subclasses
292 should not change the signature of their __init__ method, since instances
293 of the classes are instantiated automatically by parts of the framework
294 in order to be run.
Fred Drake02538202001-03-21 18:09:46 +0000295 """
Steve Purcell15d89272001-04-12 09:05:01 +0000296
297 # This attribute determines which exception will be raised when
298 # the instance's assertion methods fail; test methods raising this
299 # exception will be deemed to have 'failed' rather than 'errored'
300
301 failureException = AssertionError
302
Fred Drake02538202001-03-21 18:09:46 +0000303 def __init__(self, methodName='runTest'):
304 """Create an instance of the class that will use the named test
305 method when executed. Raises a ValueError if the instance does
306 not have a method with the specified name.
307 """
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000308 self._testMethodName = methodName
Fred Drake02538202001-03-21 18:09:46 +0000309 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000310 testMethod = getattr(self, methodName)
Fred Drake02538202001-03-21 18:09:46 +0000311 except AttributeError:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000312 raise ValueError("no such test method in %s: %s" % \
313 (self.__class__, methodName))
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000314 self._testMethodDoc = testMethod.__doc__
Fred Drake02538202001-03-21 18:09:46 +0000315
316 def setUp(self):
317 "Hook method for setting up the test fixture before exercising it."
318 pass
319
320 def tearDown(self):
321 "Hook method for deconstructing the test fixture after testing it."
322 pass
323
324 def countTestCases(self):
325 return 1
326
327 def defaultTestResult(self):
328 return TestResult()
329
330 def shortDescription(self):
331 """Returns a one-line description of the test, or None if no
332 description has been provided.
333
334 The default implementation of this method returns the first line of
335 the specified test method's docstring.
336 """
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000337 doc = self._testMethodDoc
Steve Purcell7e743842003-09-22 11:08:12 +0000338 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000339
340 def id(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000341 return "%s.%s" % (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000342
Georg Brandl15c5ce92007-03-07 09:09:40 +0000343 def __eq__(self, other):
344 if type(self) is not type(other):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000345 return NotImplemented
Georg Brandl15c5ce92007-03-07 09:09:40 +0000346
347 return self._testMethodName == other._testMethodName
348
349 def __ne__(self, other):
350 return not self == other
351
352 def __hash__(self):
Collin Winter9453e5d2007-03-09 23:30:39 +0000353 return hash((type(self), self._testMethodName))
Georg Brandl15c5ce92007-03-07 09:09:40 +0000354
Fred Drake02538202001-03-21 18:09:46 +0000355 def __str__(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000356 return "%s (%s)" % (self._testMethodName, _strclass(self.__class__))
Fred Drake02538202001-03-21 18:09:46 +0000357
358 def __repr__(self):
359 return "<%s testMethod=%s>" % \
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000360 (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000361
362 def run(self, result=None):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000363 if result is None:
364 result = self.defaultTestResult()
Fred Drake02538202001-03-21 18:09:46 +0000365 result.startTest(self)
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000366 testMethod = getattr(self, self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000367 try:
368 try:
369 self.setUp()
Benjamin Peterson692428e2009-03-23 21:50:21 +0000370 except SkipTest as e:
371 result.addSkip(self, str(e))
372 return
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000373 except Exception:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000374 result.addError(self, self._exc_info())
Fred Drake02538202001-03-21 18:09:46 +0000375 return
376
Benjamin Peterson692428e2009-03-23 21:50:21 +0000377 success = False
Fred Drake02538202001-03-21 18:09:46 +0000378 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000379 testMethod()
Skip Montanaroae5c37b2003-07-13 15:18:12 +0000380 except self.failureException:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000381 result.addFailure(self, self._exc_info())
Benjamin Peterson692428e2009-03-23 21:50:21 +0000382 except _ExpectedFailure as e:
383 result.addExpectedFailure(self, e.exc_info)
384 except _UnexpectedSuccess:
385 result.addUnexpectedSuccess(self)
386 except SkipTest as e:
387 result.addSkip(self, str(e))
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000388 except Exception:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000389 result.addError(self, self._exc_info())
Benjamin Peterson692428e2009-03-23 21:50:21 +0000390 else:
391 success = True
Fred Drake02538202001-03-21 18:09:46 +0000392
393 try:
394 self.tearDown()
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000395 except Exception:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000396 result.addError(self, self._exc_info())
Benjamin Peterson692428e2009-03-23 21:50:21 +0000397 success = False
398 if success:
399 result.addSuccess(self)
Fred Drake02538202001-03-21 18:09:46 +0000400 finally:
401 result.stopTest(self)
402
Raymond Hettinger664347b2004-12-04 21:21:53 +0000403 def __call__(self, *args, **kwds):
404 return self.run(*args, **kwds)
Steve Purcell7e743842003-09-22 11:08:12 +0000405
Fred Drake02538202001-03-21 18:09:46 +0000406 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000407 """Run the test without collecting errors in a TestResult"""
Fred Drake02538202001-03-21 18:09:46 +0000408 self.setUp()
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000409 getattr(self, self._testMethodName)()
Fred Drake02538202001-03-21 18:09:46 +0000410 self.tearDown()
411
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000412 def _exc_info(self):
Steve Purcell15d89272001-04-12 09:05:01 +0000413 """Return a version of sys.exc_info() with the traceback frame
414 minimised; usually the top level of the traceback frame is not
415 needed.
Fred Drake02538202001-03-21 18:09:46 +0000416 """
Georg Brandl15c5ce92007-03-07 09:09:40 +0000417 return sys.exc_info()
Fred Drake02538202001-03-21 18:09:46 +0000418
Benjamin Peterson692428e2009-03-23 21:50:21 +0000419 def skip(self, reason):
420 """Skip this test."""
421 raise SkipTest(reason)
422
Steve Purcell15d89272001-04-12 09:05:01 +0000423 def fail(self, msg=None):
424 """Fail immediately, with the given message."""
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000425 raise self.failureException(msg)
Fred Drake02538202001-03-21 18:09:46 +0000426
427 def failIf(self, expr, msg=None):
428 "Fail the test if the expression is true."
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000429 if expr:
430 raise self.failureException(msg)
Fred Drake02538202001-03-21 18:09:46 +0000431
Steve Purcell15d89272001-04-12 09:05:01 +0000432 def failUnless(self, expr, msg=None):
433 """Fail the test unless the expression is true."""
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000434 if not expr:
435 raise self.failureException(msg)
Steve Purcell15d89272001-04-12 09:05:01 +0000436
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000437 def failUnlessRaises(self, excClass, callableObj=None, *args, **kwargs):
Steve Purcell15d89272001-04-12 09:05:01 +0000438 """Fail unless an exception of class excClass is thrown
Fred Drake02538202001-03-21 18:09:46 +0000439 by callableObj when invoked with arguments args and keyword
440 arguments kwargs. If a different type of exception is
441 thrown, it will not be caught, and the test case will be
442 deemed to have suffered an error, exactly as for an
443 unexpected exception.
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000444
445 If called with callableObj omitted or None, will return a
446 context object used like this::
447
448 with self.failUnlessRaises(some_error_class):
449 do_something()
Fred Drake02538202001-03-21 18:09:46 +0000450 """
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000451 context = AssertRaisesContext(excClass, self)
452 if callableObj is None:
453 return context
454 with context:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000455 callableObj(*args, **kwargs)
Fred Drake02538202001-03-21 18:09:46 +0000456
Steve Purcell15d89272001-04-12 09:05:01 +0000457 def failUnlessEqual(self, first, second, msg=None):
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000458 """Fail if the two objects are unequal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000459 operator.
460 """
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000461 if not first == second:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000462 raise self.failureException(msg or '%r != %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000463
Steve Purcell15d89272001-04-12 09:05:01 +0000464 def failIfEqual(self, first, second, msg=None):
465 """Fail if the two objects are equal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000466 operator.
467 """
Steve Purcell15d89272001-04-12 09:05:01 +0000468 if first == second:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000469 raise self.failureException(msg or '%r == %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000470
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000471 def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
472 """Fail if the two objects are unequal as determined by their
473 difference rounded to the given number of decimal places
474 (default 7) and comparing to zero.
475
Steve Purcell397b45d2003-10-26 10:41:03 +0000476 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000477 as significant digits (measured from the most signficant digit).
478 """
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000479 if round(abs(second-first), places) != 0:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000480 raise self.failureException(
481 msg or '%r != %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000482
483 def failIfAlmostEqual(self, first, second, places=7, msg=None):
484 """Fail if the two objects are equal as determined by their
485 difference rounded to the given number of decimal places
486 (default 7) and comparing to zero.
487
Steve Purcellcca34912003-10-26 16:38:16 +0000488 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000489 as significant digits (measured from the most signficant digit).
490 """
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000491 if round(abs(second-first), places) == 0:
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000492 raise self.failureException(
493 msg or '%r == %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000494
Steve Purcell7e743842003-09-22 11:08:12 +0000495 # Synonyms for assertion methods
496
Steve Purcell15d89272001-04-12 09:05:01 +0000497 assertEqual = assertEquals = failUnlessEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000498
Steve Purcell15d89272001-04-12 09:05:01 +0000499 assertNotEqual = assertNotEquals = failIfEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000500
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000501 assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual
502
503 assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual
504
Steve Purcell15d89272001-04-12 09:05:01 +0000505 assertRaises = failUnlessRaises
506
Steve Purcell7e743842003-09-22 11:08:12 +0000507 assert_ = assertTrue = failUnless
508
509 assertFalse = failIf
Steve Purcell15d89272001-04-12 09:05:01 +0000510
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000511
Fred Drake02538202001-03-21 18:09:46 +0000512
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000513class TestSuite(object):
Fred Drake02538202001-03-21 18:09:46 +0000514 """A test suite is a composite test consisting of a number of TestCases.
515
516 For use, create an instance of TestSuite, then add test case instances.
517 When all tests have been added, the suite can be passed to a test
518 runner, such as TextTestRunner. It will run the individual test cases
519 in the order in which they were added, aggregating the results. When
520 subclassing, do not forget to call the base class constructor.
521 """
522 def __init__(self, tests=()):
523 self._tests = []
524 self.addTests(tests)
525
526 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000527 return "<%s tests=%s>" % (_strclass(self.__class__), self._tests)
Fred Drake02538202001-03-21 18:09:46 +0000528
Georg Brandl15c5ce92007-03-07 09:09:40 +0000529 def __eq__(self, other):
Benjamin Peterson692428e2009-03-23 21:50:21 +0000530 if not isinstance(other, self.__class__):
531 return NotImplemented
Georg Brandl15c5ce92007-03-07 09:09:40 +0000532 return self._tests == other._tests
533
534 def __ne__(self, other):
535 return not self == other
536
Nick Coghlan48361f52008-08-11 15:45:58 +0000537 # Can't guarantee hash invariant, so flag as unhashable
538 __hash__ = None
539
Jim Fultonfafd8742004-08-28 15:22:12 +0000540 def __iter__(self):
541 return iter(self._tests)
542
Fred Drake02538202001-03-21 18:09:46 +0000543 def countTestCases(self):
544 cases = 0
545 for test in self._tests:
Steve Purcell7e743842003-09-22 11:08:12 +0000546 cases += test.countTestCases()
Fred Drake02538202001-03-21 18:09:46 +0000547 return cases
548
549 def addTest(self, test):
Georg Brandld9e50262007-03-07 11:54:49 +0000550 # sanity checks
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000551 if not hasattr(test, '__call__'):
Georg Brandld9e50262007-03-07 11:54:49 +0000552 raise TypeError("the test to add must be callable")
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000553 if isinstance(test, type) and issubclass(test, (TestCase, TestSuite)):
Georg Brandld9e50262007-03-07 11:54:49 +0000554 raise TypeError("TestCases and TestSuites must be instantiated "
555 "before passing them to addTest()")
Fred Drake02538202001-03-21 18:09:46 +0000556 self._tests.append(test)
557
558 def addTests(self, tests):
Georg Brandld9e50262007-03-07 11:54:49 +0000559 if isinstance(tests, basestring):
560 raise TypeError("tests must be an iterable of tests, not a string")
Fred Drake02538202001-03-21 18:09:46 +0000561 for test in tests:
562 self.addTest(test)
563
564 def run(self, result):
Fred Drake02538202001-03-21 18:09:46 +0000565 for test in self._tests:
566 if result.shouldStop:
567 break
568 test(result)
569 return result
570
Raymond Hettinger664347b2004-12-04 21:21:53 +0000571 def __call__(self, *args, **kwds):
572 return self.run(*args, **kwds)
573
Fred Drake02538202001-03-21 18:09:46 +0000574 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000575 """Run the tests without collecting errors in a TestResult"""
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000576 for test in self._tests:
577 test.debug()
Fred Drake02538202001-03-21 18:09:46 +0000578
579
Benjamin Peterson692428e2009-03-23 21:50:21 +0000580class ClassTestSuite(TestSuite):
581 """
582 Suite of tests derived from a single TestCase class.
583 """
584
585 def __init__(self, tests, class_collected_from):
586 super(ClassTestSuite, self).__init__(tests)
587 self.collected_from = class_collected_from
588
589 def id(self):
590 module = getattr(self.collected_from, "__module__", None)
591 if module is not None:
592 return "{0}.{1}".format(module, self.collected_from.__name__)
593 return self.collected_from.__name__
594
595 def run(self, result):
596 if getattr(self.collected_from, "__unittest_skip__", False):
597 # ClassTestSuite result pretends to be a TestCase enough to be
598 # reported.
599 result.startTest(self)
600 try:
601 result.addSkip(self, self.collected_from.__unittest_skip_why__)
602 finally:
603 result.stopTest(self)
604 else:
605 result = super(ClassTestSuite, self).run(result)
606 return result
607
608 shortDescription = id
609
610
Fred Drake02538202001-03-21 18:09:46 +0000611class FunctionTestCase(TestCase):
612 """A test case that wraps a test function.
613
614 This is useful for slipping pre-existing test functions into the
Georg Brandl15c5ce92007-03-07 09:09:40 +0000615 unittest framework. Optionally, set-up and tidy-up functions can be
Fred Drake02538202001-03-21 18:09:46 +0000616 supplied. As with TestCase, the tidy-up ('tearDown') function will
617 always be called if the set-up ('setUp') function ran successfully.
618 """
619
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000620 def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
621 super(FunctionTestCase, self).__init__()
Fred Drake02538202001-03-21 18:09:46 +0000622 self.__setUpFunc = setUp
623 self.__tearDownFunc = tearDown
624 self.__testFunc = testFunc
625 self.__description = description
626
627 def setUp(self):
628 if self.__setUpFunc is not None:
629 self.__setUpFunc()
630
631 def tearDown(self):
632 if self.__tearDownFunc is not None:
633 self.__tearDownFunc()
634
635 def runTest(self):
636 self.__testFunc()
637
638 def id(self):
639 return self.__testFunc.__name__
640
Georg Brandl15c5ce92007-03-07 09:09:40 +0000641 def __eq__(self, other):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000642 if not isinstance(other, self.__class__):
643 return NotImplemented
Georg Brandl15c5ce92007-03-07 09:09:40 +0000644
645 return self.__setUpFunc == other.__setUpFunc and \
646 self.__tearDownFunc == other.__tearDownFunc and \
647 self.__testFunc == other.__testFunc and \
648 self.__description == other.__description
649
650 def __ne__(self, other):
651 return not self == other
652
653 def __hash__(self):
Collin Winter9453e5d2007-03-09 23:30:39 +0000654 return hash((type(self), self.__setUpFunc, self.__tearDownFunc,
655 self.__testFunc, self.__description))
Georg Brandl15c5ce92007-03-07 09:09:40 +0000656
Fred Drake02538202001-03-21 18:09:46 +0000657 def __str__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000658 return "%s (%s)" % (_strclass(self.__class__), self.__testFunc.__name__)
Fred Drake02538202001-03-21 18:09:46 +0000659
660 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000661 return "<%s testFunc=%s>" % (_strclass(self.__class__), self.__testFunc)
Fred Drake02538202001-03-21 18:09:46 +0000662
663 def shortDescription(self):
664 if self.__description is not None: return self.__description
665 doc = self.__testFunc.__doc__
Steve Purcell7e743842003-09-22 11:08:12 +0000666 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000667
668
669
670##############################################################################
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000671# Locating and loading tests
Fred Drake02538202001-03-21 18:09:46 +0000672##############################################################################
673
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000674class TestLoader(object):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000675 """
676 This class is responsible for loading tests according to various criteria
677 and returning them wrapped in a TestSuite
Fred Drake02538202001-03-21 18:09:46 +0000678 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000679 testMethodPrefix = 'test'
680 sortTestMethodsUsing = cmp
681 suiteClass = TestSuite
Benjamin Peterson692428e2009-03-23 21:50:21 +0000682 classSuiteClass = ClassTestSuite
Fred Drake02538202001-03-21 18:09:46 +0000683
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000684 def loadTestsFromTestCase(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000685 """Return a suite of all tests cases contained in testCaseClass"""
Johannes Gijsbersd7b6ad42004-11-07 15:46:25 +0000686 if issubclass(testCaseClass, TestSuite):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000687 raise TypeError("Test cases should not be derived from TestSuite." \
688 " Maybe you meant to derive from TestCase?")
Steve Purcell7e743842003-09-22 11:08:12 +0000689 testCaseNames = self.getTestCaseNames(testCaseClass)
690 if not testCaseNames and hasattr(testCaseClass, 'runTest'):
691 testCaseNames = ['runTest']
Benjamin Peterson692428e2009-03-23 21:50:21 +0000692 suite = self.classSuiteClass(map(testCaseClass, testCaseNames),
693 testCaseClass)
694 return suite
Fred Drake02538202001-03-21 18:09:46 +0000695
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000696 def loadTestsFromModule(self, module):
Steve Purcell15d89272001-04-12 09:05:01 +0000697 """Return a suite of all tests cases contained in the given module"""
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000698 tests = []
699 for name in dir(module):
700 obj = getattr(module, name)
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000701 if isinstance(obj, type) and issubclass(obj, TestCase):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000702 tests.append(self.loadTestsFromTestCase(obj))
703 return self.suiteClass(tests)
Fred Drake02538202001-03-21 18:09:46 +0000704
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000705 def loadTestsFromName(self, name, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000706 """Return a suite of all tests cases given a string specifier.
707
708 The name may resolve either to a module, a test case class, a
709 test method within a test case class, or a callable object which
710 returns a TestCase or TestSuite instance.
Tim Peters613b2222001-04-13 05:37:27 +0000711
Steve Purcell15d89272001-04-12 09:05:01 +0000712 The method optionally resolves the names relative to a given module.
713 """
Steve Purcell7e743842003-09-22 11:08:12 +0000714 parts = name.split('.')
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000715 if module is None:
Steve Purcell7e743842003-09-22 11:08:12 +0000716 parts_copy = parts[:]
717 while parts_copy:
718 try:
719 module = __import__('.'.join(parts_copy))
720 break
721 except ImportError:
722 del parts_copy[-1]
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000723 if not parts_copy:
724 raise
Armin Rigo1b3c04b2003-10-24 17:15:29 +0000725 parts = parts[1:]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000726 obj = module
727 for part in parts:
Steve Purcell7e743842003-09-22 11:08:12 +0000728 parent, obj = obj, getattr(obj, part)
Fred Drake02538202001-03-21 18:09:46 +0000729
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000730 if isinstance(obj, types.ModuleType):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000731 return self.loadTestsFromModule(obj)
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000732 elif isinstance(obj, type) and issubclass(obj, TestCase):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000733 return self.loadTestsFromTestCase(obj)
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000734 elif (isinstance(obj, types.UnboundMethodType) and
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000735 isinstance(parent, type) and
Georg Brandl15c5ce92007-03-07 09:09:40 +0000736 issubclass(parent, TestCase)):
737 return TestSuite([parent(obj.__name__)])
Steve Purcell397b45d2003-10-26 10:41:03 +0000738 elif isinstance(obj, TestSuite):
Steve Purcell7e743842003-09-22 11:08:12 +0000739 return obj
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000740 elif hasattr(obj, '__call__'):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000741 test = obj()
Georg Brandl15c5ce92007-03-07 09:09:40 +0000742 if isinstance(test, TestSuite):
743 return test
744 elif isinstance(test, TestCase):
745 return TestSuite([test])
746 else:
747 raise TypeError("calling %s returned %s, not a test" %
748 (obj, test))
Fred Drake02538202001-03-21 18:09:46 +0000749 else:
Georg Brandl15c5ce92007-03-07 09:09:40 +0000750 raise TypeError("don't know how to make test from: %s" % obj)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000751
752 def loadTestsFromNames(self, names, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000753 """Return a suite of all tests cases found using the given sequence
754 of string specifiers. See 'loadTestsFromName()'.
755 """
Steve Purcell7e743842003-09-22 11:08:12 +0000756 suites = [self.loadTestsFromName(name, module) for name in names]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000757 return self.suiteClass(suites)
758
759 def getTestCaseNames(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000760 """Return a sorted sequence of method names found within testCaseClass
761 """
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000762 def isTestMethod(attrname, testCaseClass=testCaseClass,
763 prefix=self.testMethodPrefix):
764 return attrname.startswith(prefix) and \
765 hasattr(getattr(testCaseClass, attrname), '__call__')
Steve Purcell7e743842003-09-22 11:08:12 +0000766 testFnNames = filter(isTestMethod, dir(testCaseClass))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000767 if self.sortTestMethodsUsing:
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000768 testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000769 return testFnNames
770
771
772
773defaultTestLoader = TestLoader()
774
775
776##############################################################################
777# Patches for old functions: these functions should be considered obsolete
778##############################################################################
779
780def _makeLoader(prefix, sortUsing, suiteClass=None):
781 loader = TestLoader()
782 loader.sortTestMethodsUsing = sortUsing
783 loader.testMethodPrefix = prefix
784 if suiteClass: loader.suiteClass = suiteClass
785 return loader
786
787def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp):
788 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
789
790def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
791 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
792
793def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
794 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module)
Fred Drake02538202001-03-21 18:09:46 +0000795
796
797##############################################################################
798# Text UI
799##############################################################################
800
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000801class _WritelnDecorator(object):
Fred Drake02538202001-03-21 18:09:46 +0000802 """Used to decorate file-like objects with a handy 'writeln' method"""
803 def __init__(self,stream):
804 self.stream = stream
Fred Drake02538202001-03-21 18:09:46 +0000805
806 def __getattr__(self, attr):
807 return getattr(self.stream,attr)
808
Raymond Hettinger91dd19d2003-09-13 02:58:00 +0000809 def writeln(self, arg=None):
810 if arg: self.write(arg)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000811 self.write('\n') # text-mode streams translate to \r\n if needed
Tim Petersa19a1682001-03-29 04:36:09 +0000812
Fred Drake02538202001-03-21 18:09:46 +0000813
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000814class _TextTestResult(TestResult):
Fred Drake02538202001-03-21 18:09:46 +0000815 """A test result class that can print formatted text results to a stream.
816
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000817 Used by TextTestRunner.
Fred Drake02538202001-03-21 18:09:46 +0000818 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000819 separator1 = '=' * 70
820 separator2 = '-' * 70
Fred Drake02538202001-03-21 18:09:46 +0000821
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000822 def __init__(self, stream, descriptions, verbosity):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000823 super(_TextTestResult, self).__init__()
Fred Drake02538202001-03-21 18:09:46 +0000824 self.stream = stream
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000825 self.showAll = verbosity > 1
826 self.dots = verbosity == 1
Fred Drake02538202001-03-21 18:09:46 +0000827 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000828
829 def getDescription(self, test):
830 if self.descriptions:
831 return test.shortDescription() or str(test)
832 else:
833 return str(test)
834
Fred Drake02538202001-03-21 18:09:46 +0000835 def startTest(self, test):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000836 super(_TextTestResult, self).startTest(test)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000837 if self.showAll:
838 self.stream.write(self.getDescription(test))
839 self.stream.write(" ... ")
Georg Brandld0632402008-05-11 15:17:41 +0000840 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000841
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000842 def addSuccess(self, test):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000843 super(_TextTestResult, self).addSuccess(test)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000844 if self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000845 self.stream.writeln("ok")
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000846 elif self.dots:
847 self.stream.write('.')
Georg Brandld0632402008-05-11 15:17:41 +0000848 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000849
850 def addError(self, test, err):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000851 super(_TextTestResult, self).addError(test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000852 if self.showAll:
853 self.stream.writeln("ERROR")
854 elif self.dots:
855 self.stream.write('E')
Georg Brandld0632402008-05-11 15:17:41 +0000856 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000857
858 def addFailure(self, test, err):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000859 super(_TextTestResult, self).addFailure(test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000860 if self.showAll:
861 self.stream.writeln("FAIL")
862 elif self.dots:
863 self.stream.write('F')
Georg Brandld0632402008-05-11 15:17:41 +0000864 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000865
Benjamin Peterson692428e2009-03-23 21:50:21 +0000866 def addSkip(self, test, reason):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000867 super(_TextTestResult, self).addSkip(test, reason)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000868 if self.showAll:
869 self.stream.writeln("skipped {0!r}".format(reason))
870 elif self.dots:
871 self.stream.write("s")
872 self.stream.flush()
873
874 def addExpectedFailure(self, test, err):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000875 super(_TextTestResult, self).addExpectedFailure(test, err)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000876 if self.showAll:
877 self.stream.writeln("expected failure")
878 elif self.dots:
879 self.stream.write(".")
880 self.stream.flush()
881
882 def addUnexpectedSuccess(self, test):
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000883 super(_TextTestResult, self).addUnexpectedSuccess(test)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000884 if self.showAll:
885 self.stream.writeln("unexpected success")
886 elif self.dots:
887 self.stream.write(".")
888 self.stream.flush()
889
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000890 def printErrors(self):
891 if self.dots or self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000892 self.stream.writeln()
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000893 self.printErrorList('ERROR', self.errors)
894 self.printErrorList('FAIL', self.failures)
895
896 def printErrorList(self, flavour, errors):
897 for test, err in errors:
898 self.stream.writeln(self.separator1)
899 self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
900 self.stream.writeln(self.separator2)
Steve Purcell7b065702001-09-06 08:24:40 +0000901 self.stream.writeln("%s" % err)
Fred Drake02538202001-03-21 18:09:46 +0000902
903
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000904class TextTestRunner(object):
Fred Drake02538202001-03-21 18:09:46 +0000905 """A test runner class that displays results in textual form.
Tim Petersa19a1682001-03-29 04:36:09 +0000906
Fred Drake02538202001-03-21 18:09:46 +0000907 It prints out the names of tests as they are run, errors as they
908 occur, and a summary of the results at the end of the test run.
909 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000910 def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1):
Fred Drake02538202001-03-21 18:09:46 +0000911 self.stream = _WritelnDecorator(stream)
912 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000913 self.verbosity = verbosity
914
915 def _makeResult(self):
916 return _TextTestResult(self.stream, self.descriptions, self.verbosity)
Fred Drake02538202001-03-21 18:09:46 +0000917
918 def run(self, test):
919 "Run the given test case or test suite."
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000920 result = self._makeResult()
Fred Drake02538202001-03-21 18:09:46 +0000921 startTime = time.time()
922 test(result)
923 stopTime = time.time()
Steve Purcell397b45d2003-10-26 10:41:03 +0000924 timeTaken = stopTime - startTime
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000925 result.printErrors()
926 self.stream.writeln(result.separator2)
Fred Drake02538202001-03-21 18:09:46 +0000927 run = result.testsRun
928 self.stream.writeln("Ran %d test%s in %.3fs" %
Neal Norwitz76165042002-05-31 14:15:11 +0000929 (run, run != 1 and "s" or "", timeTaken))
Fred Drake02538202001-03-21 18:09:46 +0000930 self.stream.writeln()
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000931 results = map(len, (result.expectedFailures,
932 result.unexpectedSuccesses,
Benjamin Peterson692428e2009-03-23 21:50:21 +0000933 result.skipped))
Benjamin Petersoncb2b0e42009-03-23 22:29:45 +0000934 expectedFails, unexpectedSuccesses, skipped = results
Benjamin Peterson692428e2009-03-23 21:50:21 +0000935 infos = []
Fred Drake02538202001-03-21 18:09:46 +0000936 if not result.wasSuccessful():
Benjamin Peterson692428e2009-03-23 21:50:21 +0000937 self.stream.write("FAILED")
Fred Drake02538202001-03-21 18:09:46 +0000938 failed, errored = map(len, (result.failures, result.errors))
939 if failed:
Benjamin Peterson692428e2009-03-23 21:50:21 +0000940 infos.append("failures=%d" % failed)
Fred Drake02538202001-03-21 18:09:46 +0000941 if errored:
Benjamin Peterson692428e2009-03-23 21:50:21 +0000942 infos.append("errors=%d" % errored)
Fred Drake02538202001-03-21 18:09:46 +0000943 else:
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000944 self.stream.writeln("OK")
Benjamin Peterson692428e2009-03-23 21:50:21 +0000945 if skipped:
946 infos.append("skipped=%d" % skipped)
Benjamin Petersona7d441d2009-03-24 00:35:20 +0000947 if expectedFails:
948 infos.append("expected failures=%d" % expectedFails)
949 if unexpectedSuccesses:
950 infos.append("unexpected successes=%d" % unexpectedSuccesses)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000951 if infos:
952 self.stream.writeln(" (%s)" % (", ".join(infos),))
Fred Drake02538202001-03-21 18:09:46 +0000953 return result
Tim Petersa19a1682001-03-29 04:36:09 +0000954
Fred Drake02538202001-03-21 18:09:46 +0000955
Fred Drake02538202001-03-21 18:09:46 +0000956
957##############################################################################
958# Facilities for running tests from the command line
959##############################################################################
960
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000961class TestProgram(object):
Fred Drake02538202001-03-21 18:09:46 +0000962 """A command-line program that runs a set of tests; this is primarily
963 for making test modules conveniently executable.
964 """
965 USAGE = """\
Steve Purcell17a781b2001-04-09 15:37:31 +0000966Usage: %(progName)s [options] [test] [...]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000967
968Options:
969 -h, --help Show this message
970 -v, --verbose Verbose output
971 -q, --quiet Minimal output
Fred Drake02538202001-03-21 18:09:46 +0000972
973Examples:
974 %(progName)s - run default set of tests
975 %(progName)s MyTestSuite - run suite 'MyTestSuite'
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000976 %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
977 %(progName)s MyTestCase - run all 'test*' test methods
Fred Drake02538202001-03-21 18:09:46 +0000978 in MyTestCase
979"""
980 def __init__(self, module='__main__', defaultTest=None,
Georg Brandld0a96252007-03-07 09:21:06 +0000981 argv=None, testRunner=TextTestRunner,
982 testLoader=defaultTestLoader):
Antoine Pitroudae1a6a2008-12-28 16:01:11 +0000983 if isinstance(module, basestring):
Fred Drake02538202001-03-21 18:09:46 +0000984 self.module = __import__(module)
Steve Purcell7e743842003-09-22 11:08:12 +0000985 for part in module.split('.')[1:]:
Fred Drake02538202001-03-21 18:09:46 +0000986 self.module = getattr(self.module, part)
987 else:
988 self.module = module
989 if argv is None:
990 argv = sys.argv
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000991 self.verbosity = 1
Fred Drake02538202001-03-21 18:09:46 +0000992 self.defaultTest = defaultTest
993 self.testRunner = testRunner
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000994 self.testLoader = testLoader
Fred Drake02538202001-03-21 18:09:46 +0000995 self.progName = os.path.basename(argv[0])
996 self.parseArgs(argv)
Fred Drake02538202001-03-21 18:09:46 +0000997 self.runTests()
998
999 def usageExit(self, msg=None):
Benjamin Petersona7d441d2009-03-24 00:35:20 +00001000 if msg:
1001 print msg
Fred Drake02538202001-03-21 18:09:46 +00001002 print self.USAGE % self.__dict__
1003 sys.exit(2)
1004
1005 def parseArgs(self, argv):
1006 import getopt
Benjamin Peterson692428e2009-03-23 21:50:21 +00001007 long_opts = ['help','verbose','quiet']
Fred Drake02538202001-03-21 18:09:46 +00001008 try:
Benjamin Peterson692428e2009-03-23 21:50:21 +00001009 options, args = getopt.getopt(argv[1:], 'hHvq', long_opts)
Fred Drake02538202001-03-21 18:09:46 +00001010 for opt, value in options:
1011 if opt in ('-h','-H','--help'):
1012 self.usageExit()
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001013 if opt in ('-q','--quiet'):
1014 self.verbosity = 0
1015 if opt in ('-v','--verbose'):
1016 self.verbosity = 2
Fred Drake02538202001-03-21 18:09:46 +00001017 if len(args) == 0 and self.defaultTest is None:
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001018 self.test = self.testLoader.loadTestsFromModule(self.module)
1019 return
Fred Drake02538202001-03-21 18:09:46 +00001020 if len(args) > 0:
1021 self.testNames = args
1022 else:
1023 self.testNames = (self.defaultTest,)
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001024 self.createTests()
Fred Drake02538202001-03-21 18:09:46 +00001025 except getopt.error, msg:
1026 self.usageExit(msg)
1027
1028 def createTests(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +00001029 self.test = self.testLoader.loadTestsFromNames(self.testNames,
1030 self.module)
Fred Drake02538202001-03-21 18:09:46 +00001031
1032 def runTests(self):
Georg Brandld0a96252007-03-07 09:21:06 +00001033 if isinstance(self.testRunner, (type, types.ClassType)):
1034 try:
1035 testRunner = self.testRunner(verbosity=self.verbosity)
1036 except TypeError:
1037 # didn't accept the verbosity argument
1038 testRunner = self.testRunner()
1039 else:
1040 # it is assumed to be a TestRunner instance
1041 testRunner = self.testRunner
1042 result = testRunner.run(self.test)
Tim Petersa19a1682001-03-29 04:36:09 +00001043 sys.exit(not result.wasSuccessful())
Fred Drake02538202001-03-21 18:09:46 +00001044
1045main = TestProgram
1046
1047
1048##############################################################################
1049# Executing this module from the command line
1050##############################################################################
1051
1052if __name__ == "__main__":
1053 main(module=None)