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*' |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 17 | self.assertEqual((1 + 2), 3) |
| 18 | self.assertEqual(0 + 1, 1) |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 19 | def testMultiply(self): |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 20 | self.assertEqual((0 * 10), 0) |
| 21 | self.assertEqual((5 * 8), 40) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | if __name__ == '__main__': |
| 24 | unittest.main() |
| 25 | |
| 26 | Further information is available in the bundled documentation, and from |
| 27 | |
Benjamin Peterson | 4e4de33 | 2009-03-24 00:37:12 +0000 | [diff] [blame] | 28 | http://docs.python.org/library/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 |
Benjamin Peterson | 4e4de33 | 2009-03-24 00:37:12 +0000 | [diff] [blame] | 31 | Copyright (c) 2003-2009 Python Software Foundation |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 32 | This module is free software, and you may redistribute it and/or modify |
| 33 | it under the same terms as Python itself, so long as this copyright message |
| 34 | and disclaimer are retained in their original form. |
| 35 | |
| 36 | IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, |
| 37 | SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF |
| 38 | THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 39 | DAMAGE. |
| 40 | |
| 41 | THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT |
| 42 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 43 | PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, |
| 44 | AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 45 | SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 46 | ''' |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 47 | |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 48 | import difflib |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 49 | import functools |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 50 | import os |
| 51 | import pprint |
| 52 | import re |
| 53 | import sys |
| 54 | import time |
| 55 | import traceback |
| 56 | import types |
Gregory P. Smith | 65ff005 | 2009-03-31 19:59:14 +0000 | [diff] [blame^] | 57 | import warnings |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 58 | |
| 59 | ############################################################################## |
Steve Purcell | d75e7e4 | 2003-09-15 11:01:21 +0000 | [diff] [blame] | 60 | # Exported classes and functions |
| 61 | ############################################################################## |
Benjamin Peterson | c750d4d | 2009-03-24 00:39:24 +0000 | [diff] [blame] | 62 | __all__ = ['TestResult', 'TestCase', 'TestSuite', 'ClassTestSuite', |
| 63 | 'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main', |
Benjamin Peterson | 0371548 | 2009-03-24 01:11:37 +0000 | [diff] [blame] | 64 | 'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless', |
Benjamin Peterson | c750d4d | 2009-03-24 00:39:24 +0000 | [diff] [blame] | 65 | 'expectedFailure'] |
Steve Purcell | d75e7e4 | 2003-09-15 11:01:21 +0000 | [diff] [blame] | 66 | |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 67 | # Expose obsolete functions for backwards compatibility |
Steve Purcell | d75e7e4 | 2003-09-15 11:01:21 +0000 | [diff] [blame] | 68 | __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases']) |
| 69 | |
| 70 | |
| 71 | ############################################################################## |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 72 | # Backward compatibility |
| 73 | ############################################################################## |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 74 | |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 75 | def _CmpToKey(mycmp): |
| 76 | 'Convert a cmp= function into a key= function' |
| 77 | class K(object): |
| 78 | def __init__(self, obj): |
| 79 | self.obj = obj |
| 80 | def __lt__(self, other): |
| 81 | return mycmp(self.obj, other.obj) == -1 |
| 82 | return K |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 83 | |
| 84 | ############################################################################## |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 85 | # Test framework core |
| 86 | ############################################################################## |
| 87 | |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 88 | def _strclass(cls): |
| 89 | return "%s.%s" % (cls.__module__, cls.__name__) |
| 90 | |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 91 | |
| 92 | class SkipTest(Exception): |
| 93 | """ |
| 94 | Raise this exception in a test to skip it. |
| 95 | |
| 96 | Usually you can use TestResult.skip() or one of the skipping decorators |
| 97 | instead of raising this directly. |
| 98 | """ |
| 99 | pass |
| 100 | |
| 101 | class _ExpectedFailure(Exception): |
| 102 | """ |
| 103 | Raise this when a test is expected to fail. |
| 104 | |
| 105 | This is an implementation detail. |
| 106 | """ |
| 107 | |
| 108 | def __init__(self, exc_info): |
| 109 | super(_ExpectedFailure, self).__init__() |
| 110 | self.exc_info = exc_info |
| 111 | |
| 112 | class _UnexpectedSuccess(Exception): |
| 113 | """ |
| 114 | The test was supposed to fail, but it didn't! |
| 115 | """ |
| 116 | pass |
| 117 | |
| 118 | def _id(obj): |
| 119 | return obj |
| 120 | |
| 121 | def skip(reason): |
| 122 | """ |
| 123 | Unconditionally skip a test. |
| 124 | """ |
| 125 | def decorator(test_item): |
| 126 | if isinstance(test_item, type) and issubclass(test_item, TestCase): |
| 127 | test_item.__unittest_skip__ = True |
| 128 | test_item.__unittest_skip_why__ = reason |
| 129 | return test_item |
| 130 | @functools.wraps(test_item) |
| 131 | def skip_wrapper(*args, **kwargs): |
| 132 | raise SkipTest(reason) |
| 133 | return skip_wrapper |
| 134 | return decorator |
| 135 | |
| 136 | def skipIf(condition, reason): |
| 137 | """ |
| 138 | Skip a test if the condition is true. |
| 139 | """ |
| 140 | if condition: |
| 141 | return skip(reason) |
| 142 | return _id |
| 143 | |
| 144 | def skipUnless(condition, reason): |
| 145 | """ |
| 146 | Skip a test unless the condition is true. |
| 147 | """ |
| 148 | if not condition: |
| 149 | return skip(reason) |
| 150 | return _id |
| 151 | |
| 152 | |
| 153 | def expectedFailure(func): |
| 154 | @functools.wraps(func) |
| 155 | def wrapper(*args, **kwargs): |
| 156 | try: |
| 157 | func(*args, **kwargs) |
| 158 | except Exception: |
| 159 | raise _ExpectedFailure(sys.exc_info()) |
| 160 | raise _UnexpectedSuccess |
| 161 | return wrapper |
| 162 | |
| 163 | |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 164 | __unittest = 1 |
| 165 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 166 | class TestResult(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 167 | """Holder for test result information. |
| 168 | |
| 169 | Test results are automatically managed by the TestCase and TestSuite |
| 170 | classes, and do not need to be explicitly manipulated by writers of tests. |
| 171 | |
| 172 | Each instance holds the total number of tests run, and collections of |
| 173 | failures and errors that occurred among those test runs. The collections |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 174 | contain tuples of (testcase, exceptioninfo), where exceptioninfo is the |
Fred Drake | 656f9ec | 2001-09-06 19:13:14 +0000 | [diff] [blame] | 175 | formatted traceback of the error that occurred. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 176 | """ |
| 177 | def __init__(self): |
| 178 | self.failures = [] |
| 179 | self.errors = [] |
| 180 | self.testsRun = 0 |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 181 | self.skipped = [] |
Benjamin Peterson | cb2b0e4 | 2009-03-23 22:29:45 +0000 | [diff] [blame] | 182 | self.expectedFailures = [] |
| 183 | self.unexpectedSuccesses = [] |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 184 | self.shouldStop = False |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 185 | |
| 186 | def startTest(self, test): |
| 187 | "Called when the given test is about to be run" |
| 188 | self.testsRun = self.testsRun + 1 |
| 189 | |
| 190 | def stopTest(self, test): |
| 191 | "Called when the given test has been run" |
| 192 | pass |
| 193 | |
| 194 | def addError(self, test, err): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 195 | """Called when an error has occurred. 'err' is a tuple of values as |
| 196 | returned by sys.exc_info(). |
| 197 | """ |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 198 | self.errors.append((test, self._exc_info_to_string(err, test))) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 199 | |
| 200 | def addFailure(self, test, err): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 201 | """Called when an error has occurred. 'err' is a tuple of values as |
| 202 | returned by sys.exc_info().""" |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 203 | self.failures.append((test, self._exc_info_to_string(err, test))) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 204 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 205 | def addSuccess(self, test): |
| 206 | "Called when a test has completed successfully" |
| 207 | pass |
| 208 | |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 209 | def addSkip(self, test, reason): |
| 210 | """Called when a test is skipped.""" |
| 211 | self.skipped.append((test, reason)) |
| 212 | |
| 213 | def addExpectedFailure(self, test, err): |
| 214 | """Called when an expected failure/error occured.""" |
Benjamin Peterson | cb2b0e4 | 2009-03-23 22:29:45 +0000 | [diff] [blame] | 215 | self.expectedFailures.append( |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 216 | (test, self._exc_info_to_string(err, test))) |
| 217 | |
| 218 | def addUnexpectedSuccess(self, test): |
| 219 | """Called when a test was expected to fail, but succeed.""" |
Benjamin Peterson | cb2b0e4 | 2009-03-23 22:29:45 +0000 | [diff] [blame] | 220 | self.unexpectedSuccesses.append(test) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 221 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 222 | def wasSuccessful(self): |
| 223 | "Tells whether or not this result was a success" |
| 224 | return len(self.failures) == len(self.errors) == 0 |
| 225 | |
| 226 | def stop(self): |
| 227 | "Indicates that the tests should be aborted" |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 228 | self.shouldStop = True |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 229 | |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 230 | def _exc_info_to_string(self, err, test): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 231 | """Converts a sys.exc_info()-style tuple of values into a string.""" |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 232 | exctype, value, tb = err |
| 233 | # Skip test runner traceback levels |
| 234 | while tb and self._is_relevant_tb_level(tb): |
| 235 | tb = tb.tb_next |
| 236 | if exctype is test.failureException: |
| 237 | # Skip assert*() traceback levels |
| 238 | length = self._count_relevant_tb_levels(tb) |
| 239 | return ''.join(traceback.format_exception(exctype, value, tb, length)) |
| 240 | return ''.join(traceback.format_exception(exctype, value, tb)) |
| 241 | |
| 242 | def _is_relevant_tb_level(self, tb): |
Georg Brandl | 56af5fc | 2008-07-18 19:30:10 +0000 | [diff] [blame] | 243 | return '__unittest' in tb.tb_frame.f_globals |
Steve Purcell | b8d5f24 | 2003-12-06 13:03:13 +0000 | [diff] [blame] | 244 | |
| 245 | def _count_relevant_tb_levels(self, tb): |
| 246 | length = 0 |
| 247 | while tb and not self._is_relevant_tb_level(tb): |
| 248 | length += 1 |
| 249 | tb = tb.tb_next |
| 250 | return length |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 251 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 252 | def __repr__(self): |
| 253 | return "<%s run=%i errors=%i failures=%i>" % \ |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 254 | (_strclass(self.__class__), self.testsRun, len(self.errors), |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 255 | len(self.failures)) |
| 256 | |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 257 | |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 258 | class _AssertRaisesContext(object): |
| 259 | """A context manager used to implement TestCase.assertRaises* methods.""" |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 260 | |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 261 | def __init__(self, expected, test_case, expected_regexp=None): |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 262 | self.expected = expected |
| 263 | self.failureException = test_case.failureException |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 264 | self.expected_regex = expected_regexp |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 266 | def __enter__(self): |
| 267 | pass |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 269 | def __exit__(self, exc_type, exc_value, traceback): |
| 270 | if exc_type is None: |
| 271 | try: |
| 272 | exc_name = self.expected.__name__ |
| 273 | except AttributeError: |
| 274 | exc_name = str(self.expected) |
| 275 | raise self.failureException( |
| 276 | "{0} not raised".format(exc_name)) |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 277 | if not issubclass(exc_type, self.expected): |
| 278 | # let unexpexted exceptions pass through |
| 279 | return False |
| 280 | if self.expected_regex is None: |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 281 | return True |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 282 | |
| 283 | expected_regexp = self.expected_regex |
| 284 | if isinstance(expected_regexp, basestring): |
| 285 | expected_regexp = re.compile(expected_regexp) |
| 286 | if not expected_regexp.search(str(exc_value)): |
| 287 | raise self.failureException('"%s" does not match "%s"' % |
| 288 | (expected_regexp.pattern, str(exc_value))) |
| 289 | return True |
| 290 | |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 291 | |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 292 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 293 | class TestCase(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 294 | """A class whose instances are single test cases. |
| 295 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 296 | By default, the test code itself should be placed in a method named |
| 297 | 'runTest'. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 298 | |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 299 | If the fixture may be used for many test cases, create as |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 300 | many test methods as are needed. When instantiating such a TestCase |
| 301 | subclass, specify in the constructor arguments the name of the test method |
| 302 | that the instance is to execute. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 303 | |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 304 | Test authors should subclass TestCase for their own tests. Construction |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 305 | and deconstruction of the test's environment ('fixture') can be |
| 306 | implemented by overriding the 'setUp' and 'tearDown' methods respectively. |
| 307 | |
| 308 | If it is necessary to override the __init__ method, the base class |
| 309 | __init__ method must always be called. It is important that subclasses |
| 310 | should not change the signature of their __init__ method, since instances |
| 311 | of the classes are instantiated automatically by parts of the framework |
| 312 | in order to be run. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 313 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 314 | |
| 315 | # This attribute determines which exception will be raised when |
| 316 | # the instance's assertion methods fail; test methods raising this |
| 317 | # exception will be deemed to have 'failed' rather than 'errored' |
| 318 | |
| 319 | failureException = AssertionError |
| 320 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 321 | def __init__(self, methodName='runTest'): |
| 322 | """Create an instance of the class that will use the named test |
| 323 | method when executed. Raises a ValueError if the instance does |
| 324 | not have a method with the specified name. |
| 325 | """ |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 326 | self._testMethodName = methodName |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 327 | try: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 328 | testMethod = getattr(self, methodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 329 | except AttributeError: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 330 | raise ValueError("no such test method in %s: %s" % \ |
| 331 | (self.__class__, methodName)) |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 332 | self._testMethodDoc = testMethod.__doc__ |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 333 | |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 334 | # Map types to custom assertEqual functions that will compare |
| 335 | # instances of said type in more detail to generate a more useful |
| 336 | # error message. |
| 337 | self.__type_equality_funcs = {} |
| 338 | self.addTypeEqualityFunc(dict, self.assertDictEqual) |
| 339 | self.addTypeEqualityFunc(list, self.assertListEqual) |
| 340 | self.addTypeEqualityFunc(tuple, self.assertTupleEqual) |
| 341 | self.addTypeEqualityFunc(set, self.assertSetEqual) |
| 342 | self.addTypeEqualityFunc(frozenset, self.assertSetEqual) |
| 343 | |
| 344 | def addTypeEqualityFunc(self, typeobj, function): |
| 345 | """Add a type specific assertEqual style function to compare a type. |
| 346 | |
| 347 | This method is for use by TestCase subclasses that need to register |
| 348 | their own type equality functions to provide nicer error messages. |
| 349 | |
| 350 | Args: |
| 351 | typeobj: The data type to call this function on when both values |
| 352 | are of the same type in assertEqual(). |
| 353 | function: The callable taking two arguments and an optional |
| 354 | msg= argument that raises self.failureException with a |
| 355 | useful error message when the two arguments are not equal. |
| 356 | """ |
| 357 | self.__type_equality_funcs[typeobj] = function |
| 358 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 359 | def setUp(self): |
| 360 | "Hook method for setting up the test fixture before exercising it." |
| 361 | pass |
| 362 | |
| 363 | def tearDown(self): |
| 364 | "Hook method for deconstructing the test fixture after testing it." |
| 365 | pass |
| 366 | |
| 367 | def countTestCases(self): |
| 368 | return 1 |
| 369 | |
| 370 | def defaultTestResult(self): |
| 371 | return TestResult() |
| 372 | |
| 373 | def shortDescription(self): |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 374 | """Returns both the test method name and first line of its docstring. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 375 | |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 376 | If no docstring is given, only returns the method name. |
| 377 | |
| 378 | This method overrides unittest.TestCase.shortDescription(), which |
| 379 | only returns the first line of the docstring, obscuring the name |
| 380 | of the test upon failure. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 381 | """ |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 382 | desc = str(self) |
| 383 | doc_first_line = None |
| 384 | |
| 385 | if self._testMethodDoc: |
| 386 | doc_first_line = self._testMethodDoc.split("\n")[0].strip() |
| 387 | if doc_first_line: |
| 388 | desc = '\n'.join((desc, doc_first_line)) |
| 389 | return desc |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 390 | |
| 391 | def id(self): |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 392 | return "%s.%s" % (_strclass(self.__class__), self._testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 393 | |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 394 | def __eq__(self, other): |
| 395 | if type(self) is not type(other): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 396 | return NotImplemented |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 397 | |
| 398 | return self._testMethodName == other._testMethodName |
| 399 | |
| 400 | def __ne__(self, other): |
| 401 | return not self == other |
| 402 | |
| 403 | def __hash__(self): |
Collin Winter | 9453e5d | 2007-03-09 23:30:39 +0000 | [diff] [blame] | 404 | return hash((type(self), self._testMethodName)) |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 405 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 406 | def __str__(self): |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 407 | return "%s (%s)" % (self._testMethodName, _strclass(self.__class__)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 408 | |
| 409 | def __repr__(self): |
| 410 | return "<%s testMethod=%s>" % \ |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 411 | (_strclass(self.__class__), self._testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 412 | |
| 413 | def run(self, result=None): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 414 | if result is None: |
| 415 | result = self.defaultTestResult() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 416 | result.startTest(self) |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 417 | testMethod = getattr(self, self._testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 418 | try: |
| 419 | try: |
| 420 | self.setUp() |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 421 | except SkipTest as e: |
| 422 | result.addSkip(self, str(e)) |
| 423 | return |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 424 | except Exception: |
Benjamin Peterson | c930135 | 2009-03-26 16:32:23 +0000 | [diff] [blame] | 425 | result.addError(self, sys.exc_info()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 426 | return |
| 427 | |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 428 | success = False |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 429 | try: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 430 | testMethod() |
Skip Montanaro | ae5c37b | 2003-07-13 15:18:12 +0000 | [diff] [blame] | 431 | except self.failureException: |
Benjamin Peterson | c930135 | 2009-03-26 16:32:23 +0000 | [diff] [blame] | 432 | result.addFailure(self, sys.exc_info()) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 433 | except _ExpectedFailure as e: |
| 434 | result.addExpectedFailure(self, e.exc_info) |
| 435 | except _UnexpectedSuccess: |
| 436 | result.addUnexpectedSuccess(self) |
| 437 | except SkipTest as e: |
| 438 | result.addSkip(self, str(e)) |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 439 | except Exception: |
Benjamin Peterson | c930135 | 2009-03-26 16:32:23 +0000 | [diff] [blame] | 440 | result.addError(self, sys.exc_info()) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 441 | else: |
| 442 | success = True |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 443 | |
| 444 | try: |
| 445 | self.tearDown() |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 446 | except Exception: |
Benjamin Peterson | c930135 | 2009-03-26 16:32:23 +0000 | [diff] [blame] | 447 | result.addError(self, sys.exc_info()) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 448 | success = False |
| 449 | if success: |
| 450 | result.addSuccess(self) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 451 | finally: |
| 452 | result.stopTest(self) |
| 453 | |
Raymond Hettinger | 664347b | 2004-12-04 21:21:53 +0000 | [diff] [blame] | 454 | def __call__(self, *args, **kwds): |
| 455 | return self.run(*args, **kwds) |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 456 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 457 | def debug(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 458 | """Run the test without collecting errors in a TestResult""" |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 459 | self.setUp() |
Georg Brandl | 81cdb4e | 2006-01-20 17:55:00 +0000 | [diff] [blame] | 460 | getattr(self, self._testMethodName)() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 461 | self.tearDown() |
| 462 | |
Benjamin Peterson | 47d9738 | 2009-03-26 20:05:50 +0000 | [diff] [blame] | 463 | def skipTest(self, reason): |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 464 | """Skip this test.""" |
| 465 | raise SkipTest(reason) |
| 466 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 467 | def fail(self, msg=None): |
| 468 | """Fail immediately, with the given message.""" |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 469 | raise self.failureException(msg) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 470 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 471 | def assertFalse(self, expr, msg=None): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 472 | "Fail the test if the expression is true." |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 473 | if expr: |
| 474 | raise self.failureException(msg) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 475 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 476 | def assertTrue(self, expr, msg=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 477 | """Fail the test unless the expression is true.""" |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 478 | if not expr: |
| 479 | raise self.failureException(msg) |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 480 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 481 | def assertRaises(self, excClass, callableObj=None, *args, **kwargs): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 482 | """Fail unless an exception of class excClass is thrown |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 483 | by callableObj when invoked with arguments args and keyword |
| 484 | arguments kwargs. If a different type of exception is |
| 485 | thrown, it will not be caught, and the test case will be |
| 486 | deemed to have suffered an error, exactly as for an |
| 487 | unexpected exception. |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 488 | |
| 489 | If called with callableObj omitted or None, will return a |
| 490 | context object used like this:: |
| 491 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 492 | with self.assertRaises(some_error_class): |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 493 | do_something() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 494 | """ |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 495 | context = _AssertRaisesContext(excClass, self) |
Antoine Pitrou | 697ca3d | 2008-12-28 14:09:36 +0000 | [diff] [blame] | 496 | if callableObj is None: |
| 497 | return context |
| 498 | with context: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 499 | callableObj(*args, **kwargs) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 500 | |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 501 | def _getAssertEqualityFunc(self, first, second): |
| 502 | """Get a detailed comparison function for the types of the two args. |
| 503 | |
| 504 | Returns: A callable accepting (first, second, msg=None) that will |
| 505 | raise a failure exception if first != second with a useful human |
| 506 | readable error message for those types. |
| 507 | """ |
| 508 | # |
| 509 | # NOTE(gregory.p.smith): I considered isinstance(first, type(second)) |
| 510 | # and vice versa. I opted for the conservative approach in case |
| 511 | # subclasses are not intended to be compared in detail to their super |
| 512 | # class instances using a type equality func. This means testing |
| 513 | # subtypes won't automagically use the detailed comparison. Callers |
| 514 | # should use their type specific assertSpamEqual method to compare |
| 515 | # subclasses if the detailed comparison is desired and appropriate. |
| 516 | # See the discussion in http://bugs.python.org/issue2578. |
| 517 | # |
| 518 | if type(first) is type(second): |
| 519 | return self.__type_equality_funcs.get(type(first), |
| 520 | self._baseAssertEqual) |
| 521 | return self._baseAssertEqual |
| 522 | |
| 523 | def _baseAssertEqual(self, first, second, msg=None): |
| 524 | """The default assertEqual implementation, not type specific.""" |
| 525 | if not first == second: |
| 526 | raise self.failureException(msg or '%r != %r' % (first, second)) |
| 527 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 528 | def assertEqual(self, first, second, msg=None): |
Raymond Hettinger | c377cbf | 2003-04-04 22:56:42 +0000 | [diff] [blame] | 529 | """Fail if the two objects are unequal as determined by the '==' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 530 | operator. |
| 531 | """ |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 532 | assertion_func = self._getAssertEqualityFunc(first, second) |
| 533 | assertion_func(first, second, msg=msg) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 534 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 535 | def assertNotEqual(self, first, second, msg=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 536 | """Fail if the two objects are equal as determined by the '==' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 537 | operator. |
| 538 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 539 | if first == second: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 540 | raise self.failureException(msg or '%r == %r' % (first, second)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 541 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 542 | def assertAlmostEqual(self, first, second, places=7, msg=None): |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 543 | """Fail if the two objects are unequal as determined by their |
| 544 | difference rounded to the given number of decimal places |
| 545 | (default 7) and comparing to zero. |
| 546 | |
Steve Purcell | 397b45d | 2003-10-26 10:41:03 +0000 | [diff] [blame] | 547 | Note that decimal places (from zero) are usually not the same |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 548 | as significant digits (measured from the most signficant digit). |
| 549 | """ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 550 | if round(abs(second-first), places) != 0: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 551 | raise self.failureException( |
| 552 | msg or '%r != %r within %r places' % (first, second, places)) |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 553 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 554 | def assertNotAlmostEqual(self, first, second, places=7, msg=None): |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 555 | """Fail if the two objects are equal as determined by their |
| 556 | difference rounded to the given number of decimal places |
| 557 | (default 7) and comparing to zero. |
| 558 | |
Steve Purcell | cca3491 | 2003-10-26 16:38:16 +0000 | [diff] [blame] | 559 | Note that decimal places (from zero) are usually not the same |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 560 | as significant digits (measured from the most signficant digit). |
| 561 | """ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 562 | if round(abs(second-first), places) == 0: |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 563 | raise self.failureException( |
| 564 | msg or '%r == %r within %r places' % (first, second, places)) |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 565 | |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 566 | # Synonyms for assertion methods |
| 567 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 568 | # The plurals are undocumented. Keep them that way to discourage use. |
| 569 | # Do not add more. Do not remove. |
| 570 | # Going through a deprecation cycle on these would annoy many people. |
| 571 | assertEquals = assertEqual |
| 572 | assertNotEquals = assertNotEqual |
| 573 | assertAlmostEquals = assertAlmostEqual |
| 574 | assertNotAlmostEquals = assertNotAlmostEqual |
| 575 | assert_ = assertTrue |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 576 | |
Gregory P. Smith | 7558d57 | 2009-03-31 19:03:28 +0000 | [diff] [blame] | 577 | # These fail* assertion method names are pending deprecation and will |
Gregory P. Smith | 65ff005 | 2009-03-31 19:59:14 +0000 | [diff] [blame^] | 578 | # be a DeprecationWarning in 3.2; http://bugs.python.org/issue2578 |
| 579 | def __deprecate(original_func): |
| 580 | def deprecated_func(*args, **kwargs): |
| 581 | warnings.warn( |
| 582 | 'Please use {0} instead.'.format(original_func.__name__), |
| 583 | PendingDeprecationWarning, 2) |
| 584 | return original_func(*args, **kwargs) |
| 585 | return deprecated_func |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 586 | |
Gregory P. Smith | 65ff005 | 2009-03-31 19:59:14 +0000 | [diff] [blame^] | 587 | failUnlessEqual = __deprecate(assertEqual) |
| 588 | failIfEqual = __deprecate(assertNotEqual) |
| 589 | failUnlessAlmostEqual = __deprecate(assertAlmostEqual) |
| 590 | failIfAlmostEqual = __deprecate(assertNotAlmostEqual) |
| 591 | failUnless = __deprecate(assertTrue) |
| 592 | failUnlessRaises = __deprecate(assertRaises) |
| 593 | failIf = __deprecate(assertFalse) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 594 | |
Gregory P. Smith | 2839985 | 2009-03-31 16:54:10 +0000 | [diff] [blame] | 595 | def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None): |
| 596 | """An equality assertion for ordered sequences (like lists and tuples). |
| 597 | |
| 598 | For the purposes of this function, a valid orderd sequence type is one |
| 599 | which can be indexed, has a length, and has an equality operator. |
| 600 | |
| 601 | Args: |
| 602 | seq1: The first sequence to compare. |
| 603 | seq2: The second sequence to compare. |
| 604 | seq_type: The expected datatype of the sequences, or None if no |
| 605 | datatype should be enforced. |
| 606 | msg: Optional message to use on failure instead of a list of |
| 607 | differences. |
| 608 | """ |
| 609 | if seq_type != None: |
| 610 | seq_type_name = seq_type.__name__ |
| 611 | if not isinstance(seq1, seq_type): |
| 612 | raise self.failureException('First sequence is not a %s: %r' |
| 613 | % (seq_type_name, seq1)) |
| 614 | if not isinstance(seq2, seq_type): |
| 615 | raise self.failureException('Second sequence is not a %s: %r' |
| 616 | % (seq_type_name, seq2)) |
| 617 | else: |
| 618 | seq_type_name = "sequence" |
| 619 | |
| 620 | differing = None |
| 621 | try: |
| 622 | len1 = len(seq1) |
| 623 | except (TypeError, NotImplementedError): |
| 624 | differing = 'First %s has no length. Non-sequence?' % ( |
| 625 | seq_type_name) |
| 626 | |
| 627 | if differing is None: |
| 628 | try: |
| 629 | len2 = len(seq2) |
| 630 | except (TypeError, NotImplementedError): |
| 631 | differing = 'Second %s has no length. Non-sequence?' % ( |
| 632 | seq_type_name) |
| 633 | |
| 634 | if differing is None: |
| 635 | if seq1 == seq2: |
| 636 | return |
| 637 | |
| 638 | for i in xrange(min(len1, len2)): |
| 639 | try: |
| 640 | item1 = seq1[i] |
| 641 | except (TypeError, IndexError, NotImplementedError): |
| 642 | differing = ('Unable to index element %d of first %s\n' % |
| 643 | (i, seq_type_name)) |
| 644 | break |
| 645 | |
| 646 | try: |
| 647 | item2 = seq2[i] |
| 648 | except (TypeError, IndexError, NotImplementedError): |
| 649 | differing = ('Unable to index element %d of second %s\n' % |
| 650 | (i, seq_type_name)) |
| 651 | break |
| 652 | |
| 653 | if item1 != item2: |
| 654 | differing = ('First differing element %d:\n%s\n%s\n' % |
| 655 | (i, item1, item2)) |
| 656 | break |
| 657 | else: |
| 658 | if (len1 == len2 and seq_type is None and |
| 659 | type(seq1) != type(seq2)): |
| 660 | # The sequences are the same, but have differing types. |
| 661 | return |
| 662 | # A catch-all message for handling arbitrary user-defined |
| 663 | # sequences. |
| 664 | differing = '%ss differ:\n' % seq_type_name.capitalize() |
| 665 | if len1 > len2: |
| 666 | differing = ('First %s contains %d additional ' |
| 667 | 'elements.\n' % (seq_type_name, len1 - len2)) |
| 668 | try: |
| 669 | differing += ('First extra element %d:\n%s\n' % |
| 670 | (len2, seq1[len2])) |
| 671 | except (TypeError, IndexError, NotImplementedError): |
| 672 | differing += ('Unable to index element %d ' |
| 673 | 'of first %s\n' % (len2, seq_type_name)) |
| 674 | elif len1 < len2: |
| 675 | differing = ('Second %s contains %d additional ' |
| 676 | 'elements.\n' % (seq_type_name, len2 - len1)) |
| 677 | try: |
| 678 | differing += ('First extra element %d:\n%s\n' % |
| 679 | (len1, seq2[len1])) |
| 680 | except (TypeError, IndexError, NotImplementedError): |
| 681 | differing += ('Unable to index element %d ' |
| 682 | 'of second %s\n' % (len1, seq_type_name)) |
| 683 | if not msg: |
| 684 | msg = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), |
| 685 | pprint.pformat(seq2).splitlines())) |
| 686 | self.fail(differing + msg) |
| 687 | |
| 688 | def assertListEqual(self, list1, list2, msg=None): |
| 689 | """A list-specific equality assertion. |
| 690 | |
| 691 | Args: |
| 692 | list1: The first list to compare. |
| 693 | list2: The second list to compare. |
| 694 | msg: Optional message to use on failure instead of a list of |
| 695 | differences. |
| 696 | |
| 697 | """ |
| 698 | self.assertSequenceEqual(list1, list2, msg, seq_type=list) |
| 699 | |
| 700 | def assertTupleEqual(self, tuple1, tuple2, msg=None): |
| 701 | """A tuple-specific equality assertion. |
| 702 | |
| 703 | Args: |
| 704 | tuple1: The first tuple to compare. |
| 705 | tuple2: The second tuple to compare. |
| 706 | msg: Optional message to use on failure instead of a list of |
| 707 | differences. |
| 708 | """ |
| 709 | self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple) |
| 710 | |
| 711 | def assertSetEqual(self, set1, set2, msg=None): |
| 712 | """A set-specific equality assertion. |
| 713 | |
| 714 | Args: |
| 715 | set1: The first set to compare. |
| 716 | set2: The second set to compare. |
| 717 | msg: Optional message to use on failure instead of a list of |
| 718 | differences. |
| 719 | |
| 720 | For more general containership equality, assertSameElements will work |
| 721 | with things other than sets. This uses ducktyping to support |
| 722 | different types of sets, and is optimized for sets specifically |
| 723 | (parameters must support a difference method). |
| 724 | """ |
| 725 | try: |
| 726 | difference1 = set1.difference(set2) |
| 727 | except TypeError, e: |
| 728 | self.fail('invalid type when attempting set difference: %s' % e) |
| 729 | except AttributeError, e: |
| 730 | self.fail('first argument does not support set difference: %s' % e) |
| 731 | |
| 732 | try: |
| 733 | difference2 = set2.difference(set1) |
| 734 | except TypeError, e: |
| 735 | self.fail('invalid type when attempting set difference: %s' % e) |
| 736 | except AttributeError, e: |
| 737 | self.fail('second argument does not support set difference: %s' % e) |
| 738 | |
| 739 | if not (difference1 or difference2): |
| 740 | return |
| 741 | |
| 742 | if msg is not None: |
| 743 | self.fail(msg) |
| 744 | |
| 745 | lines = [] |
| 746 | if difference1: |
| 747 | lines.append('Items in the first set but not the second:') |
| 748 | for item in difference1: |
| 749 | lines.append(repr(item)) |
| 750 | if difference2: |
| 751 | lines.append('Items in the second set but not the first:') |
| 752 | for item in difference2: |
| 753 | lines.append(repr(item)) |
| 754 | self.fail('\n'.join(lines)) |
| 755 | |
| 756 | def assertIn(self, a, b, msg=None): |
| 757 | """Just like self.assert_(a in b), but with a nicer default message.""" |
| 758 | if msg is None: |
| 759 | msg = '"%s" not found in "%s"' % (a, b) |
| 760 | self.assert_(a in b, msg) |
| 761 | |
| 762 | def assertNotIn(self, a, b, msg=None): |
| 763 | """Just like self.assert_(a not in b), but with a nicer default message.""" |
| 764 | if msg is None: |
| 765 | msg = '"%s" unexpectedly found in "%s"' % (a, b) |
| 766 | self.assert_(a not in b, msg) |
| 767 | |
| 768 | def assertDictEqual(self, d1, d2, msg=None): |
| 769 | self.assert_(isinstance(d1, dict), 'First argument is not a dictionary') |
| 770 | self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary') |
| 771 | |
| 772 | if d1 != d2: |
| 773 | self.fail(msg or ('\n' + '\n'.join(difflib.ndiff( |
| 774 | pprint.pformat(d1).splitlines(), |
| 775 | pprint.pformat(d2).splitlines())))) |
| 776 | |
| 777 | def assertDictContainsSubset(self, expected, actual, msg=None): |
| 778 | """Checks whether actual is a superset of expected.""" |
| 779 | missing = [] |
| 780 | mismatched = [] |
| 781 | for key, value in expected.iteritems(): |
| 782 | if key not in actual: |
| 783 | missing.append(key) |
| 784 | elif value != actual[key]: |
| 785 | mismatched.append('%s, expected: %s, actual: %s' % (key, value, |
| 786 | actual[key])) |
| 787 | |
| 788 | if not (missing or mismatched): |
| 789 | return |
| 790 | |
| 791 | missing_msg = mismatched_msg = '' |
| 792 | if missing: |
| 793 | missing_msg = 'Missing: %s' % ','.join(missing) |
| 794 | if mismatched: |
| 795 | mismatched_msg = 'Mismatched values: %s' % ','.join(mismatched) |
| 796 | |
| 797 | if msg: |
| 798 | msg = '%s: %s; %s' % (msg, missing_msg, mismatched_msg) |
| 799 | else: |
| 800 | msg = '%s; %s' % (missing_msg, mismatched_msg) |
| 801 | self.fail(msg) |
| 802 | |
| 803 | def assertSameElements(self, expected_seq, actual_seq, msg=None): |
| 804 | """An unordered sequence specific comparison. |
| 805 | |
| 806 | Raises with an error message listing which elements of expected_seq |
| 807 | are missing from actual_seq and vice versa if any. |
| 808 | """ |
| 809 | try: |
| 810 | expected = set(expected_seq) |
| 811 | actual = set(actual_seq) |
| 812 | missing = list(expected.difference(actual)) |
| 813 | unexpected = list(actual.difference(expected)) |
| 814 | missing.sort() |
| 815 | unexpected.sort() |
| 816 | except TypeError: |
| 817 | # Fall back to slower list-compare if any of the objects are |
| 818 | # not hashable. |
| 819 | expected = list(expected_seq) |
| 820 | actual = list(actual_seq) |
| 821 | expected.sort() |
| 822 | actual.sort() |
| 823 | missing, unexpected = _SortedListDifference(expected, actual) |
| 824 | errors = [] |
| 825 | if missing: |
| 826 | errors.append('Expected, but missing:\n %r\n' % missing) |
| 827 | if unexpected: |
| 828 | errors.append('Unexpected, but present:\n %r\n' % unexpected) |
| 829 | if errors: |
| 830 | self.fail(msg or ''.join(errors)) |
| 831 | |
| 832 | def assertMultiLineEqual(self, first, second, msg=None): |
| 833 | """Assert that two multi-line strings are equal.""" |
| 834 | self.assert_(isinstance(first, types.StringTypes), ( |
| 835 | 'First argument is not a string')) |
| 836 | self.assert_(isinstance(second, types.StringTypes), ( |
| 837 | 'Second argument is not a string')) |
| 838 | |
| 839 | if first != second: |
| 840 | raise self.failureException( |
| 841 | msg or '\n' + ''.join(difflib.ndiff(first.splitlines(True), |
| 842 | second.splitlines(True)))) |
| 843 | |
| 844 | def assertLess(self, a, b, msg=None): |
| 845 | """Just like self.assert_(a < b), but with a nicer default message.""" |
| 846 | if msg is None: |
| 847 | msg = '"%r" unexpectedly not less than "%r"' % (a, b) |
| 848 | self.assert_(a < b, msg) |
| 849 | |
| 850 | def assertLessEqual(self, a, b, msg=None): |
| 851 | """Just like self.assert_(a <= b), but with a nicer default message.""" |
| 852 | if msg is None: |
| 853 | msg = '"%r" unexpectedly not less than or equal to "%r"' % (a, b) |
| 854 | self.assert_(a <= b, msg) |
| 855 | |
| 856 | def assertGreater(self, a, b, msg=None): |
| 857 | """Just like self.assert_(a > b), but with a nicer default message.""" |
| 858 | if msg is None: |
| 859 | msg = '"%r" unexpectedly not greater than "%r"' % (a, b) |
| 860 | self.assert_(a > b, msg) |
| 861 | |
| 862 | def assertGreaterEqual(self, a, b, msg=None): |
| 863 | """Just like self.assert_(a >= b), but with a nicer default message.""" |
| 864 | if msg is None: |
| 865 | msg = '"%r" unexpectedly not greater than or equal to "%r"' % (a, b) |
| 866 | self.assert_(a >= b, msg) |
| 867 | |
| 868 | def assertIsNone(self, obj, msg=None): |
| 869 | """Same as self.assert_(obj is None), with a nicer default message.""" |
| 870 | if msg is None: |
| 871 | msg = '"%s" unexpectedly not None' % obj |
| 872 | self.assert_(obj is None, msg) |
| 873 | |
| 874 | def assertIsNotNone(self, obj, msg='unexpectedly None'): |
| 875 | """Included for symmetry with assertIsNone.""" |
| 876 | self.assert_(obj is not None, msg) |
| 877 | |
| 878 | def assertRaisesRegexp(self, expected_exception, expected_regexp, |
| 879 | callable_obj=None, *args, **kwargs): |
| 880 | """Asserts that the message in a raised exception matches a regexp. |
| 881 | |
| 882 | Args: |
| 883 | expected_exception: Exception class expected to be raised. |
| 884 | expected_regexp: Regexp (re pattern object or string) expected |
| 885 | to be found in error message. |
| 886 | callable_obj: Function to be called. |
| 887 | args: Extra args. |
| 888 | kwargs: Extra kwargs. |
| 889 | """ |
| 890 | context = _AssertRaisesContext(expected_exception, self, expected_regexp) |
| 891 | if callable_obj is None: |
| 892 | return context |
| 893 | with context: |
| 894 | callable_obj(*args, **kwargs) |
| 895 | |
| 896 | def assertRegexpMatches(self, text, expected_regex, msg=None): |
| 897 | if isinstance(expected_regex, basestring): |
| 898 | expected_regex = re.compile(expected_regex) |
| 899 | if not expected_regex.search(text): |
| 900 | msg = msg or "Regexp didn't match" |
| 901 | msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text) |
| 902 | raise self.failureException(msg) |
| 903 | |
| 904 | |
| 905 | def _SortedListDifference(expected, actual): |
| 906 | """Finds elements in only one or the other of two, sorted input lists. |
| 907 | |
| 908 | Returns a two-element tuple of lists. The first list contains those |
| 909 | elements in the "expected" list but not in the "actual" list, and the |
| 910 | second contains those elements in the "actual" list but not in the |
| 911 | "expected" list. Duplicate elements in either input list are ignored. |
| 912 | """ |
| 913 | i = j = 0 |
| 914 | missing = [] |
| 915 | unexpected = [] |
| 916 | while True: |
| 917 | try: |
| 918 | e = expected[i] |
| 919 | a = actual[j] |
| 920 | if e < a: |
| 921 | missing.append(e) |
| 922 | i += 1 |
| 923 | while expected[i] == e: |
| 924 | i += 1 |
| 925 | elif e > a: |
| 926 | unexpected.append(a) |
| 927 | j += 1 |
| 928 | while actual[j] == a: |
| 929 | j += 1 |
| 930 | else: |
| 931 | i += 1 |
| 932 | try: |
| 933 | while expected[i] == e: |
| 934 | i += 1 |
| 935 | finally: |
| 936 | j += 1 |
| 937 | while actual[j] == a: |
| 938 | j += 1 |
| 939 | except IndexError: |
| 940 | missing.extend(expected[i:]) |
| 941 | unexpected.extend(actual[j:]) |
| 942 | break |
| 943 | return missing, unexpected |
| 944 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 946 | class TestSuite(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 947 | """A test suite is a composite test consisting of a number of TestCases. |
| 948 | |
| 949 | For use, create an instance of TestSuite, then add test case instances. |
| 950 | When all tests have been added, the suite can be passed to a test |
| 951 | runner, such as TextTestRunner. It will run the individual test cases |
| 952 | in the order in which they were added, aggregating the results. When |
| 953 | subclassing, do not forget to call the base class constructor. |
| 954 | """ |
| 955 | def __init__(self, tests=()): |
| 956 | self._tests = [] |
| 957 | self.addTests(tests) |
| 958 | |
| 959 | def __repr__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 960 | return "<%s tests=%s>" % (_strclass(self.__class__), self._tests) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 961 | |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 962 | def __eq__(self, other): |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 963 | if not isinstance(other, self.__class__): |
| 964 | return NotImplemented |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 965 | return self._tests == other._tests |
| 966 | |
| 967 | def __ne__(self, other): |
| 968 | return not self == other |
| 969 | |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 970 | # Can't guarantee hash invariant, so flag as unhashable |
| 971 | __hash__ = None |
| 972 | |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 973 | def __iter__(self): |
| 974 | return iter(self._tests) |
| 975 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 976 | def countTestCases(self): |
| 977 | cases = 0 |
| 978 | for test in self._tests: |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 979 | cases += test.countTestCases() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 980 | return cases |
| 981 | |
| 982 | def addTest(self, test): |
Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 983 | # sanity checks |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 984 | if not hasattr(test, '__call__'): |
Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 985 | raise TypeError("the test to add must be callable") |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 986 | if isinstance(test, type) and issubclass(test, (TestCase, TestSuite)): |
Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 987 | raise TypeError("TestCases and TestSuites must be instantiated " |
| 988 | "before passing them to addTest()") |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 989 | self._tests.append(test) |
| 990 | |
| 991 | def addTests(self, tests): |
Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 992 | if isinstance(tests, basestring): |
| 993 | raise TypeError("tests must be an iterable of tests, not a string") |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 994 | for test in tests: |
| 995 | self.addTest(test) |
| 996 | |
| 997 | def run(self, result): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 998 | for test in self._tests: |
| 999 | if result.shouldStop: |
| 1000 | break |
| 1001 | test(result) |
| 1002 | return result |
| 1003 | |
Raymond Hettinger | 664347b | 2004-12-04 21:21:53 +0000 | [diff] [blame] | 1004 | def __call__(self, *args, **kwds): |
| 1005 | return self.run(*args, **kwds) |
| 1006 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1007 | def debug(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1008 | """Run the tests without collecting errors in a TestResult""" |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1009 | for test in self._tests: |
| 1010 | test.debug() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1011 | |
| 1012 | |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1013 | class ClassTestSuite(TestSuite): |
| 1014 | """ |
| 1015 | Suite of tests derived from a single TestCase class. |
| 1016 | """ |
| 1017 | |
| 1018 | def __init__(self, tests, class_collected_from): |
| 1019 | super(ClassTestSuite, self).__init__(tests) |
| 1020 | self.collected_from = class_collected_from |
| 1021 | |
| 1022 | def id(self): |
| 1023 | module = getattr(self.collected_from, "__module__", None) |
| 1024 | if module is not None: |
| 1025 | return "{0}.{1}".format(module, self.collected_from.__name__) |
| 1026 | return self.collected_from.__name__ |
| 1027 | |
| 1028 | def run(self, result): |
| 1029 | if getattr(self.collected_from, "__unittest_skip__", False): |
| 1030 | # ClassTestSuite result pretends to be a TestCase enough to be |
| 1031 | # reported. |
| 1032 | result.startTest(self) |
| 1033 | try: |
| 1034 | result.addSkip(self, self.collected_from.__unittest_skip_why__) |
| 1035 | finally: |
| 1036 | result.stopTest(self) |
| 1037 | else: |
| 1038 | result = super(ClassTestSuite, self).run(result) |
| 1039 | return result |
| 1040 | |
| 1041 | shortDescription = id |
| 1042 | |
| 1043 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1044 | class FunctionTestCase(TestCase): |
| 1045 | """A test case that wraps a test function. |
| 1046 | |
| 1047 | This is useful for slipping pre-existing test functions into the |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1048 | unittest framework. Optionally, set-up and tidy-up functions can be |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1049 | supplied. As with TestCase, the tidy-up ('tearDown') function will |
| 1050 | always be called if the set-up ('setUp') function ran successfully. |
| 1051 | """ |
| 1052 | |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1053 | def __init__(self, testFunc, setUp=None, tearDown=None, description=None): |
| 1054 | super(FunctionTestCase, self).__init__() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1055 | self.__setUpFunc = setUp |
| 1056 | self.__tearDownFunc = tearDown |
| 1057 | self.__testFunc = testFunc |
| 1058 | self.__description = description |
| 1059 | |
| 1060 | def setUp(self): |
| 1061 | if self.__setUpFunc is not None: |
| 1062 | self.__setUpFunc() |
| 1063 | |
| 1064 | def tearDown(self): |
| 1065 | if self.__tearDownFunc is not None: |
| 1066 | self.__tearDownFunc() |
| 1067 | |
| 1068 | def runTest(self): |
| 1069 | self.__testFunc() |
| 1070 | |
| 1071 | def id(self): |
| 1072 | return self.__testFunc.__name__ |
| 1073 | |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1074 | def __eq__(self, other): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1075 | if not isinstance(other, self.__class__): |
| 1076 | return NotImplemented |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1077 | |
| 1078 | return self.__setUpFunc == other.__setUpFunc and \ |
| 1079 | self.__tearDownFunc == other.__tearDownFunc and \ |
| 1080 | self.__testFunc == other.__testFunc and \ |
| 1081 | self.__description == other.__description |
| 1082 | |
| 1083 | def __ne__(self, other): |
| 1084 | return not self == other |
| 1085 | |
| 1086 | def __hash__(self): |
Collin Winter | 9453e5d | 2007-03-09 23:30:39 +0000 | [diff] [blame] | 1087 | return hash((type(self), self.__setUpFunc, self.__tearDownFunc, |
| 1088 | self.__testFunc, self.__description)) |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1089 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1090 | def __str__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 1091 | return "%s (%s)" % (_strclass(self.__class__), self.__testFunc.__name__) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1092 | |
| 1093 | def __repr__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 1094 | return "<%s testFunc=%s>" % (_strclass(self.__class__), self.__testFunc) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1095 | |
| 1096 | def shortDescription(self): |
| 1097 | if self.__description is not None: return self.__description |
| 1098 | doc = self.__testFunc.__doc__ |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1099 | return doc and doc.split("\n")[0].strip() or None |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1100 | |
| 1101 | |
| 1102 | |
| 1103 | ############################################################################## |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1104 | # Locating and loading tests |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1105 | ############################################################################## |
| 1106 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 1107 | class TestLoader(object): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1108 | """ |
| 1109 | This class is responsible for loading tests according to various criteria |
| 1110 | and returning them wrapped in a TestSuite |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1111 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1112 | testMethodPrefix = 'test' |
| 1113 | sortTestMethodsUsing = cmp |
| 1114 | suiteClass = TestSuite |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1115 | classSuiteClass = ClassTestSuite |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1116 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1117 | def loadTestsFromTestCase(self, testCaseClass): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 1118 | """Return a suite of all tests cases contained in testCaseClass""" |
Johannes Gijsbers | d7b6ad4 | 2004-11-07 15:46:25 +0000 | [diff] [blame] | 1119 | if issubclass(testCaseClass, TestSuite): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1120 | raise TypeError("Test cases should not be derived from TestSuite." \ |
| 1121 | " Maybe you meant to derive from TestCase?") |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1122 | testCaseNames = self.getTestCaseNames(testCaseClass) |
| 1123 | if not testCaseNames and hasattr(testCaseClass, 'runTest'): |
| 1124 | testCaseNames = ['runTest'] |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1125 | suite = self.classSuiteClass(map(testCaseClass, testCaseNames), |
| 1126 | testCaseClass) |
| 1127 | return suite |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1128 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1129 | def loadTestsFromModule(self, module): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 1130 | """Return a suite of all tests cases contained in the given module""" |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1131 | tests = [] |
| 1132 | for name in dir(module): |
| 1133 | obj = getattr(module, name) |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1134 | if isinstance(obj, type) and issubclass(obj, TestCase): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1135 | tests.append(self.loadTestsFromTestCase(obj)) |
| 1136 | return self.suiteClass(tests) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1137 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1138 | def loadTestsFromName(self, name, module=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 1139 | """Return a suite of all tests cases given a string specifier. |
| 1140 | |
| 1141 | The name may resolve either to a module, a test case class, a |
| 1142 | test method within a test case class, or a callable object which |
| 1143 | returns a TestCase or TestSuite instance. |
Tim Peters | 613b222 | 2001-04-13 05:37:27 +0000 | [diff] [blame] | 1144 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 1145 | The method optionally resolves the names relative to a given module. |
| 1146 | """ |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1147 | parts = name.split('.') |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1148 | if module is None: |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1149 | parts_copy = parts[:] |
| 1150 | while parts_copy: |
| 1151 | try: |
| 1152 | module = __import__('.'.join(parts_copy)) |
| 1153 | break |
| 1154 | except ImportError: |
| 1155 | del parts_copy[-1] |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1156 | if not parts_copy: |
| 1157 | raise |
Armin Rigo | 1b3c04b | 2003-10-24 17:15:29 +0000 | [diff] [blame] | 1158 | parts = parts[1:] |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1159 | obj = module |
| 1160 | for part in parts: |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1161 | parent, obj = obj, getattr(obj, part) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 1163 | if isinstance(obj, types.ModuleType): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1164 | return self.loadTestsFromModule(obj) |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1165 | elif isinstance(obj, type) and issubclass(obj, TestCase): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1166 | return self.loadTestsFromTestCase(obj) |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 1167 | elif (isinstance(obj, types.UnboundMethodType) and |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1168 | isinstance(parent, type) and |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1169 | issubclass(parent, TestCase)): |
| 1170 | return TestSuite([parent(obj.__name__)]) |
Steve Purcell | 397b45d | 2003-10-26 10:41:03 +0000 | [diff] [blame] | 1171 | elif isinstance(obj, TestSuite): |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1172 | return obj |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 1173 | elif hasattr(obj, '__call__'): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1174 | test = obj() |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1175 | if isinstance(test, TestSuite): |
| 1176 | return test |
| 1177 | elif isinstance(test, TestCase): |
| 1178 | return TestSuite([test]) |
| 1179 | else: |
| 1180 | raise TypeError("calling %s returned %s, not a test" % |
| 1181 | (obj, test)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1182 | else: |
Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1183 | raise TypeError("don't know how to make test from: %s" % obj) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1184 | |
| 1185 | def loadTestsFromNames(self, names, module=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 1186 | """Return a suite of all tests cases found using the given sequence |
| 1187 | of string specifiers. See 'loadTestsFromName()'. |
| 1188 | """ |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1189 | suites = [self.loadTestsFromName(name, module) for name in names] |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1190 | return self.suiteClass(suites) |
| 1191 | |
| 1192 | def getTestCaseNames(self, testCaseClass): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 1193 | """Return a sorted sequence of method names found within testCaseClass |
| 1194 | """ |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1195 | def isTestMethod(attrname, testCaseClass=testCaseClass, |
| 1196 | prefix=self.testMethodPrefix): |
| 1197 | return attrname.startswith(prefix) and \ |
| 1198 | hasattr(getattr(testCaseClass, attrname), '__call__') |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1199 | testFnNames = filter(isTestMethod, dir(testCaseClass)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1200 | if self.sortTestMethodsUsing: |
Raymond Hettinger | 5930d8f | 2008-07-10 16:06:41 +0000 | [diff] [blame] | 1201 | testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1202 | return testFnNames |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | defaultTestLoader = TestLoader() |
| 1207 | |
| 1208 | |
| 1209 | ############################################################################## |
| 1210 | # Patches for old functions: these functions should be considered obsolete |
| 1211 | ############################################################################## |
| 1212 | |
| 1213 | def _makeLoader(prefix, sortUsing, suiteClass=None): |
| 1214 | loader = TestLoader() |
| 1215 | loader.sortTestMethodsUsing = sortUsing |
| 1216 | loader.testMethodPrefix = prefix |
| 1217 | if suiteClass: loader.suiteClass = suiteClass |
| 1218 | return loader |
| 1219 | |
| 1220 | def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp): |
| 1221 | return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) |
| 1222 | |
| 1223 | def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite): |
| 1224 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass) |
| 1225 | |
| 1226 | def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite): |
| 1227 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1228 | |
| 1229 | |
| 1230 | ############################################################################## |
| 1231 | # Text UI |
| 1232 | ############################################################################## |
| 1233 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 1234 | class _WritelnDecorator(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1235 | """Used to decorate file-like objects with a handy 'writeln' method""" |
| 1236 | def __init__(self,stream): |
| 1237 | self.stream = stream |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1238 | |
| 1239 | def __getattr__(self, attr): |
| 1240 | return getattr(self.stream,attr) |
| 1241 | |
Raymond Hettinger | 91dd19d | 2003-09-13 02:58:00 +0000 | [diff] [blame] | 1242 | def writeln(self, arg=None): |
Benjamin Peterson | d0cdb2d | 2009-03-24 23:07:07 +0000 | [diff] [blame] | 1243 | if arg: |
| 1244 | self.write(arg) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1245 | self.write('\n') # text-mode streams translate to \r\n if needed |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 1246 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1247 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1248 | class _TextTestResult(TestResult): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1249 | """A test result class that can print formatted text results to a stream. |
| 1250 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1251 | Used by TextTestRunner. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1252 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1253 | separator1 = '=' * 70 |
| 1254 | separator2 = '-' * 70 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1255 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1256 | def __init__(self, stream, descriptions, verbosity): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1257 | super(_TextTestResult, self).__init__() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1258 | self.stream = stream |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1259 | self.showAll = verbosity > 1 |
| 1260 | self.dots = verbosity == 1 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1261 | self.descriptions = descriptions |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1262 | |
| 1263 | def getDescription(self, test): |
| 1264 | if self.descriptions: |
| 1265 | return test.shortDescription() or str(test) |
| 1266 | else: |
| 1267 | return str(test) |
| 1268 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1269 | def startTest(self, test): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1270 | super(_TextTestResult, self).startTest(test) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1271 | if self.showAll: |
| 1272 | self.stream.write(self.getDescription(test)) |
| 1273 | self.stream.write(" ... ") |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 1274 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1275 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1276 | def addSuccess(self, test): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1277 | super(_TextTestResult, self).addSuccess(test) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1278 | if self.showAll: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1279 | self.stream.writeln("ok") |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1280 | elif self.dots: |
| 1281 | self.stream.write('.') |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 1282 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1283 | |
| 1284 | def addError(self, test, err): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1285 | super(_TextTestResult, self).addError(test, err) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1286 | if self.showAll: |
| 1287 | self.stream.writeln("ERROR") |
| 1288 | elif self.dots: |
| 1289 | self.stream.write('E') |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 1290 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1291 | |
| 1292 | def addFailure(self, test, err): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1293 | super(_TextTestResult, self).addFailure(test, err) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1294 | if self.showAll: |
| 1295 | self.stream.writeln("FAIL") |
| 1296 | elif self.dots: |
| 1297 | self.stream.write('F') |
Georg Brandl | d063240 | 2008-05-11 15:17:41 +0000 | [diff] [blame] | 1298 | self.stream.flush() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1299 | |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1300 | def addSkip(self, test, reason): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1301 | super(_TextTestResult, self).addSkip(test, reason) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1302 | if self.showAll: |
| 1303 | self.stream.writeln("skipped {0!r}".format(reason)) |
| 1304 | elif self.dots: |
| 1305 | self.stream.write("s") |
| 1306 | self.stream.flush() |
| 1307 | |
| 1308 | def addExpectedFailure(self, test, err): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1309 | super(_TextTestResult, self).addExpectedFailure(test, err) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1310 | if self.showAll: |
| 1311 | self.stream.writeln("expected failure") |
| 1312 | elif self.dots: |
Benjamin Peterson | a8adceb | 2009-03-25 21:24:04 +0000 | [diff] [blame] | 1313 | self.stream.write("x") |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1314 | self.stream.flush() |
| 1315 | |
| 1316 | def addUnexpectedSuccess(self, test): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1317 | super(_TextTestResult, self).addUnexpectedSuccess(test) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1318 | if self.showAll: |
| 1319 | self.stream.writeln("unexpected success") |
| 1320 | elif self.dots: |
Benjamin Peterson | a8adceb | 2009-03-25 21:24:04 +0000 | [diff] [blame] | 1321 | self.stream.write("u") |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1322 | self.stream.flush() |
| 1323 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1324 | def printErrors(self): |
| 1325 | if self.dots or self.showAll: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1326 | self.stream.writeln() |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1327 | self.printErrorList('ERROR', self.errors) |
| 1328 | self.printErrorList('FAIL', self.failures) |
| 1329 | |
| 1330 | def printErrorList(self, flavour, errors): |
| 1331 | for test, err in errors: |
| 1332 | self.stream.writeln(self.separator1) |
| 1333 | self.stream.writeln("%s: %s" % (flavour,self.getDescription(test))) |
| 1334 | self.stream.writeln(self.separator2) |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 1335 | self.stream.writeln("%s" % err) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1336 | |
| 1337 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 1338 | class TextTestRunner(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1339 | """A test runner class that displays results in textual form. |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 1340 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1341 | It prints out the names of tests as they are run, errors as they |
| 1342 | occur, and a summary of the results at the end of the test run. |
| 1343 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1344 | def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1345 | self.stream = _WritelnDecorator(stream) |
| 1346 | self.descriptions = descriptions |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1347 | self.verbosity = verbosity |
| 1348 | |
| 1349 | def _makeResult(self): |
| 1350 | return _TextTestResult(self.stream, self.descriptions, self.verbosity) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1351 | |
| 1352 | def run(self, test): |
| 1353 | "Run the given test case or test suite." |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1354 | result = self._makeResult() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1355 | startTime = time.time() |
| 1356 | test(result) |
| 1357 | stopTime = time.time() |
Steve Purcell | 397b45d | 2003-10-26 10:41:03 +0000 | [diff] [blame] | 1358 | timeTaken = stopTime - startTime |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1359 | result.printErrors() |
| 1360 | self.stream.writeln(result.separator2) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1361 | run = result.testsRun |
| 1362 | self.stream.writeln("Ran %d test%s in %.3fs" % |
Neal Norwitz | 7616504 | 2002-05-31 14:15:11 +0000 | [diff] [blame] | 1363 | (run, run != 1 and "s" or "", timeTaken)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1364 | self.stream.writeln() |
Benjamin Peterson | cb2b0e4 | 2009-03-23 22:29:45 +0000 | [diff] [blame] | 1365 | results = map(len, (result.expectedFailures, |
| 1366 | result.unexpectedSuccesses, |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1367 | result.skipped)) |
Benjamin Peterson | cb2b0e4 | 2009-03-23 22:29:45 +0000 | [diff] [blame] | 1368 | expectedFails, unexpectedSuccesses, skipped = results |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1369 | infos = [] |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1370 | if not result.wasSuccessful(): |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1371 | self.stream.write("FAILED") |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1372 | failed, errored = map(len, (result.failures, result.errors)) |
| 1373 | if failed: |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1374 | infos.append("failures=%d" % failed) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1375 | if errored: |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1376 | infos.append("errors=%d" % errored) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1377 | else: |
Benjamin Peterson | a473f00 | 2009-03-24 22:56:32 +0000 | [diff] [blame] | 1378 | self.stream.write("OK") |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1379 | if skipped: |
| 1380 | infos.append("skipped=%d" % skipped) |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1381 | if expectedFails: |
| 1382 | infos.append("expected failures=%d" % expectedFails) |
| 1383 | if unexpectedSuccesses: |
| 1384 | infos.append("unexpected successes=%d" % unexpectedSuccesses) |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1385 | if infos: |
| 1386 | self.stream.writeln(" (%s)" % (", ".join(infos),)) |
Benjamin Peterson | a473f00 | 2009-03-24 22:56:32 +0000 | [diff] [blame] | 1387 | else: |
| 1388 | self.stream.write("\n") |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1389 | return result |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 1390 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1391 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1392 | |
| 1393 | ############################################################################## |
| 1394 | # Facilities for running tests from the command line |
| 1395 | ############################################################################## |
| 1396 | |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 1397 | class TestProgram(object): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1398 | """A command-line program that runs a set of tests; this is primarily |
| 1399 | for making test modules conveniently executable. |
| 1400 | """ |
| 1401 | USAGE = """\ |
Steve Purcell | 17a781b | 2001-04-09 15:37:31 +0000 | [diff] [blame] | 1402 | Usage: %(progName)s [options] [test] [...] |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1403 | |
| 1404 | Options: |
| 1405 | -h, --help Show this message |
| 1406 | -v, --verbose Verbose output |
| 1407 | -q, --quiet Minimal output |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1408 | |
| 1409 | Examples: |
| 1410 | %(progName)s - run default set of tests |
| 1411 | %(progName)s MyTestSuite - run suite 'MyTestSuite' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1412 | %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething |
| 1413 | %(progName)s MyTestCase - run all 'test*' test methods |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1414 | in MyTestCase |
| 1415 | """ |
| 1416 | def __init__(self, module='__main__', defaultTest=None, |
Georg Brandl | d0a9625 | 2007-03-07 09:21:06 +0000 | [diff] [blame] | 1417 | argv=None, testRunner=TextTestRunner, |
| 1418 | testLoader=defaultTestLoader): |
Antoine Pitrou | dae1a6a | 2008-12-28 16:01:11 +0000 | [diff] [blame] | 1419 | if isinstance(module, basestring): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1420 | self.module = __import__(module) |
Steve Purcell | 7e74384 | 2003-09-22 11:08:12 +0000 | [diff] [blame] | 1421 | for part in module.split('.')[1:]: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1422 | self.module = getattr(self.module, part) |
| 1423 | else: |
| 1424 | self.module = module |
| 1425 | if argv is None: |
| 1426 | argv = sys.argv |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1427 | self.verbosity = 1 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1428 | self.defaultTest = defaultTest |
| 1429 | self.testRunner = testRunner |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1430 | self.testLoader = testLoader |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1431 | self.progName = os.path.basename(argv[0]) |
| 1432 | self.parseArgs(argv) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1433 | self.runTests() |
| 1434 | |
| 1435 | def usageExit(self, msg=None): |
Benjamin Peterson | a7d441d | 2009-03-24 00:35:20 +0000 | [diff] [blame] | 1436 | if msg: |
| 1437 | print msg |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1438 | print self.USAGE % self.__dict__ |
| 1439 | sys.exit(2) |
| 1440 | |
| 1441 | def parseArgs(self, argv): |
| 1442 | import getopt |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1443 | long_opts = ['help','verbose','quiet'] |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1444 | try: |
Benjamin Peterson | 692428e | 2009-03-23 21:50:21 +0000 | [diff] [blame] | 1445 | options, args = getopt.getopt(argv[1:], 'hHvq', long_opts) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1446 | for opt, value in options: |
| 1447 | if opt in ('-h','-H','--help'): |
| 1448 | self.usageExit() |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1449 | if opt in ('-q','--quiet'): |
| 1450 | self.verbosity = 0 |
| 1451 | if opt in ('-v','--verbose'): |
| 1452 | self.verbosity = 2 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1453 | if len(args) == 0 and self.defaultTest is None: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1454 | self.test = self.testLoader.loadTestsFromModule(self.module) |
| 1455 | return |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1456 | if len(args) > 0: |
| 1457 | self.testNames = args |
| 1458 | else: |
| 1459 | self.testNames = (self.defaultTest,) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1460 | self.createTests() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1461 | except getopt.error, msg: |
| 1462 | self.usageExit(msg) |
| 1463 | |
| 1464 | def createTests(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 1465 | self.test = self.testLoader.loadTestsFromNames(self.testNames, |
| 1466 | self.module) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1467 | |
| 1468 | def runTests(self): |
Georg Brandl | d0a9625 | 2007-03-07 09:21:06 +0000 | [diff] [blame] | 1469 | if isinstance(self.testRunner, (type, types.ClassType)): |
| 1470 | try: |
| 1471 | testRunner = self.testRunner(verbosity=self.verbosity) |
| 1472 | except TypeError: |
| 1473 | # didn't accept the verbosity argument |
| 1474 | testRunner = self.testRunner() |
| 1475 | else: |
| 1476 | # it is assumed to be a TestRunner instance |
| 1477 | testRunner = self.testRunner |
| 1478 | result = testRunner.run(self.test) |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 1479 | sys.exit(not result.wasSuccessful()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 1480 | |
| 1481 | main = TestProgram |
| 1482 | |
| 1483 | |
| 1484 | ############################################################################## |
| 1485 | # Executing this module from the command line |
| 1486 | ############################################################################## |
| 1487 | |
| 1488 | if __name__ == "__main__": |
| 1489 | main(module=None) |