Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 2 | ''' |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 3 | Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's |
| 4 | Smalltalk testing framework. |
| 5 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 6 | This module contains the core framework classes that form the basis of |
| 7 | specific test cases and suites (TestCase, TestSuite etc.), and also a |
| 8 | text-based utility class for running the tests and reporting the results |
Jeremy Hylton | efef5da | 2001-10-22 18:14:15 +0000 | [diff] [blame] | 9 | (TextTestRunner). |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 10 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 11 | Simple 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 Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 19 | def testMultiply(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 20 | self.assertEquals((0 * 10), 0) |
| 21 | self.assertEquals((5 * 8), 40) |
| 22 | |
| 23 | if __name__ == '__main__': |
| 24 | unittest.main() |
| 25 | |
| 26 | Further information is available in the bundled documentation, and from |
| 27 | |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 28 | http://docs.python.org/lib/module-unittest.html |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 29 | |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 30 | Copyright (c) 1999-2003 Steve Purcell |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 31 | This module is free software, and you may redistribute it and/or modify |
| 32 | it under the same terms as Python itself, so long as this copyright message |
| 33 | and disclaimer are retained in their original form. |
| 34 | |
| 35 | IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, |
| 36 | SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF |
| 37 | THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 38 | DAMAGE. |
| 39 | |
| 40 | THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT |
| 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 42 | PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, |
| 43 | AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 44 | SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 45 | ''' |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 46 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 47 | __author__ = "Steve Purcell" |
| 48 | __email__ = "stephen_purcell at yahoo dot com" |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 49 | __version__ = "#Revision: 1.63 $"[11:-2] |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 50 | |
| 51 | import time |
| 52 | import sys |
| 53 | import traceback |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 54 | import os |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 55 | import types |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 56 | |
| 57 | ############################################################################## |
Steve Purcell | d75e7e4 | 2003-09-15 11:01:21 +0000 | [diff] [blame] | 58 | # Exported classes and functions |
| 59 | ############################################################################## |
| 60 | __all__ = ['TestResult', 'TestCase', 'TestSuite', 'TextTestRunner', |
| 61 | 'TestLoader', 'FunctionTestCase', 'main', 'defaultTestLoader'] |
| 62 | |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 63 | # Expose obsolete functions for backwards compatibility |
Steve Purcell | d75e7e4 | 2003-09-15 11:01:21 +0000 | [diff] [blame] | 64 | __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases']) |
| 65 | |
| 66 | |
| 67 | ############################################################################## |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 68 | # Backward compatibility |
| 69 | ############################################################################## |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 70 | |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 71 | def _CmpToKey(mycmp): |
| 72 | 'Convert a cmp= function into a key= function' |
| 73 | class K(object): |
| 74 | def __init__(self, obj): |
| 75 | self.obj = obj |
| 76 | def __lt__(self, other): |
| 77 | return mycmp(self.obj, other.obj) == -1 |
| 78 | return K |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 79 | |
| 80 | ############################################################################## |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 81 | # Test framework core |
| 82 | ############################################################################## |
| 83 | |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 84 | def _strclass(cls): |
| 85 | return "%s.%s" % (cls.__module__, cls.__name__) |
| 86 | |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 87 | __unittest = 1 |
| 88 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 89 | class TestResult(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 90 | """Holder for test result information. |
| 91 | |
| 92 | Test results are automatically managed by the TestCase and TestSuite |
| 93 | classes, and do not need to be explicitly manipulated by writers of tests. |
| 94 | |
| 95 | Each instance holds the total number of tests run, and collections of |
| 96 | failures and errors that occurred among those test runs. The collections |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 97 | contain tuples of (testcase, exceptioninfo), where exceptioninfo is the |
Fred Drake | 656f9ec | 2001-09-06 19:13:14 +0000 | [diff] [blame] | 98 | formatted traceback of the error that occurred. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 99 | """ |
| 100 | def __init__(self): |
| 101 | self.failures = [] |
| 102 | self.errors = [] |
| 103 | self.testsRun = 0 |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 104 | self.shouldStop = False |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 105 | |
| 106 | def startTest(self, test): |
| 107 | "Called when the given test is about to be run" |
| 108 | self.testsRun = self.testsRun + 1 |
| 109 | |
| 110 | def stopTest(self, test): |
| 111 | "Called when the given test has been run" |
| 112 | pass |
| 113 | |
| 114 | def addError(self, test, err): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 115 | """Called when an error has occurred. 'err' is a tuple of values as |
| 116 | returned by sys.exc_info(). |
| 117 | """ |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 118 | self.errors.append((test, self._exc_info_to_string(err, test))) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 119 | |
| 120 | def addFailure(self, test, err): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 121 | """Called when an error has occurred. 'err' is a tuple of values as |
| 122 | returned by sys.exc_info().""" |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 123 | self.failures.append((test, self._exc_info_to_string(err, test))) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 124 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 125 | def addSuccess(self, test): |
| 126 | "Called when a test has completed successfully" |
| 127 | pass |
| 128 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 129 | def wasSuccessful(self): |
| 130 | "Tells whether or not this result was a success" |
| 131 | return len(self.failures) == len(self.errors) == 0 |
| 132 | |
| 133 | def stop(self): |
| 134 | "Indicates that the tests should be aborted" |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 135 | self.shouldStop = True |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 136 | |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 137 | def _exc_info_to_string(self, err, test): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 138 | """Converts a sys.exc_info()-style tuple of values into a string.""" |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 139 | exctype, value, tb = err |
| 140 | # Skip test runner traceback levels |
| 141 | while tb and self._is_relevant_tb_level(tb): |
| 142 | tb = tb.tb_next |
| 143 | if exctype is test.failureException: |
| 144 | # Skip assert*() traceback levels |
| 145 | length = self._count_relevant_tb_levels(tb) |
| 146 | return ''.join(traceback.format_exception(exctype, value, tb, length)) |
| 147 | return ''.join(traceback.format_exception(exctype, value, tb)) |
| 148 | |
| 149 | def _is_relevant_tb_level(self, tb): |
Georg Brandl | 56af5fc | 2008-07-18 19:30:10 +0000 | [diff] [blame] | 150 | return '__unittest' in tb.tb_frame.f_globals |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 151 | |
| 152 | def _count_relevant_tb_levels(self, tb): |
| 153 | length = 0 |
| 154 | while tb and not self._is_relevant_tb_level(tb): |
| 155 | length += 1 |
| 156 | tb = tb.tb_next |
| 157 | return length |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 158 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 159 | def __repr__(self): |
| 160 | return "<%s run=%i errors=%i failures=%i>" % \ |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 161 | (_strclass(self.__class__), self.testsRun, len(self.errors), |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 162 | len(self.failures)) |
| 163 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 164 | class AssertRaisesContext(object): |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 165 | def __init__(self, expected, test_case): |
| 166 | self.expected = expected |
| 167 | self.failureException = test_case.failureException |
| 168 | def __enter__(self): |
| 169 | pass |
| 170 | def __exit__(self, exc_type, exc_value, traceback): |
| 171 | if exc_type is None: |
| 172 | try: |
| 173 | exc_name = self.expected.__name__ |
| 174 | except AttributeError: |
| 175 | exc_name = str(self.expected) |
| 176 | raise self.failureException( |
| 177 | "{0} not raised".format(exc_name)) |
| 178 | if issubclass(exc_type, self.expected): |
| 179 | return True |
| 180 | # Let unexpected exceptions skip through |
| 181 | return False |
| 182 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 183 | class TestCase(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 184 | """A class whose instances are single test cases. |
| 185 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 186 | By default, the test code itself should be placed in a method named |
| 187 | 'runTest'. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 188 | |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 189 | If the fixture may be used for many test cases, create as |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 190 | many test methods as are needed. When instantiating such a TestCase |
| 191 | subclass, specify in the constructor arguments the name of the test method |
| 192 | that the instance is to execute. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 193 | |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 194 | Test authors should subclass TestCase for their own tests. Construction |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 195 | and deconstruction of the test's environment ('fixture') can be |
| 196 | implemented by overriding the 'setUp' and 'tearDown' methods respectively. |
| 197 | |
| 198 | If it is necessary to override the __init__ method, the base class |
| 199 | __init__ method must always be called. It is important that subclasses |
| 200 | should not change the signature of their __init__ method, since instances |
| 201 | of the classes are instantiated automatically by parts of the framework |
| 202 | in order to be run. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 203 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 204 | |
| 205 | # This attribute determines which exception will be raised when |
| 206 | # the instance's assertion methods fail; test methods raising this |
| 207 | # exception will be deemed to have 'failed' rather than 'errored' |
| 208 | |
| 209 | failureException = AssertionError |
| 210 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 211 | def __init__(self, methodName='runTest'): |
| 212 | """Create an instance of the class that will use the named test |
| 213 | method when executed. Raises a ValueError if the instance does |
| 214 | not have a method with the specified name. |
| 215 | """ |
| 216 | try: |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 217 | self._testMethodName = methodName |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 218 | testMethod = getattr(self, methodName) |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 219 | self._testMethodDoc = testMethod.__doc__ |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 220 | except AttributeError: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 221 | raise ValueError("no such test method in %s: %s" % \ |
| 222 | (self.__class__, methodName)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 223 | |
| 224 | def setUp(self): |
| 225 | "Hook method for setting up the test fixture before exercising it." |
| 226 | pass |
| 227 | |
| 228 | def tearDown(self): |
| 229 | "Hook method for deconstructing the test fixture after testing it." |
| 230 | pass |
| 231 | |
| 232 | def countTestCases(self): |
| 233 | return 1 |
| 234 | |
| 235 | def defaultTestResult(self): |
| 236 | return TestResult() |
| 237 | |
| 238 | def shortDescription(self): |
| 239 | """Returns a one-line description of the test, or None if no |
| 240 | description has been provided. |
| 241 | |
| 242 | The default implementation of this method returns the first line of |
| 243 | the specified test method's docstring. |
| 244 | """ |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 245 | doc = self._testMethodDoc |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 246 | return doc and doc.split("\n")[0].strip() or None |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 247 | |
| 248 | def id(self): |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 249 | return "%s.%s" % (_strclass(self.__class__), self._testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 250 | |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 251 | def __eq__(self, other): |
| 252 | if type(self) is not type(other): |
| 253 | return False |
| 254 | |
| 255 | return self._testMethodName == other._testMethodName |
| 256 | |
| 257 | def __ne__(self, other): |
| 258 | return not self == other |
| 259 | |
| 260 | def __hash__(self): |
Collin Winter | 9453e5d | 2007-03-09 23:30:39 +0000 | [diff] [blame] | 261 | return hash((type(self), self._testMethodName)) |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 262 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 263 | def __str__(self): |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 264 | return "%s (%s)" % (self._testMethodName, _strclass(self.__class__)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 265 | |
| 266 | def __repr__(self): |
| 267 | return "<%s testMethod=%s>" % \ |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 268 | (_strclass(self.__class__), self._testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 269 | |
| 270 | def run(self, result=None): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 271 | if result is None: result = self.defaultTestResult() |
| 272 | result.startTest(self) |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 273 | testMethod = getattr(self, self._testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 274 | try: |
| 275 | try: |
| 276 | self.setUp() |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 277 | except Exception: |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 278 | result.addError(self, self._exc_info()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 279 | return |
| 280 | |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 281 | ok = False |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 282 | try: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 283 | testMethod() |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 284 | ok = True |
Skip Montanaro | ae5c37b | 2003-07-13 15:18:12 +0000 | [diff] [blame] | 285 | except self.failureException: |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 286 | result.addFailure(self, self._exc_info()) |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 287 | except Exception: |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 288 | result.addError(self, self._exc_info()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 289 | |
| 290 | try: |
| 291 | self.tearDown() |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 292 | except Exception: |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 293 | result.addError(self, self._exc_info()) |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 294 | ok = False |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 295 | if ok: result.addSuccess(self) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 296 | finally: |
| 297 | result.stopTest(self) |
| 298 | |
Raymond Hettinger | 664347b | 2004-12-04 21:21:53 +0000 | [diff] [blame] | 299 | def __call__(self, *args, **kwds): |
| 300 | return self.run(*args, **kwds) |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 301 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 302 | def debug(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 303 | """Run the test without collecting errors in a TestResult""" |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 304 | self.setUp() |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 305 | getattr(self, self._testMethodName)() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 306 | self.tearDown() |
| 307 | |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 308 | def _exc_info(self): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 309 | """Return a version of sys.exc_info() with the traceback frame |
| 310 | minimised; usually the top level of the traceback frame is not |
| 311 | needed. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 312 | """ |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 313 | return sys.exc_info() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 314 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 315 | def fail(self, msg=None): |
| 316 | """Fail immediately, with the given message.""" |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 317 | raise self.failureException(msg) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 318 | |
| 319 | def failIf(self, expr, msg=None): |
| 320 | "Fail the test if the expression is true." |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 321 | if expr: raise self.failureException(msg) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 322 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 323 | def failUnless(self, expr, msg=None): |
| 324 | """Fail the test unless the expression is true.""" |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 325 | if not expr: raise self.failureException(msg) |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 327 | def failUnlessRaises(self, excClass, callableObj=None, *args, **kwargs): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 328 | """Fail unless an exception of class excClass is thrown |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 329 | by callableObj when invoked with arguments args and keyword |
| 330 | arguments kwargs. If a different type of exception is |
| 331 | thrown, it will not be caught, and the test case will be |
| 332 | deemed to have suffered an error, exactly as for an |
| 333 | unexpected exception. |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 334 | |
| 335 | If called with callableObj omitted or None, will return a |
| 336 | context object used like this:: |
| 337 | |
| 338 | with self.failUnlessRaises(some_error_class): |
| 339 | do_something() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 340 | """ |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 341 | context = AssertRaisesContext(excClass, self) |
| 342 | if callableObj is None: |
| 343 | return context |
| 344 | with context: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 345 | callableObj(*args, **kwargs) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 346 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 347 | def failUnlessEqual(self, first, second, msg=None): |
Raymond Hettinger | c377cbf | 2003-04-04 22:56:42 +0000 | [diff] [blame] | 348 | """Fail if the two objects are unequal as determined by the '==' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 349 | operator. |
| 350 | """ |
Raymond Hettinger | c377cbf | 2003-04-04 22:56:42 +0000 | [diff] [blame] | 351 | if not first == second: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 352 | raise self.failureException(msg or '%r != %r' % (first, second)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 353 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 354 | def failIfEqual(self, first, second, msg=None): |
| 355 | """Fail if the two objects are equal as determined by the '==' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 356 | operator. |
| 357 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 358 | if first == second: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 359 | raise self.failureException(msg or '%r == %r' % (first, second)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 360 | |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 361 | def failUnlessAlmostEqual(self, first, second, places=7, msg=None): |
| 362 | """Fail if the two objects are unequal as determined by their |
| 363 | difference rounded to the given number of decimal places |
| 364 | (default 7) and comparing to zero. |
| 365 | |
Steve Purcell | 397b45d | 2003-10-26 10:41:03 +0000 | [diff] [blame] | 366 | Note that decimal places (from zero) are usually not the same |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 367 | as significant digits (measured from the most signficant digit). |
| 368 | """ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 369 | if round(abs(second-first), places) != 0: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 370 | raise self.failureException( |
| 371 | msg or '%r != %r within %r places' % (first, second, places)) |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 372 | |
| 373 | def failIfAlmostEqual(self, first, second, places=7, msg=None): |
| 374 | """Fail if the two objects are equal as determined by their |
| 375 | difference rounded to the given number of decimal places |
| 376 | (default 7) and comparing to zero. |
| 377 | |
Steve Purcell | cca3491 | 2003-10-26 16:38:16 +0000 | [diff] [blame] | 378 | Note that decimal places (from zero) are usually not the same |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 379 | as significant digits (measured from the most signficant digit). |
| 380 | """ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 381 | if round(abs(second-first), places) == 0: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 382 | raise self.failureException( |
| 383 | msg or '%r == %r within %r places' % (first, second, places)) |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 384 | |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 385 | # Synonyms for assertion methods |
| 386 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 387 | assertEqual = assertEquals = failUnlessEqual |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 388 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 389 | assertNotEqual = assertNotEquals = failIfEqual |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 390 | |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 391 | assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual |
| 392 | |
| 393 | assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual |
| 394 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 395 | assertRaises = failUnlessRaises |
| 396 | |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 397 | assert_ = assertTrue = failUnless |
| 398 | |
| 399 | assertFalse = failIf |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 400 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 401 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 403 | class TestSuite(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 404 | """A test suite is a composite test consisting of a number of TestCases. |
| 405 | |
| 406 | For use, create an instance of TestSuite, then add test case instances. |
| 407 | When all tests have been added, the suite can be passed to a test |
| 408 | runner, such as TextTestRunner. It will run the individual test cases |
| 409 | in the order in which they were added, aggregating the results. When |
| 410 | subclassing, do not forget to call the base class constructor. |
| 411 | """ |
| 412 | def __init__(self, tests=()): |
| 413 | self._tests = [] |
| 414 | self.addTests(tests) |
| 415 | |
| 416 | def __repr__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 417 | return "<%s tests=%s>" % (_strclass(self.__class__), self._tests) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 418 | |
| 419 | __str__ = __repr__ |
| 420 | |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 421 | def __eq__(self, other): |
| 422 | if type(self) is not type(other): |
| 423 | return False |
| 424 | return self._tests == other._tests |
| 425 | |
| 426 | def __ne__(self, other): |
| 427 | return not self == other |
| 428 | |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 429 | # Can't guarantee hash invariant, so flag as unhashable |
| 430 | __hash__ = None |
| 431 | |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 432 | def __iter__(self): |
| 433 | return iter(self._tests) |
| 434 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 435 | def countTestCases(self): |
| 436 | cases = 0 |
| 437 | for test in self._tests: |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 438 | cases += test.countTestCases() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 439 | return cases |
| 440 | |
| 441 | def addTest(self, test): |
Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 442 | # sanity checks |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 443 | if not hasattr(test, '__call__'): |
Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 444 | raise TypeError("the test to add must be callable") |
| 445 | if (isinstance(test, (type, types.ClassType)) and |
| 446 | issubclass(test, (TestCase, TestSuite))): |
| 447 | raise TypeError("TestCases and TestSuites must be instantiated " |
| 448 | "before passing them to addTest()") |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 449 | self._tests.append(test) |
| 450 | |
| 451 | def addTests(self, tests): |
Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 452 | if isinstance(tests, basestring): |
| 453 | raise TypeError("tests must be an iterable of tests, not a string") |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 454 | for test in tests: |
| 455 | self.addTest(test) |
| 456 | |
| 457 | def run(self, result): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 458 | for test in self._tests: |
| 459 | if result.shouldStop: |
| 460 | break |
| 461 | test(result) |
| 462 | return result |
| 463 | |
Raymond Hettinger | 664347b | 2004-12-04 21:21:53 +0000 | [diff] [blame] | 464 | def __call__(self, *args, **kwds): |
| 465 | return self.run(*args, **kwds) |
| 466 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 467 | def debug(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 468 | """Run the tests without collecting errors in a TestResult""" |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 469 | for test in self._tests: test.debug() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 470 | |
| 471 | |
| 472 | class FunctionTestCase(TestCase): |
| 473 | """A test case that wraps a test function. |
| 474 | |
| 475 | This is useful for slipping pre-existing test functions into the |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 476 | unittest framework. Optionally, set-up and tidy-up functions can be |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 477 | supplied. As with TestCase, the tidy-up ('tearDown') function will |
| 478 | always be called if the set-up ('setUp') function ran successfully. |
| 479 | """ |
| 480 | |
| 481 | def __init__(self, testFunc, setUp=None, tearDown=None, |
| 482 | description=None): |
| 483 | TestCase.__init__(self) |
| 484 | self.__setUpFunc = setUp |
| 485 | self.__tearDownFunc = tearDown |
| 486 | self.__testFunc = testFunc |
| 487 | self.__description = description |
| 488 | |
| 489 | def setUp(self): |
| 490 | if self.__setUpFunc is not None: |
| 491 | self.__setUpFunc() |
| 492 | |
| 493 | def tearDown(self): |
| 494 | if self.__tearDownFunc is not None: |
| 495 | self.__tearDownFunc() |
| 496 | |
| 497 | def runTest(self): |
| 498 | self.__testFunc() |
| 499 | |
| 500 | def id(self): |
| 501 | return self.__testFunc.__name__ |
| 502 | |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 503 | def __eq__(self, other): |
| 504 | if type(self) is not type(other): |
| 505 | return False |
| 506 | |
| 507 | return self.__setUpFunc == other.__setUpFunc and \ |
| 508 | self.__tearDownFunc == other.__tearDownFunc and \ |
| 509 | self.__testFunc == other.__testFunc and \ |
| 510 | self.__description == other.__description |
| 511 | |
| 512 | def __ne__(self, other): |
| 513 | return not self == other |
| 514 | |
| 515 | def __hash__(self): |
Collin Winter | 9453e5d | 2007-03-09 23:30:39 +0000 | [diff] [blame] | 516 | return hash((type(self), self.__setUpFunc, self.__tearDownFunc, |
| 517 | self.__testFunc, self.__description)) |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 518 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 519 | def __str__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 520 | return "%s (%s)" % (_strclass(self.__class__), self.__testFunc.__name__) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 521 | |
| 522 | def __repr__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 523 | return "<%s testFunc=%s>" % (_strclass(self.__class__), self.__testFunc) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 524 | |
| 525 | def shortDescription(self): |
| 526 | if self.__description is not None: return self.__description |
| 527 | doc = self.__testFunc.__doc__ |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 528 | return doc and doc.split("\n")[0].strip() or None |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 529 | |
| 530 | |
| 531 | |
| 532 | ############################################################################## |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 533 | # Locating and loading tests |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 534 | ############################################################################## |
| 535 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 536 | class TestLoader(object): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 537 | """This class is responsible for loading tests according to various |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 538 | criteria and returning them wrapped in a TestSuite |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 539 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 540 | testMethodPrefix = 'test' |
| 541 | sortTestMethodsUsing = cmp |
| 542 | suiteClass = TestSuite |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 543 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 544 | def loadTestsFromTestCase(self, testCaseClass): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 545 | """Return a suite of all tests cases contained in testCaseClass""" |
Johannes Gijsbers | d7b6ad4 | 2004-11-07 15:46:25 +0000 | [diff] [blame] | 546 | if issubclass(testCaseClass, TestSuite): |
| 547 | raise TypeError("Test cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?") |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 548 | testCaseNames = self.getTestCaseNames(testCaseClass) |
| 549 | if not testCaseNames and hasattr(testCaseClass, 'runTest'): |
| 550 | testCaseNames = ['runTest'] |
| 551 | return self.suiteClass(map(testCaseClass, testCaseNames)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 552 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 553 | def loadTestsFromModule(self, module): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 554 | """Return a suite of all tests cases contained in the given module""" |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 555 | tests = [] |
| 556 | for name in dir(module): |
| 557 | obj = getattr(module, name) |
Guido van Rossum | 6791137 | 2002-09-30 19:25:56 +0000 | [diff] [blame] | 558 | if (isinstance(obj, (type, types.ClassType)) and |
| 559 | issubclass(obj, TestCase)): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 560 | tests.append(self.loadTestsFromTestCase(obj)) |
| 561 | return self.suiteClass(tests) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 562 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 563 | def loadTestsFromName(self, name, module=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 564 | """Return a suite of all tests cases given a string specifier. |
| 565 | |
| 566 | The name may resolve either to a module, a test case class, a |
| 567 | test method within a test case class, or a callable object which |
| 568 | returns a TestCase or TestSuite instance. |
Tim Peters | 613b222 | 2001-04-13 05:37:27 +0000 | [diff] [blame] | 569 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 570 | The method optionally resolves the names relative to a given module. |
| 571 | """ |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 572 | parts = name.split('.') |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 573 | if module is None: |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 574 | parts_copy = parts[:] |
| 575 | while parts_copy: |
| 576 | try: |
| 577 | module = __import__('.'.join(parts_copy)) |
| 578 | break |
| 579 | except ImportError: |
| 580 | del parts_copy[-1] |
| 581 | if not parts_copy: raise |
Armin Rigo | 1b3c04b | 2003-10-24 17:15:29 +0000 | [diff] [blame] | 582 | parts = parts[1:] |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 583 | obj = module |
| 584 | for part in parts: |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 585 | parent, obj = obj, getattr(obj, part) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 586 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 587 | if isinstance(obj, types.ModuleType): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 588 | return self.loadTestsFromModule(obj) |
Guido van Rossum | 6791137 | 2002-09-30 19:25:56 +0000 | [diff] [blame] | 589 | elif (isinstance(obj, (type, types.ClassType)) and |
Steve Purcell | 397b45d | 2003-10-26 10:41:03 +0000 | [diff] [blame] | 590 | issubclass(obj, TestCase)): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 591 | return self.loadTestsFromTestCase(obj) |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 592 | elif (isinstance(obj, types.UnboundMethodType) and |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 593 | isinstance(parent, (type, types.ClassType)) and |
| 594 | issubclass(parent, TestCase)): |
| 595 | return TestSuite([parent(obj.__name__)]) |
Steve Purcell | 397b45d | 2003-10-26 10:41:03 +0000 | [diff] [blame] | 596 | elif isinstance(obj, TestSuite): |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 597 | return obj |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 598 | elif hasattr(obj, '__call__'): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 599 | test = obj() |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 600 | if isinstance(test, TestSuite): |
| 601 | return test |
| 602 | elif isinstance(test, TestCase): |
| 603 | return TestSuite([test]) |
| 604 | else: |
| 605 | raise TypeError("calling %s returned %s, not a test" % |
| 606 | (obj, test)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 607 | else: |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 608 | raise TypeError("don't know how to make test from: %s" % obj) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 609 | |
| 610 | def loadTestsFromNames(self, names, module=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 611 | """Return a suite of all tests cases found using the given sequence |
| 612 | of string specifiers. See 'loadTestsFromName()'. |
| 613 | """ |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 614 | suites = [self.loadTestsFromName(name, module) for name in names] |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 615 | return self.suiteClass(suites) |
| 616 | |
| 617 | def getTestCaseNames(self, testCaseClass): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 618 | """Return a sorted sequence of method names found within testCaseClass |
| 619 | """ |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 620 | def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix): |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 621 | return attrname.startswith(prefix) and hasattr(getattr(testCaseClass, attrname), '__call__') |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 622 | testFnNames = filter(isTestMethod, dir(testCaseClass)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 623 | if self.sortTestMethodsUsing: |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 624 | testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 625 | return testFnNames |
| 626 | |
| 627 | |
| 628 | |
| 629 | defaultTestLoader = TestLoader() |
| 630 | |
| 631 | |
| 632 | ############################################################################## |
| 633 | # Patches for old functions: these functions should be considered obsolete |
| 634 | ############################################################################## |
| 635 | |
| 636 | def _makeLoader(prefix, sortUsing, suiteClass=None): |
| 637 | loader = TestLoader() |
| 638 | loader.sortTestMethodsUsing = sortUsing |
| 639 | loader.testMethodPrefix = prefix |
| 640 | if suiteClass: loader.suiteClass = suiteClass |
| 641 | return loader |
| 642 | |
| 643 | def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp): |
| 644 | return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) |
| 645 | |
| 646 | def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite): |
| 647 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass) |
| 648 | |
| 649 | def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite): |
| 650 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 651 | |
| 652 | |
| 653 | ############################################################################## |
| 654 | # Text UI |
| 655 | ############################################################################## |
| 656 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 657 | class _WritelnDecorator(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 658 | """Used to decorate file-like objects with a handy 'writeln' method""" |
| 659 | def __init__(self,stream): |
| 660 | self.stream = stream |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 661 | |
| 662 | def __getattr__(self, attr): |
| 663 | return getattr(self.stream,attr) |
| 664 | |
Raymond Hettinger | 91dd19d | 2003-09-13 02:58:00 +0000 | [diff] [blame] | 665 | def writeln(self, arg=None): |
| 666 | if arg: self.write(arg) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 667 | self.write('\n') # text-mode streams translate to \r\n if needed |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 668 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 669 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 670 | class _TextTestResult(TestResult): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 671 | """A test result class that can print formatted text results to a stream. |
| 672 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 673 | Used by TextTestRunner. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 674 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 675 | separator1 = '=' * 70 |
| 676 | separator2 = '-' * 70 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 677 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 678 | def __init__(self, stream, descriptions, verbosity): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 679 | TestResult.__init__(self) |
| 680 | self.stream = stream |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 681 | self.showAll = verbosity > 1 |
| 682 | self.dots = verbosity == 1 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 683 | self.descriptions = descriptions |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 684 | |
| 685 | def getDescription(self, test): |
| 686 | if self.descriptions: |
| 687 | return test.shortDescription() or str(test) |
| 688 | else: |
| 689 | return str(test) |
| 690 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 691 | def startTest(self, test): |
| 692 | TestResult.startTest(self, test) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 693 | if self.showAll: |
| 694 | self.stream.write(self.getDescription(test)) |
| 695 | self.stream.write(" ... ") |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 696 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 697 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 698 | def addSuccess(self, test): |
| 699 | TestResult.addSuccess(self, test) |
| 700 | if self.showAll: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 701 | self.stream.writeln("ok") |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 702 | elif self.dots: |
| 703 | self.stream.write('.') |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 704 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 705 | |
| 706 | def addError(self, test, err): |
| 707 | TestResult.addError(self, test, err) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 708 | if self.showAll: |
| 709 | self.stream.writeln("ERROR") |
| 710 | elif self.dots: |
| 711 | self.stream.write('E') |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 712 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 713 | |
| 714 | def addFailure(self, test, err): |
| 715 | TestResult.addFailure(self, test, err) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 716 | if self.showAll: |
| 717 | self.stream.writeln("FAIL") |
| 718 | elif self.dots: |
| 719 | self.stream.write('F') |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 720 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 721 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 722 | def printErrors(self): |
| 723 | if self.dots or self.showAll: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 724 | self.stream.writeln() |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 725 | self.printErrorList('ERROR', self.errors) |
| 726 | self.printErrorList('FAIL', self.failures) |
| 727 | |
| 728 | def printErrorList(self, flavour, errors): |
| 729 | for test, err in errors: |
| 730 | self.stream.writeln(self.separator1) |
| 731 | self.stream.writeln("%s: %s" % (flavour,self.getDescription(test))) |
| 732 | self.stream.writeln(self.separator2) |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 733 | self.stream.writeln("%s" % err) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 734 | |
| 735 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 736 | class TextTestRunner(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 737 | """A test runner class that displays results in textual form. |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 738 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 739 | It prints out the names of tests as they are run, errors as they |
| 740 | occur, and a summary of the results at the end of the test run. |
| 741 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 742 | def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 743 | self.stream = _WritelnDecorator(stream) |
| 744 | self.descriptions = descriptions |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 745 | self.verbosity = verbosity |
| 746 | |
| 747 | def _makeResult(self): |
| 748 | return _TextTestResult(self.stream, self.descriptions, self.verbosity) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 749 | |
| 750 | def run(self, test): |
| 751 | "Run the given test case or test suite." |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 752 | result = self._makeResult() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 753 | startTime = time.time() |
| 754 | test(result) |
| 755 | stopTime = time.time() |
Steve Purcell | 397b45d | 2003-10-26 10:41:03 +0000 | [diff] [blame] | 756 | timeTaken = stopTime - startTime |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 757 | result.printErrors() |
| 758 | self.stream.writeln(result.separator2) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 759 | run = result.testsRun |
| 760 | self.stream.writeln("Ran %d test%s in %.3fs" % |
Neal Norwitz | 7616504 | 2002-05-31 14:15:11 +0000 | [diff] [blame] | 761 | (run, run != 1 and "s" or "", timeTaken)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 762 | self.stream.writeln() |
| 763 | if not result.wasSuccessful(): |
| 764 | self.stream.write("FAILED (") |
| 765 | failed, errored = map(len, (result.failures, result.errors)) |
| 766 | if failed: |
| 767 | self.stream.write("failures=%d" % failed) |
| 768 | if errored: |
| 769 | if failed: self.stream.write(", ") |
| 770 | self.stream.write("errors=%d" % errored) |
| 771 | self.stream.writeln(")") |
| 772 | else: |
| 773 | self.stream.writeln("OK") |
| 774 | return result |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 775 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 776 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 777 | |
| 778 | ############################################################################## |
| 779 | # Facilities for running tests from the command line |
| 780 | ############################################################################## |
| 781 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 782 | class TestProgram(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 783 | """A command-line program that runs a set of tests; this is primarily |
| 784 | for making test modules conveniently executable. |
| 785 | """ |
| 786 | USAGE = """\ |
Steve Purcell | 17a781b | 2001-04-09 15:37:31 +0000 | [diff] [blame] | 787 | Usage: %(progName)s [options] [test] [...] |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 788 | |
| 789 | Options: |
| 790 | -h, --help Show this message |
| 791 | -v, --verbose Verbose output |
| 792 | -q, --quiet Minimal output |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 793 | |
| 794 | Examples: |
| 795 | %(progName)s - run default set of tests |
| 796 | %(progName)s MyTestSuite - run suite 'MyTestSuite' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 797 | %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething |
| 798 | %(progName)s MyTestCase - run all 'test*' test methods |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 799 | in MyTestCase |
| 800 | """ |
| 801 | def __init__(self, module='__main__', defaultTest=None, |
Georg Brandl | d0a9625 | 2007-03-07 09:21:06 +0000 | [diff] [blame] | 802 | argv=None, testRunner=TextTestRunner, |
| 803 | testLoader=defaultTestLoader): |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 804 | if isinstance(module, basestring): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 805 | self.module = __import__(module) |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 806 | for part in module.split('.')[1:]: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 807 | self.module = getattr(self.module, part) |
| 808 | else: |
| 809 | self.module = module |
| 810 | if argv is None: |
| 811 | argv = sys.argv |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 812 | self.verbosity = 1 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 813 | self.defaultTest = defaultTest |
| 814 | self.testRunner = testRunner |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 815 | self.testLoader = testLoader |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 816 | self.progName = os.path.basename(argv[0]) |
| 817 | self.parseArgs(argv) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 818 | self.runTests() |
| 819 | |
| 820 | def usageExit(self, msg=None): |
| 821 | if msg: print msg |
| 822 | print self.USAGE % self.__dict__ |
| 823 | sys.exit(2) |
| 824 | |
| 825 | def parseArgs(self, argv): |
| 826 | import getopt |
| 827 | try: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 828 | options, args = getopt.getopt(argv[1:], 'hHvq', |
| 829 | ['help','verbose','quiet']) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 830 | for opt, value in options: |
| 831 | if opt in ('-h','-H','--help'): |
| 832 | self.usageExit() |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 833 | if opt in ('-q','--quiet'): |
| 834 | self.verbosity = 0 |
| 835 | if opt in ('-v','--verbose'): |
| 836 | self.verbosity = 2 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 837 | if len(args) == 0 and self.defaultTest is None: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 838 | self.test = self.testLoader.loadTestsFromModule(self.module) |
| 839 | return |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 840 | if len(args) > 0: |
| 841 | self.testNames = args |
| 842 | else: |
| 843 | self.testNames = (self.defaultTest,) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 844 | self.createTests() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 845 | except getopt.error, msg: |
| 846 | self.usageExit(msg) |
| 847 | |
| 848 | def createTests(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 849 | self.test = self.testLoader.loadTestsFromNames(self.testNames, |
| 850 | self.module) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 851 | |
| 852 | def runTests(self): |
Georg Brandl | d0a9625 | 2007-03-07 09:21:06 +0000 | [diff] [blame] | 853 | if isinstance(self.testRunner, (type, types.ClassType)): |
| 854 | try: |
| 855 | testRunner = self.testRunner(verbosity=self.verbosity) |
| 856 | except TypeError: |
| 857 | # didn't accept the verbosity argument |
| 858 | testRunner = self.testRunner() |
| 859 | else: |
| 860 | # it is assumed to be a TestRunner instance |
| 861 | testRunner = self.testRunner |
| 862 | result = testRunner.run(self.test) |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 863 | sys.exit(not result.wasSuccessful()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 864 | |
| 865 | main = TestProgram |
| 866 | |
| 867 | |
| 868 | ############################################################################## |
| 869 | # Executing this module from the command line |
| 870 | ############################################################################## |
| 871 | |
| 872 | if __name__ == "__main__": |
| 873 | main(module=None) |