blob: 4538e302d597cf0a6ba2c00bb2923a9d318967f5 [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
Georg Brandl15c5ce92007-03-07 09:09:40 +000028 http://docs.python.org/lib/module-unittest.html
Steve Purcell5ddd1a82001-03-22 08:45:36 +000029
Steve Purcell7e743842003-09-22 11:08:12 +000030Copyright (c) 1999-2003 Steve Purcell
Fred Drake02538202001-03-21 18:09:46 +000031This module is free software, and you may redistribute it and/or modify
32it under the same terms as Python itself, so long as this copyright message
33and disclaimer are retained in their original form.
34
35IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
36SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
37THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
38DAMAGE.
39
40THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
41LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
42PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
43AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
44SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Steve Purcell5ddd1a82001-03-22 08:45:36 +000045'''
Fred Drake02538202001-03-21 18:09:46 +000046
Steve Purcell5ddd1a82001-03-22 08:45:36 +000047__author__ = "Steve Purcell"
48__email__ = "stephen_purcell at yahoo dot com"
Steve Purcellb8d5f242003-12-06 13:03:13 +000049__version__ = "#Revision: 1.63 $"[11:-2]
Fred Drake02538202001-03-21 18:09:46 +000050
51import time
52import sys
53import traceback
Fred Drake02538202001-03-21 18:09:46 +000054import os
Steve Purcell5ddd1a82001-03-22 08:45:36 +000055import types
Fred Drake02538202001-03-21 18:09:46 +000056
57##############################################################################
Steve Purcelld75e7e42003-09-15 11:01:21 +000058# Exported classes and functions
59##############################################################################
60__all__ = ['TestResult', 'TestCase', 'TestSuite', 'TextTestRunner',
61 'TestLoader', 'FunctionTestCase', 'main', 'defaultTestLoader']
62
Steve Purcell7e743842003-09-22 11:08:12 +000063# Expose obsolete functions for backwards compatibility
Steve Purcelld75e7e42003-09-15 11:01:21 +000064__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
65
66
67##############################################################################
Steve Purcell7e743842003-09-22 11:08:12 +000068# Backward compatibility
69##############################################################################
70if sys.version_info[:2] < (2, 2):
Steve Purcell7e743842003-09-22 11:08:12 +000071 def isinstance(obj, clsinfo):
72 import __builtin__
Raymond Hettingerf7153662005-02-07 14:16:21 +000073 if type(clsinfo) in (tuple, list):
Steve Purcell7e743842003-09-22 11:08:12 +000074 for cls in clsinfo:
75 if cls is type: cls = types.ClassType
76 if __builtin__.isinstance(obj, cls):
77 return 1
78 return 0
79 else: return __builtin__.isinstance(obj, clsinfo)
80
Raymond Hettinger5930d8f2008-07-10 16:06:41 +000081def _CmpToKey(mycmp):
82 'Convert a cmp= function into a key= function'
83 class K(object):
84 def __init__(self, obj):
85 self.obj = obj
86 def __lt__(self, other):
87 return mycmp(self.obj, other.obj) == -1
88 return K
Steve Purcell7e743842003-09-22 11:08:12 +000089
90##############################################################################
Fred Drake02538202001-03-21 18:09:46 +000091# Test framework core
92##############################################################################
93
Steve Purcell824574d2002-08-08 13:38:02 +000094# All classes defined herein are 'new-style' classes, allowing use of 'super()'
95__metaclass__ = type
96
Steve Purcelldc391a62002-08-09 09:46:23 +000097def _strclass(cls):
98 return "%s.%s" % (cls.__module__, cls.__name__)
99
Steve Purcellb8d5f242003-12-06 13:03:13 +0000100__unittest = 1
101
Fred Drake02538202001-03-21 18:09:46 +0000102class TestResult:
103 """Holder for test result information.
104
105 Test results are automatically managed by the TestCase and TestSuite
106 classes, and do not need to be explicitly manipulated by writers of tests.
107
108 Each instance holds the total number of tests run, and collections of
109 failures and errors that occurred among those test runs. The collections
Steve Purcell7b065702001-09-06 08:24:40 +0000110 contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
Fred Drake656f9ec2001-09-06 19:13:14 +0000111 formatted traceback of the error that occurred.
Fred Drake02538202001-03-21 18:09:46 +0000112 """
113 def __init__(self):
114 self.failures = []
115 self.errors = []
116 self.testsRun = 0
Georg Brandl15c5ce92007-03-07 09:09:40 +0000117 self.shouldStop = False
Fred Drake02538202001-03-21 18:09:46 +0000118
119 def startTest(self, test):
120 "Called when the given test is about to be run"
121 self.testsRun = self.testsRun + 1
122
123 def stopTest(self, test):
124 "Called when the given test has been run"
125 pass
126
127 def addError(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000128 """Called when an error has occurred. 'err' is a tuple of values as
129 returned by sys.exc_info().
130 """
Steve Purcellb8d5f242003-12-06 13:03:13 +0000131 self.errors.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000132
133 def addFailure(self, test, err):
Steve Purcell7b065702001-09-06 08:24:40 +0000134 """Called when an error has occurred. 'err' is a tuple of values as
135 returned by sys.exc_info()."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000136 self.failures.append((test, self._exc_info_to_string(err, test)))
Fred Drake02538202001-03-21 18:09:46 +0000137
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000138 def addSuccess(self, test):
139 "Called when a test has completed successfully"
140 pass
141
Fred Drake02538202001-03-21 18:09:46 +0000142 def wasSuccessful(self):
143 "Tells whether or not this result was a success"
144 return len(self.failures) == len(self.errors) == 0
145
146 def stop(self):
147 "Indicates that the tests should be aborted"
Steve Purcell7e743842003-09-22 11:08:12 +0000148 self.shouldStop = True
Tim Petersa19a1682001-03-29 04:36:09 +0000149
Steve Purcellb8d5f242003-12-06 13:03:13 +0000150 def _exc_info_to_string(self, err, test):
Steve Purcell7b065702001-09-06 08:24:40 +0000151 """Converts a sys.exc_info()-style tuple of values into a string."""
Steve Purcellb8d5f242003-12-06 13:03:13 +0000152 exctype, value, tb = err
153 # Skip test runner traceback levels
154 while tb and self._is_relevant_tb_level(tb):
155 tb = tb.tb_next
156 if exctype is test.failureException:
157 # Skip assert*() traceback levels
158 length = self._count_relevant_tb_levels(tb)
159 return ''.join(traceback.format_exception(exctype, value, tb, length))
160 return ''.join(traceback.format_exception(exctype, value, tb))
161
162 def _is_relevant_tb_level(self, tb):
Georg Brandl56af5fc2008-07-18 19:30:10 +0000163 return '__unittest' in tb.tb_frame.f_globals
Steve Purcellb8d5f242003-12-06 13:03:13 +0000164
165 def _count_relevant_tb_levels(self, tb):
166 length = 0
167 while tb and not self._is_relevant_tb_level(tb):
168 length += 1
169 tb = tb.tb_next
170 return length
Steve Purcell7b065702001-09-06 08:24:40 +0000171
Fred Drake02538202001-03-21 18:09:46 +0000172 def __repr__(self):
173 return "<%s run=%i errors=%i failures=%i>" % \
Steve Purcelldc391a62002-08-09 09:46:23 +0000174 (_strclass(self.__class__), self.testsRun, len(self.errors),
Fred Drake02538202001-03-21 18:09:46 +0000175 len(self.failures))
176
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000177class AssertRaisesContext:
178 def __init__(self, expected, test_case):
179 self.expected = expected
180 self.failureException = test_case.failureException
181 def __enter__(self):
182 pass
183 def __exit__(self, exc_type, exc_value, traceback):
184 if exc_type is None:
185 try:
186 exc_name = self.expected.__name__
187 except AttributeError:
188 exc_name = str(self.expected)
189 raise self.failureException(
190 "{0} not raised".format(exc_name))
191 if issubclass(exc_type, self.expected):
192 return True
193 # Let unexpected exceptions skip through
194 return False
195
Fred Drake02538202001-03-21 18:09:46 +0000196class TestCase:
197 """A class whose instances are single test cases.
198
Fred Drake02538202001-03-21 18:09:46 +0000199 By default, the test code itself should be placed in a method named
200 'runTest'.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000201
Tim Petersa19a1682001-03-29 04:36:09 +0000202 If the fixture may be used for many test cases, create as
Fred Drake02538202001-03-21 18:09:46 +0000203 many test methods as are needed. When instantiating such a TestCase
204 subclass, specify in the constructor arguments the name of the test method
205 that the instance is to execute.
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000206
Tim Petersa19a1682001-03-29 04:36:09 +0000207 Test authors should subclass TestCase for their own tests. Construction
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000208 and deconstruction of the test's environment ('fixture') can be
209 implemented by overriding the 'setUp' and 'tearDown' methods respectively.
210
211 If it is necessary to override the __init__ method, the base class
212 __init__ method must always be called. It is important that subclasses
213 should not change the signature of their __init__ method, since instances
214 of the classes are instantiated automatically by parts of the framework
215 in order to be run.
Fred Drake02538202001-03-21 18:09:46 +0000216 """
Steve Purcell15d89272001-04-12 09:05:01 +0000217
218 # This attribute determines which exception will be raised when
219 # the instance's assertion methods fail; test methods raising this
220 # exception will be deemed to have 'failed' rather than 'errored'
221
222 failureException = AssertionError
223
Fred Drake02538202001-03-21 18:09:46 +0000224 def __init__(self, methodName='runTest'):
225 """Create an instance of the class that will use the named test
226 method when executed. Raises a ValueError if the instance does
227 not have a method with the specified name.
228 """
229 try:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000230 self._testMethodName = methodName
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000231 testMethod = getattr(self, methodName)
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000232 self._testMethodDoc = testMethod.__doc__
Fred Drake02538202001-03-21 18:09:46 +0000233 except AttributeError:
234 raise ValueError, "no such test method in %s: %s" % \
235 (self.__class__, methodName)
236
237 def setUp(self):
238 "Hook method for setting up the test fixture before exercising it."
239 pass
240
241 def tearDown(self):
242 "Hook method for deconstructing the test fixture after testing it."
243 pass
244
245 def countTestCases(self):
246 return 1
247
248 def defaultTestResult(self):
249 return TestResult()
250
251 def shortDescription(self):
252 """Returns a one-line description of the test, or None if no
253 description has been provided.
254
255 The default implementation of this method returns the first line of
256 the specified test method's docstring.
257 """
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000258 doc = self._testMethodDoc
Steve Purcell7e743842003-09-22 11:08:12 +0000259 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000260
261 def id(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000262 return "%s.%s" % (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000263
Georg Brandl15c5ce92007-03-07 09:09:40 +0000264 def __eq__(self, other):
265 if type(self) is not type(other):
266 return False
267
268 return self._testMethodName == other._testMethodName
269
270 def __ne__(self, other):
271 return not self == other
272
273 def __hash__(self):
Collin Winter9453e5d2007-03-09 23:30:39 +0000274 return hash((type(self), self._testMethodName))
Georg Brandl15c5ce92007-03-07 09:09:40 +0000275
Fred Drake02538202001-03-21 18:09:46 +0000276 def __str__(self):
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000277 return "%s (%s)" % (self._testMethodName, _strclass(self.__class__))
Fred Drake02538202001-03-21 18:09:46 +0000278
279 def __repr__(self):
280 return "<%s testMethod=%s>" % \
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000281 (_strclass(self.__class__), self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000282
283 def run(self, result=None):
Fred Drake02538202001-03-21 18:09:46 +0000284 if result is None: result = self.defaultTestResult()
285 result.startTest(self)
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000286 testMethod = getattr(self, self._testMethodName)
Fred Drake02538202001-03-21 18:09:46 +0000287 try:
288 try:
289 self.setUp()
Guido van Rossum202dd1e2001-12-07 03:39:34 +0000290 except KeyboardInterrupt:
291 raise
Fred Drake02538202001-03-21 18:09:46 +0000292 except:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000293 result.addError(self, self._exc_info())
Fred Drake02538202001-03-21 18:09:46 +0000294 return
295
Steve Purcell7e743842003-09-22 11:08:12 +0000296 ok = False
Fred Drake02538202001-03-21 18:09:46 +0000297 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000298 testMethod()
Steve Purcell7e743842003-09-22 11:08:12 +0000299 ok = True
Skip Montanaroae5c37b2003-07-13 15:18:12 +0000300 except self.failureException:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000301 result.addFailure(self, self._exc_info())
Guido van Rossum202dd1e2001-12-07 03:39:34 +0000302 except KeyboardInterrupt:
303 raise
Fred Drake02538202001-03-21 18:09:46 +0000304 except:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000305 result.addError(self, self._exc_info())
Fred Drake02538202001-03-21 18:09:46 +0000306
307 try:
308 self.tearDown()
Guido van Rossum202dd1e2001-12-07 03:39:34 +0000309 except KeyboardInterrupt:
310 raise
Fred Drake02538202001-03-21 18:09:46 +0000311 except:
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000312 result.addError(self, self._exc_info())
Steve Purcell7e743842003-09-22 11:08:12 +0000313 ok = False
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000314 if ok: result.addSuccess(self)
Fred Drake02538202001-03-21 18:09:46 +0000315 finally:
316 result.stopTest(self)
317
Raymond Hettinger664347b2004-12-04 21:21:53 +0000318 def __call__(self, *args, **kwds):
319 return self.run(*args, **kwds)
Steve Purcell7e743842003-09-22 11:08:12 +0000320
Fred Drake02538202001-03-21 18:09:46 +0000321 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000322 """Run the test without collecting errors in a TestResult"""
Fred Drake02538202001-03-21 18:09:46 +0000323 self.setUp()
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000324 getattr(self, self._testMethodName)()
Fred Drake02538202001-03-21 18:09:46 +0000325 self.tearDown()
326
Georg Brandl81cdb4e2006-01-20 17:55:00 +0000327 def _exc_info(self):
Steve Purcell15d89272001-04-12 09:05:01 +0000328 """Return a version of sys.exc_info() with the traceback frame
329 minimised; usually the top level of the traceback frame is not
330 needed.
Fred Drake02538202001-03-21 18:09:46 +0000331 """
Georg Brandl15c5ce92007-03-07 09:09:40 +0000332 return sys.exc_info()
Fred Drake02538202001-03-21 18:09:46 +0000333
Steve Purcell15d89272001-04-12 09:05:01 +0000334 def fail(self, msg=None):
335 """Fail immediately, with the given message."""
336 raise self.failureException, msg
Fred Drake02538202001-03-21 18:09:46 +0000337
338 def failIf(self, expr, msg=None):
339 "Fail the test if the expression is true."
Steve Purcell15d89272001-04-12 09:05:01 +0000340 if expr: raise self.failureException, msg
Fred Drake02538202001-03-21 18:09:46 +0000341
Steve Purcell15d89272001-04-12 09:05:01 +0000342 def failUnless(self, expr, msg=None):
343 """Fail the test unless the expression is true."""
344 if not expr: raise self.failureException, msg
345
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000346 def failUnlessRaises(self, excClass, callableObj=None, *args, **kwargs):
Steve Purcell15d89272001-04-12 09:05:01 +0000347 """Fail unless an exception of class excClass is thrown
Fred Drake02538202001-03-21 18:09:46 +0000348 by callableObj when invoked with arguments args and keyword
349 arguments kwargs. If a different type of exception is
350 thrown, it will not be caught, and the test case will be
351 deemed to have suffered an error, exactly as for an
352 unexpected exception.
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000353
354 If called with callableObj omitted or None, will return a
355 context object used like this::
356
357 with self.failUnlessRaises(some_error_class):
358 do_something()
Fred Drake02538202001-03-21 18:09:46 +0000359 """
Antoine Pitrou697ca3d2008-12-28 14:09:36 +0000360 context = AssertRaisesContext(excClass, self)
361 if callableObj is None:
362 return context
363 with context:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000364 callableObj(*args, **kwargs)
Fred Drake02538202001-03-21 18:09:46 +0000365
Steve Purcell15d89272001-04-12 09:05:01 +0000366 def failUnlessEqual(self, first, second, msg=None):
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000367 """Fail if the two objects are unequal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000368 operator.
369 """
Raymond Hettingerc377cbf2003-04-04 22:56:42 +0000370 if not first == second:
Steve Purcellca9aaf32001-12-17 10:13:17 +0000371 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000372 (msg or '%r != %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000373
Steve Purcell15d89272001-04-12 09:05:01 +0000374 def failIfEqual(self, first, second, msg=None):
375 """Fail if the two objects are equal as determined by the '=='
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000376 operator.
377 """
Steve Purcell15d89272001-04-12 09:05:01 +0000378 if first == second:
Steve Purcellca9aaf32001-12-17 10:13:17 +0000379 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000380 (msg or '%r == %r' % (first, second))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000381
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000382 def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
383 """Fail if the two objects are unequal as determined by their
384 difference rounded to the given number of decimal places
385 (default 7) and comparing to zero.
386
Steve Purcell397b45d2003-10-26 10:41:03 +0000387 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000388 as significant digits (measured from the most signficant digit).
389 """
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000390 if round(abs(second-first), places) != 0:
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000391 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000392 (msg or '%r != %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000393
394 def failIfAlmostEqual(self, first, second, places=7, msg=None):
395 """Fail if the two objects are equal as determined by their
396 difference rounded to the given number of decimal places
397 (default 7) and comparing to zero.
398
Steve Purcellcca34912003-10-26 16:38:16 +0000399 Note that decimal places (from zero) are usually not the same
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000400 as significant digits (measured from the most signficant digit).
401 """
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000402 if round(abs(second-first), places) == 0:
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000403 raise self.failureException, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000404 (msg or '%r == %r within %r places' % (first, second, places))
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000405
Steve Purcell7e743842003-09-22 11:08:12 +0000406 # Synonyms for assertion methods
407
Steve Purcell15d89272001-04-12 09:05:01 +0000408 assertEqual = assertEquals = failUnlessEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000409
Steve Purcell15d89272001-04-12 09:05:01 +0000410 assertNotEqual = assertNotEquals = failIfEqual
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000411
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000412 assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual
413
414 assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual
415
Steve Purcell15d89272001-04-12 09:05:01 +0000416 assertRaises = failUnlessRaises
417
Steve Purcell7e743842003-09-22 11:08:12 +0000418 assert_ = assertTrue = failUnless
419
420 assertFalse = failIf
Steve Purcell15d89272001-04-12 09:05:01 +0000421
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000422
Fred Drake02538202001-03-21 18:09:46 +0000423
424class TestSuite:
425 """A test suite is a composite test consisting of a number of TestCases.
426
427 For use, create an instance of TestSuite, then add test case instances.
428 When all tests have been added, the suite can be passed to a test
429 runner, such as TextTestRunner. It will run the individual test cases
430 in the order in which they were added, aggregating the results. When
431 subclassing, do not forget to call the base class constructor.
432 """
433 def __init__(self, tests=()):
434 self._tests = []
435 self.addTests(tests)
436
437 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000438 return "<%s tests=%s>" % (_strclass(self.__class__), self._tests)
Fred Drake02538202001-03-21 18:09:46 +0000439
440 __str__ = __repr__
441
Georg Brandl15c5ce92007-03-07 09:09:40 +0000442 def __eq__(self, other):
443 if type(self) is not type(other):
444 return False
445 return self._tests == other._tests
446
447 def __ne__(self, other):
448 return not self == other
449
Nick Coghlan48361f52008-08-11 15:45:58 +0000450 # Can't guarantee hash invariant, so flag as unhashable
451 __hash__ = None
452
Jim Fultonfafd8742004-08-28 15:22:12 +0000453 def __iter__(self):
454 return iter(self._tests)
455
Fred Drake02538202001-03-21 18:09:46 +0000456 def countTestCases(self):
457 cases = 0
458 for test in self._tests:
Steve Purcell7e743842003-09-22 11:08:12 +0000459 cases += test.countTestCases()
Fred Drake02538202001-03-21 18:09:46 +0000460 return cases
461
462 def addTest(self, test):
Georg Brandld9e50262007-03-07 11:54:49 +0000463 # sanity checks
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000464 if not hasattr(test, '__call__'):
Georg Brandld9e50262007-03-07 11:54:49 +0000465 raise TypeError("the test to add must be callable")
466 if (isinstance(test, (type, types.ClassType)) and
467 issubclass(test, (TestCase, TestSuite))):
468 raise TypeError("TestCases and TestSuites must be instantiated "
469 "before passing them to addTest()")
Fred Drake02538202001-03-21 18:09:46 +0000470 self._tests.append(test)
471
472 def addTests(self, tests):
Georg Brandld9e50262007-03-07 11:54:49 +0000473 if isinstance(tests, basestring):
474 raise TypeError("tests must be an iterable of tests, not a string")
Fred Drake02538202001-03-21 18:09:46 +0000475 for test in tests:
476 self.addTest(test)
477
478 def run(self, result):
Fred Drake02538202001-03-21 18:09:46 +0000479 for test in self._tests:
480 if result.shouldStop:
481 break
482 test(result)
483 return result
484
Raymond Hettinger664347b2004-12-04 21:21:53 +0000485 def __call__(self, *args, **kwds):
486 return self.run(*args, **kwds)
487
Fred Drake02538202001-03-21 18:09:46 +0000488 def debug(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000489 """Run the tests without collecting errors in a TestResult"""
Fred Drake02538202001-03-21 18:09:46 +0000490 for test in self._tests: test.debug()
Fred Drake02538202001-03-21 18:09:46 +0000491
492
493class FunctionTestCase(TestCase):
494 """A test case that wraps a test function.
495
496 This is useful for slipping pre-existing test functions into the
Georg Brandl15c5ce92007-03-07 09:09:40 +0000497 unittest framework. Optionally, set-up and tidy-up functions can be
Fred Drake02538202001-03-21 18:09:46 +0000498 supplied. As with TestCase, the tidy-up ('tearDown') function will
499 always be called if the set-up ('setUp') function ran successfully.
500 """
501
502 def __init__(self, testFunc, setUp=None, tearDown=None,
503 description=None):
504 TestCase.__init__(self)
505 self.__setUpFunc = setUp
506 self.__tearDownFunc = tearDown
507 self.__testFunc = testFunc
508 self.__description = description
509
510 def setUp(self):
511 if self.__setUpFunc is not None:
512 self.__setUpFunc()
513
514 def tearDown(self):
515 if self.__tearDownFunc is not None:
516 self.__tearDownFunc()
517
518 def runTest(self):
519 self.__testFunc()
520
521 def id(self):
522 return self.__testFunc.__name__
523
Georg Brandl15c5ce92007-03-07 09:09:40 +0000524 def __eq__(self, other):
525 if type(self) is not type(other):
526 return False
527
528 return self.__setUpFunc == other.__setUpFunc and \
529 self.__tearDownFunc == other.__tearDownFunc and \
530 self.__testFunc == other.__testFunc and \
531 self.__description == other.__description
532
533 def __ne__(self, other):
534 return not self == other
535
536 def __hash__(self):
Collin Winter9453e5d2007-03-09 23:30:39 +0000537 return hash((type(self), self.__setUpFunc, self.__tearDownFunc,
538 self.__testFunc, self.__description))
Georg Brandl15c5ce92007-03-07 09:09:40 +0000539
Fred Drake02538202001-03-21 18:09:46 +0000540 def __str__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000541 return "%s (%s)" % (_strclass(self.__class__), self.__testFunc.__name__)
Fred Drake02538202001-03-21 18:09:46 +0000542
543 def __repr__(self):
Steve Purcelldc391a62002-08-09 09:46:23 +0000544 return "<%s testFunc=%s>" % (_strclass(self.__class__), self.__testFunc)
Fred Drake02538202001-03-21 18:09:46 +0000545
546 def shortDescription(self):
547 if self.__description is not None: return self.__description
548 doc = self.__testFunc.__doc__
Steve Purcell7e743842003-09-22 11:08:12 +0000549 return doc and doc.split("\n")[0].strip() or None
Fred Drake02538202001-03-21 18:09:46 +0000550
551
552
553##############################################################################
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000554# Locating and loading tests
Fred Drake02538202001-03-21 18:09:46 +0000555##############################################################################
556
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000557class TestLoader:
558 """This class is responsible for loading tests according to various
Georg Brandl15c5ce92007-03-07 09:09:40 +0000559 criteria and returning them wrapped in a TestSuite
Fred Drake02538202001-03-21 18:09:46 +0000560 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000561 testMethodPrefix = 'test'
562 sortTestMethodsUsing = cmp
563 suiteClass = TestSuite
Fred Drake02538202001-03-21 18:09:46 +0000564
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000565 def loadTestsFromTestCase(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000566 """Return a suite of all tests cases contained in testCaseClass"""
Johannes Gijsbersd7b6ad42004-11-07 15:46:25 +0000567 if issubclass(testCaseClass, TestSuite):
568 raise TypeError("Test cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?")
Steve Purcell7e743842003-09-22 11:08:12 +0000569 testCaseNames = self.getTestCaseNames(testCaseClass)
570 if not testCaseNames and hasattr(testCaseClass, 'runTest'):
571 testCaseNames = ['runTest']
572 return self.suiteClass(map(testCaseClass, testCaseNames))
Fred Drake02538202001-03-21 18:09:46 +0000573
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000574 def loadTestsFromModule(self, module):
Steve Purcell15d89272001-04-12 09:05:01 +0000575 """Return a suite of all tests cases contained in the given module"""
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000576 tests = []
577 for name in dir(module):
578 obj = getattr(module, name)
Guido van Rossum67911372002-09-30 19:25:56 +0000579 if (isinstance(obj, (type, types.ClassType)) and
580 issubclass(obj, TestCase)):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000581 tests.append(self.loadTestsFromTestCase(obj))
582 return self.suiteClass(tests)
Fred Drake02538202001-03-21 18:09:46 +0000583
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000584 def loadTestsFromName(self, name, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000585 """Return a suite of all tests cases given a string specifier.
586
587 The name may resolve either to a module, a test case class, a
588 test method within a test case class, or a callable object which
589 returns a TestCase or TestSuite instance.
Tim Peters613b2222001-04-13 05:37:27 +0000590
Steve Purcell15d89272001-04-12 09:05:01 +0000591 The method optionally resolves the names relative to a given module.
592 """
Steve Purcell7e743842003-09-22 11:08:12 +0000593 parts = name.split('.')
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000594 if module is None:
Steve Purcell7e743842003-09-22 11:08:12 +0000595 parts_copy = parts[:]
596 while parts_copy:
597 try:
598 module = __import__('.'.join(parts_copy))
599 break
600 except ImportError:
601 del parts_copy[-1]
602 if not parts_copy: raise
Armin Rigo1b3c04b2003-10-24 17:15:29 +0000603 parts = parts[1:]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000604 obj = module
605 for part in parts:
Steve Purcell7e743842003-09-22 11:08:12 +0000606 parent, obj = obj, getattr(obj, part)
Fred Drake02538202001-03-21 18:09:46 +0000607
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000608 if type(obj) == types.ModuleType:
609 return self.loadTestsFromModule(obj)
Guido van Rossum67911372002-09-30 19:25:56 +0000610 elif (isinstance(obj, (type, types.ClassType)) and
Steve Purcell397b45d2003-10-26 10:41:03 +0000611 issubclass(obj, TestCase)):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000612 return self.loadTestsFromTestCase(obj)
Georg Brandl15c5ce92007-03-07 09:09:40 +0000613 elif (type(obj) == types.UnboundMethodType and
614 isinstance(parent, (type, types.ClassType)) and
615 issubclass(parent, TestCase)):
616 return TestSuite([parent(obj.__name__)])
Steve Purcell397b45d2003-10-26 10:41:03 +0000617 elif isinstance(obj, TestSuite):
Steve Purcell7e743842003-09-22 11:08:12 +0000618 return obj
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000619 elif hasattr(obj, '__call__'):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000620 test = obj()
Georg Brandl15c5ce92007-03-07 09:09:40 +0000621 if isinstance(test, TestSuite):
622 return test
623 elif isinstance(test, TestCase):
624 return TestSuite([test])
625 else:
626 raise TypeError("calling %s returned %s, not a test" %
627 (obj, test))
Fred Drake02538202001-03-21 18:09:46 +0000628 else:
Georg Brandl15c5ce92007-03-07 09:09:40 +0000629 raise TypeError("don't know how to make test from: %s" % obj)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000630
631 def loadTestsFromNames(self, names, module=None):
Steve Purcell15d89272001-04-12 09:05:01 +0000632 """Return a suite of all tests cases found using the given sequence
633 of string specifiers. See 'loadTestsFromName()'.
634 """
Steve Purcell7e743842003-09-22 11:08:12 +0000635 suites = [self.loadTestsFromName(name, module) for name in names]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000636 return self.suiteClass(suites)
637
638 def getTestCaseNames(self, testCaseClass):
Steve Purcell15d89272001-04-12 09:05:01 +0000639 """Return a sorted sequence of method names found within testCaseClass
640 """
Steve Purcell7e743842003-09-22 11:08:12 +0000641 def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix):
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000642 return attrname.startswith(prefix) and hasattr(getattr(testCaseClass, attrname), '__call__')
Steve Purcell7e743842003-09-22 11:08:12 +0000643 testFnNames = filter(isTestMethod, dir(testCaseClass))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000644 if self.sortTestMethodsUsing:
Raymond Hettinger5930d8f2008-07-10 16:06:41 +0000645 testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000646 return testFnNames
647
648
649
650defaultTestLoader = TestLoader()
651
652
653##############################################################################
654# Patches for old functions: these functions should be considered obsolete
655##############################################################################
656
657def _makeLoader(prefix, sortUsing, suiteClass=None):
658 loader = TestLoader()
659 loader.sortTestMethodsUsing = sortUsing
660 loader.testMethodPrefix = prefix
661 if suiteClass: loader.suiteClass = suiteClass
662 return loader
663
664def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp):
665 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
666
667def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
668 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
669
670def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
671 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module)
Fred Drake02538202001-03-21 18:09:46 +0000672
673
674##############################################################################
675# Text UI
676##############################################################################
677
678class _WritelnDecorator:
679 """Used to decorate file-like objects with a handy 'writeln' method"""
680 def __init__(self,stream):
681 self.stream = stream
Fred Drake02538202001-03-21 18:09:46 +0000682
683 def __getattr__(self, attr):
684 return getattr(self.stream,attr)
685
Raymond Hettinger91dd19d2003-09-13 02:58:00 +0000686 def writeln(self, arg=None):
687 if arg: self.write(arg)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000688 self.write('\n') # text-mode streams translate to \r\n if needed
Tim Petersa19a1682001-03-29 04:36:09 +0000689
Fred Drake02538202001-03-21 18:09:46 +0000690
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000691class _TextTestResult(TestResult):
Fred Drake02538202001-03-21 18:09:46 +0000692 """A test result class that can print formatted text results to a stream.
693
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000694 Used by TextTestRunner.
Fred Drake02538202001-03-21 18:09:46 +0000695 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000696 separator1 = '=' * 70
697 separator2 = '-' * 70
Fred Drake02538202001-03-21 18:09:46 +0000698
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000699 def __init__(self, stream, descriptions, verbosity):
Fred Drake02538202001-03-21 18:09:46 +0000700 TestResult.__init__(self)
701 self.stream = stream
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000702 self.showAll = verbosity > 1
703 self.dots = verbosity == 1
Fred Drake02538202001-03-21 18:09:46 +0000704 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000705
706 def getDescription(self, test):
707 if self.descriptions:
708 return test.shortDescription() or str(test)
709 else:
710 return str(test)
711
Fred Drake02538202001-03-21 18:09:46 +0000712 def startTest(self, test):
713 TestResult.startTest(self, test)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000714 if self.showAll:
715 self.stream.write(self.getDescription(test))
716 self.stream.write(" ... ")
Georg Brandld0632402008-05-11 15:17:41 +0000717 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000718
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000719 def addSuccess(self, test):
720 TestResult.addSuccess(self, test)
721 if self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000722 self.stream.writeln("ok")
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000723 elif self.dots:
724 self.stream.write('.')
Georg Brandld0632402008-05-11 15:17:41 +0000725 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000726
727 def addError(self, test, err):
728 TestResult.addError(self, test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000729 if self.showAll:
730 self.stream.writeln("ERROR")
731 elif self.dots:
732 self.stream.write('E')
Georg Brandld0632402008-05-11 15:17:41 +0000733 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000734
735 def addFailure(self, test, err):
736 TestResult.addFailure(self, test, err)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000737 if self.showAll:
738 self.stream.writeln("FAIL")
739 elif self.dots:
740 self.stream.write('F')
Georg Brandld0632402008-05-11 15:17:41 +0000741 self.stream.flush()
Fred Drake02538202001-03-21 18:09:46 +0000742
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000743 def printErrors(self):
744 if self.dots or self.showAll:
Fred Drake02538202001-03-21 18:09:46 +0000745 self.stream.writeln()
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000746 self.printErrorList('ERROR', self.errors)
747 self.printErrorList('FAIL', self.failures)
748
749 def printErrorList(self, flavour, errors):
750 for test, err in errors:
751 self.stream.writeln(self.separator1)
752 self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
753 self.stream.writeln(self.separator2)
Steve Purcell7b065702001-09-06 08:24:40 +0000754 self.stream.writeln("%s" % err)
Fred Drake02538202001-03-21 18:09:46 +0000755
756
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000757class TextTestRunner:
Fred Drake02538202001-03-21 18:09:46 +0000758 """A test runner class that displays results in textual form.
Tim Petersa19a1682001-03-29 04:36:09 +0000759
Fred Drake02538202001-03-21 18:09:46 +0000760 It prints out the names of tests as they are run, errors as they
761 occur, and a summary of the results at the end of the test run.
762 """
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000763 def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1):
Fred Drake02538202001-03-21 18:09:46 +0000764 self.stream = _WritelnDecorator(stream)
765 self.descriptions = descriptions
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000766 self.verbosity = verbosity
767
768 def _makeResult(self):
769 return _TextTestResult(self.stream, self.descriptions, self.verbosity)
Fred Drake02538202001-03-21 18:09:46 +0000770
771 def run(self, test):
772 "Run the given test case or test suite."
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000773 result = self._makeResult()
Fred Drake02538202001-03-21 18:09:46 +0000774 startTime = time.time()
775 test(result)
776 stopTime = time.time()
Steve Purcell397b45d2003-10-26 10:41:03 +0000777 timeTaken = stopTime - startTime
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000778 result.printErrors()
779 self.stream.writeln(result.separator2)
Fred Drake02538202001-03-21 18:09:46 +0000780 run = result.testsRun
781 self.stream.writeln("Ran %d test%s in %.3fs" %
Neal Norwitz76165042002-05-31 14:15:11 +0000782 (run, run != 1 and "s" or "", timeTaken))
Fred Drake02538202001-03-21 18:09:46 +0000783 self.stream.writeln()
784 if not result.wasSuccessful():
785 self.stream.write("FAILED (")
786 failed, errored = map(len, (result.failures, result.errors))
787 if failed:
788 self.stream.write("failures=%d" % failed)
789 if errored:
790 if failed: self.stream.write(", ")
791 self.stream.write("errors=%d" % errored)
792 self.stream.writeln(")")
793 else:
794 self.stream.writeln("OK")
795 return result
Tim Petersa19a1682001-03-29 04:36:09 +0000796
Fred Drake02538202001-03-21 18:09:46 +0000797
Fred Drake02538202001-03-21 18:09:46 +0000798
799##############################################################################
800# Facilities for running tests from the command line
801##############################################################################
802
803class TestProgram:
804 """A command-line program that runs a set of tests; this is primarily
805 for making test modules conveniently executable.
806 """
807 USAGE = """\
Steve Purcell17a781b2001-04-09 15:37:31 +0000808Usage: %(progName)s [options] [test] [...]
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000809
810Options:
811 -h, --help Show this message
812 -v, --verbose Verbose output
813 -q, --quiet Minimal output
Fred Drake02538202001-03-21 18:09:46 +0000814
815Examples:
816 %(progName)s - run default set of tests
817 %(progName)s MyTestSuite - run suite 'MyTestSuite'
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000818 %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
819 %(progName)s MyTestCase - run all 'test*' test methods
Fred Drake02538202001-03-21 18:09:46 +0000820 in MyTestCase
821"""
822 def __init__(self, module='__main__', defaultTest=None,
Georg Brandld0a96252007-03-07 09:21:06 +0000823 argv=None, testRunner=TextTestRunner,
824 testLoader=defaultTestLoader):
Fred Drake02538202001-03-21 18:09:46 +0000825 if type(module) == type(''):
826 self.module = __import__(module)
Steve Purcell7e743842003-09-22 11:08:12 +0000827 for part in module.split('.')[1:]:
Fred Drake02538202001-03-21 18:09:46 +0000828 self.module = getattr(self.module, part)
829 else:
830 self.module = module
831 if argv is None:
832 argv = sys.argv
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000833 self.verbosity = 1
Fred Drake02538202001-03-21 18:09:46 +0000834 self.defaultTest = defaultTest
835 self.testRunner = testRunner
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000836 self.testLoader = testLoader
Fred Drake02538202001-03-21 18:09:46 +0000837 self.progName = os.path.basename(argv[0])
838 self.parseArgs(argv)
Fred Drake02538202001-03-21 18:09:46 +0000839 self.runTests()
840
841 def usageExit(self, msg=None):
842 if msg: print msg
843 print self.USAGE % self.__dict__
844 sys.exit(2)
845
846 def parseArgs(self, argv):
847 import getopt
848 try:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000849 options, args = getopt.getopt(argv[1:], 'hHvq',
850 ['help','verbose','quiet'])
Fred Drake02538202001-03-21 18:09:46 +0000851 for opt, value in options:
852 if opt in ('-h','-H','--help'):
853 self.usageExit()
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000854 if opt in ('-q','--quiet'):
855 self.verbosity = 0
856 if opt in ('-v','--verbose'):
857 self.verbosity = 2
Fred Drake02538202001-03-21 18:09:46 +0000858 if len(args) == 0 and self.defaultTest is None:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000859 self.test = self.testLoader.loadTestsFromModule(self.module)
860 return
Fred Drake02538202001-03-21 18:09:46 +0000861 if len(args) > 0:
862 self.testNames = args
863 else:
864 self.testNames = (self.defaultTest,)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000865 self.createTests()
Fred Drake02538202001-03-21 18:09:46 +0000866 except getopt.error, msg:
867 self.usageExit(msg)
868
869 def createTests(self):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000870 self.test = self.testLoader.loadTestsFromNames(self.testNames,
871 self.module)
Fred Drake02538202001-03-21 18:09:46 +0000872
873 def runTests(self):
Georg Brandld0a96252007-03-07 09:21:06 +0000874 if isinstance(self.testRunner, (type, types.ClassType)):
875 try:
876 testRunner = self.testRunner(verbosity=self.verbosity)
877 except TypeError:
878 # didn't accept the verbosity argument
879 testRunner = self.testRunner()
880 else:
881 # it is assumed to be a TestRunner instance
882 testRunner = self.testRunner
883 result = testRunner.run(self.test)
Tim Petersa19a1682001-03-29 04:36:09 +0000884 sys.exit(not result.wasSuccessful())
Fred Drake02538202001-03-21 18:09:46 +0000885
886main = TestProgram
887
888
889##############################################################################
890# Executing this module from the command line
891##############################################################################
892
893if __name__ == "__main__":
894 main(module=None)