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 | |
| 28 | http://pyunit.sourceforge.net/ |
| 29 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 30 | Copyright (c) 1999, 2000, 2001 Steve Purcell |
| 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 | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 49 | __version__ = "#Revision: 1.46 $"[11:-2] |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 50 | |
| 51 | import time |
| 52 | import sys |
| 53 | import traceback |
| 54 | import string |
| 55 | import os |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 56 | import types |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 57 | |
| 58 | ############################################################################## |
| 59 | # Test framework core |
| 60 | ############################################################################## |
| 61 | |
Steve Purcell | 824574d | 2002-08-08 13:38:02 +0000 | [diff] [blame] | 62 | # All classes defined herein are 'new-style' classes, allowing use of 'super()' |
| 63 | __metaclass__ = type |
| 64 | |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 65 | def _strclass(cls): |
| 66 | return "%s.%s" % (cls.__module__, cls.__name__) |
| 67 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 68 | class TestResult: |
| 69 | """Holder for test result information. |
| 70 | |
| 71 | Test results are automatically managed by the TestCase and TestSuite |
| 72 | classes, and do not need to be explicitly manipulated by writers of tests. |
| 73 | |
| 74 | Each instance holds the total number of tests run, and collections of |
| 75 | failures and errors that occurred among those test runs. The collections |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 76 | contain tuples of (testcase, exceptioninfo), where exceptioninfo is the |
Fred Drake | 656f9ec | 2001-09-06 19:13:14 +0000 | [diff] [blame] | 77 | formatted traceback of the error that occurred. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 78 | """ |
| 79 | def __init__(self): |
| 80 | self.failures = [] |
| 81 | self.errors = [] |
| 82 | self.testsRun = 0 |
| 83 | self.shouldStop = 0 |
| 84 | |
| 85 | def startTest(self, test): |
| 86 | "Called when the given test is about to be run" |
| 87 | self.testsRun = self.testsRun + 1 |
| 88 | |
| 89 | def stopTest(self, test): |
| 90 | "Called when the given test has been run" |
| 91 | pass |
| 92 | |
| 93 | def addError(self, test, err): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 94 | """Called when an error has occurred. 'err' is a tuple of values as |
| 95 | returned by sys.exc_info(). |
| 96 | """ |
| 97 | self.errors.append((test, self._exc_info_to_string(err))) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 98 | |
| 99 | def addFailure(self, test, err): |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 100 | """Called when an error has occurred. 'err' is a tuple of values as |
| 101 | returned by sys.exc_info().""" |
| 102 | self.failures.append((test, self._exc_info_to_string(err))) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 103 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 104 | def addSuccess(self, test): |
| 105 | "Called when a test has completed successfully" |
| 106 | pass |
| 107 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 108 | def wasSuccessful(self): |
| 109 | "Tells whether or not this result was a success" |
| 110 | return len(self.failures) == len(self.errors) == 0 |
| 111 | |
| 112 | def stop(self): |
| 113 | "Indicates that the tests should be aborted" |
| 114 | self.shouldStop = 1 |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 115 | |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 116 | def _exc_info_to_string(self, err): |
| 117 | """Converts a sys.exc_info()-style tuple of values into a string.""" |
| 118 | return string.join(apply(traceback.format_exception, err), '') |
| 119 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 120 | def __repr__(self): |
| 121 | return "<%s run=%i errors=%i failures=%i>" % \ |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 122 | (_strclass(self.__class__), self.testsRun, len(self.errors), |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 123 | len(self.failures)) |
| 124 | |
| 125 | |
| 126 | class TestCase: |
| 127 | """A class whose instances are single test cases. |
| 128 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 129 | By default, the test code itself should be placed in a method named |
| 130 | 'runTest'. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 131 | |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 132 | If the fixture may be used for many test cases, create as |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 133 | many test methods as are needed. When instantiating such a TestCase |
| 134 | subclass, specify in the constructor arguments the name of the test method |
| 135 | that the instance is to execute. |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 136 | |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 137 | Test authors should subclass TestCase for their own tests. Construction |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 138 | and deconstruction of the test's environment ('fixture') can be |
| 139 | implemented by overriding the 'setUp' and 'tearDown' methods respectively. |
| 140 | |
| 141 | If it is necessary to override the __init__ method, the base class |
| 142 | __init__ method must always be called. It is important that subclasses |
| 143 | should not change the signature of their __init__ method, since instances |
| 144 | of the classes are instantiated automatically by parts of the framework |
| 145 | in order to be run. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 146 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 147 | |
| 148 | # This attribute determines which exception will be raised when |
| 149 | # the instance's assertion methods fail; test methods raising this |
| 150 | # exception will be deemed to have 'failed' rather than 'errored' |
| 151 | |
| 152 | failureException = AssertionError |
| 153 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 154 | def __init__(self, methodName='runTest'): |
| 155 | """Create an instance of the class that will use the named test |
| 156 | method when executed. Raises a ValueError if the instance does |
| 157 | not have a method with the specified name. |
| 158 | """ |
| 159 | try: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 160 | self.__testMethodName = methodName |
| 161 | testMethod = getattr(self, methodName) |
| 162 | self.__testMethodDoc = testMethod.__doc__ |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 163 | except AttributeError: |
| 164 | raise ValueError, "no such test method in %s: %s" % \ |
| 165 | (self.__class__, methodName) |
| 166 | |
| 167 | def setUp(self): |
| 168 | "Hook method for setting up the test fixture before exercising it." |
| 169 | pass |
| 170 | |
| 171 | def tearDown(self): |
| 172 | "Hook method for deconstructing the test fixture after testing it." |
| 173 | pass |
| 174 | |
| 175 | def countTestCases(self): |
| 176 | return 1 |
| 177 | |
| 178 | def defaultTestResult(self): |
| 179 | return TestResult() |
| 180 | |
| 181 | def shortDescription(self): |
| 182 | """Returns a one-line description of the test, or None if no |
| 183 | description has been provided. |
| 184 | |
| 185 | The default implementation of this method returns the first line of |
| 186 | the specified test method's docstring. |
| 187 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 188 | doc = self.__testMethodDoc |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 189 | return doc and string.strip(string.split(doc, "\n")[0]) or None |
| 190 | |
| 191 | def id(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 192 | return "%s.%s" % (_strclass(self.__class__), self.__testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 193 | |
| 194 | def __str__(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 195 | return "%s (%s)" % (self.__testMethodName, self.__class__) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 196 | |
| 197 | def __repr__(self): |
| 198 | return "<%s testMethod=%s>" % \ |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 199 | (_strclass(self.__class__), self.__testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 200 | |
| 201 | def run(self, result=None): |
| 202 | return self(result) |
| 203 | |
| 204 | def __call__(self, result=None): |
| 205 | if result is None: result = self.defaultTestResult() |
| 206 | result.startTest(self) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 207 | testMethod = getattr(self, self.__testMethodName) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 208 | try: |
| 209 | try: |
| 210 | self.setUp() |
Guido van Rossum | 202dd1e | 2001-12-07 03:39:34 +0000 | [diff] [blame] | 211 | except KeyboardInterrupt: |
| 212 | raise |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 213 | except: |
Jeremy Hylton | efef5da | 2001-10-22 18:14:15 +0000 | [diff] [blame] | 214 | result.addError(self, self.__exc_info()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 215 | return |
| 216 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 217 | ok = 0 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 218 | try: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 219 | testMethod() |
| 220 | ok = 1 |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 221 | except self.failureException, e: |
Jeremy Hylton | efef5da | 2001-10-22 18:14:15 +0000 | [diff] [blame] | 222 | result.addFailure(self, self.__exc_info()) |
Guido van Rossum | 202dd1e | 2001-12-07 03:39:34 +0000 | [diff] [blame] | 223 | except KeyboardInterrupt: |
| 224 | raise |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 225 | except: |
Jeremy Hylton | efef5da | 2001-10-22 18:14:15 +0000 | [diff] [blame] | 226 | result.addError(self, self.__exc_info()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 227 | |
| 228 | try: |
| 229 | self.tearDown() |
Guido van Rossum | 202dd1e | 2001-12-07 03:39:34 +0000 | [diff] [blame] | 230 | except KeyboardInterrupt: |
| 231 | raise |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 232 | except: |
Jeremy Hylton | efef5da | 2001-10-22 18:14:15 +0000 | [diff] [blame] | 233 | result.addError(self, self.__exc_info()) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 234 | ok = 0 |
| 235 | if ok: result.addSuccess(self) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 236 | finally: |
| 237 | result.stopTest(self) |
| 238 | |
| 239 | def debug(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 240 | """Run the test without collecting errors in a TestResult""" |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 241 | self.setUp() |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 242 | getattr(self, self.__testMethodName)() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 243 | self.tearDown() |
| 244 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 245 | def __exc_info(self): |
| 246 | """Return a version of sys.exc_info() with the traceback frame |
| 247 | minimised; usually the top level of the traceback frame is not |
| 248 | needed. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 249 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 250 | exctype, excvalue, tb = sys.exc_info() |
| 251 | if sys.platform[:4] == 'java': ## tracebacks look different in Jython |
| 252 | return (exctype, excvalue, tb) |
| 253 | newtb = tb.tb_next |
| 254 | if newtb is None: |
| 255 | return (exctype, excvalue, tb) |
| 256 | return (exctype, excvalue, newtb) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 257 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 258 | def fail(self, msg=None): |
| 259 | """Fail immediately, with the given message.""" |
| 260 | raise self.failureException, msg |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 261 | |
| 262 | def failIf(self, expr, msg=None): |
| 263 | "Fail the test if the expression is true." |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 264 | if expr: raise self.failureException, msg |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 265 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 266 | def failUnless(self, expr, msg=None): |
| 267 | """Fail the test unless the expression is true.""" |
| 268 | if not expr: raise self.failureException, msg |
| 269 | |
| 270 | def failUnlessRaises(self, excClass, callableObj, *args, **kwargs): |
| 271 | """Fail unless an exception of class excClass is thrown |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 272 | by callableObj when invoked with arguments args and keyword |
| 273 | arguments kwargs. If a different type of exception is |
| 274 | thrown, it will not be caught, and the test case will be |
| 275 | deemed to have suffered an error, exactly as for an |
| 276 | unexpected exception. |
| 277 | """ |
| 278 | try: |
| 279 | apply(callableObj, args, kwargs) |
| 280 | except excClass: |
| 281 | return |
| 282 | else: |
| 283 | if hasattr(excClass,'__name__'): excName = excClass.__name__ |
| 284 | else: excName = str(excClass) |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 285 | raise self.failureException, excName |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 286 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 287 | def failUnlessEqual(self, first, second, msg=None): |
| 288 | """Fail if the two objects are unequal as determined by the '!=' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 289 | operator. |
| 290 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 291 | if first != second: |
Steve Purcell | ca9aaf3 | 2001-12-17 10:13:17 +0000 | [diff] [blame] | 292 | raise self.failureException, \ |
| 293 | (msg or '%s != %s' % (`first`, `second`)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 294 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 295 | def failIfEqual(self, first, second, msg=None): |
| 296 | """Fail if the two objects are equal as determined by the '==' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 297 | operator. |
| 298 | """ |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 299 | if first == second: |
Steve Purcell | ca9aaf3 | 2001-12-17 10:13:17 +0000 | [diff] [blame] | 300 | raise self.failureException, \ |
| 301 | (msg or '%s == %s' % (`first`, `second`)) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 302 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 303 | assertEqual = assertEquals = failUnlessEqual |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 304 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 305 | assertNotEqual = assertNotEquals = failIfEqual |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 306 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 307 | assertRaises = failUnlessRaises |
| 308 | |
| 309 | assert_ = failUnless |
| 310 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 311 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 312 | |
| 313 | class TestSuite: |
| 314 | """A test suite is a composite test consisting of a number of TestCases. |
| 315 | |
| 316 | For use, create an instance of TestSuite, then add test case instances. |
| 317 | When all tests have been added, the suite can be passed to a test |
| 318 | runner, such as TextTestRunner. It will run the individual test cases |
| 319 | in the order in which they were added, aggregating the results. When |
| 320 | subclassing, do not forget to call the base class constructor. |
| 321 | """ |
| 322 | def __init__(self, tests=()): |
| 323 | self._tests = [] |
| 324 | self.addTests(tests) |
| 325 | |
| 326 | def __repr__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 327 | return "<%s tests=%s>" % (_strclass(self.__class__), self._tests) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 328 | |
| 329 | __str__ = __repr__ |
| 330 | |
| 331 | def countTestCases(self): |
| 332 | cases = 0 |
| 333 | for test in self._tests: |
| 334 | cases = cases + test.countTestCases() |
| 335 | return cases |
| 336 | |
| 337 | def addTest(self, test): |
| 338 | self._tests.append(test) |
| 339 | |
| 340 | def addTests(self, tests): |
| 341 | for test in tests: |
| 342 | self.addTest(test) |
| 343 | |
| 344 | def run(self, result): |
| 345 | return self(result) |
| 346 | |
| 347 | def __call__(self, result): |
| 348 | for test in self._tests: |
| 349 | if result.shouldStop: |
| 350 | break |
| 351 | test(result) |
| 352 | return result |
| 353 | |
| 354 | def debug(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 355 | """Run the tests without collecting errors in a TestResult""" |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 356 | for test in self._tests: test.debug() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 357 | |
| 358 | |
| 359 | class FunctionTestCase(TestCase): |
| 360 | """A test case that wraps a test function. |
| 361 | |
| 362 | This is useful for slipping pre-existing test functions into the |
| 363 | PyUnit framework. Optionally, set-up and tidy-up functions can be |
| 364 | supplied. As with TestCase, the tidy-up ('tearDown') function will |
| 365 | always be called if the set-up ('setUp') function ran successfully. |
| 366 | """ |
| 367 | |
| 368 | def __init__(self, testFunc, setUp=None, tearDown=None, |
| 369 | description=None): |
| 370 | TestCase.__init__(self) |
| 371 | self.__setUpFunc = setUp |
| 372 | self.__tearDownFunc = tearDown |
| 373 | self.__testFunc = testFunc |
| 374 | self.__description = description |
| 375 | |
| 376 | def setUp(self): |
| 377 | if self.__setUpFunc is not None: |
| 378 | self.__setUpFunc() |
| 379 | |
| 380 | def tearDown(self): |
| 381 | if self.__tearDownFunc is not None: |
| 382 | self.__tearDownFunc() |
| 383 | |
| 384 | def runTest(self): |
| 385 | self.__testFunc() |
| 386 | |
| 387 | def id(self): |
| 388 | return self.__testFunc.__name__ |
| 389 | |
| 390 | def __str__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 391 | return "%s (%s)" % (_strclass(self.__class__), self.__testFunc.__name__) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 392 | |
| 393 | def __repr__(self): |
Steve Purcell | dc391a6 | 2002-08-09 09:46:23 +0000 | [diff] [blame] | 394 | return "<%s testFunc=%s>" % (_strclass(self.__class__), self.__testFunc) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 395 | |
| 396 | def shortDescription(self): |
| 397 | if self.__description is not None: return self.__description |
| 398 | doc = self.__testFunc.__doc__ |
| 399 | return doc and string.strip(string.split(doc, "\n")[0]) or None |
| 400 | |
| 401 | |
| 402 | |
| 403 | ############################################################################## |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 404 | # Locating and loading tests |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 405 | ############################################################################## |
| 406 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 407 | class TestLoader: |
| 408 | """This class is responsible for loading tests according to various |
| 409 | criteria and returning them wrapped in a Test |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 410 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 411 | testMethodPrefix = 'test' |
| 412 | sortTestMethodsUsing = cmp |
| 413 | suiteClass = TestSuite |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 414 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 415 | def loadTestsFromTestCase(self, testCaseClass): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 416 | """Return a suite of all tests cases contained in testCaseClass""" |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 417 | return self.suiteClass(map(testCaseClass, |
| 418 | self.getTestCaseNames(testCaseClass))) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 419 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 420 | def loadTestsFromModule(self, module): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 421 | """Return a suite of all tests cases contained in the given module""" |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 422 | tests = [] |
| 423 | for name in dir(module): |
| 424 | obj = getattr(module, name) |
| 425 | if type(obj) == types.ClassType and issubclass(obj, TestCase): |
| 426 | tests.append(self.loadTestsFromTestCase(obj)) |
| 427 | return self.suiteClass(tests) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 428 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 429 | def loadTestsFromName(self, name, module=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 430 | """Return a suite of all tests cases given a string specifier. |
| 431 | |
| 432 | The name may resolve either to a module, a test case class, a |
| 433 | test method within a test case class, or a callable object which |
| 434 | returns a TestCase or TestSuite instance. |
Tim Peters | 613b222 | 2001-04-13 05:37:27 +0000 | [diff] [blame] | 435 | |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 436 | The method optionally resolves the names relative to a given module. |
| 437 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 438 | parts = string.split(name, '.') |
| 439 | if module is None: |
| 440 | if not parts: |
| 441 | raise ValueError, "incomplete test name: %s" % name |
| 442 | else: |
Steve Purcell | 17a781b | 2001-04-09 15:37:31 +0000 | [diff] [blame] | 443 | parts_copy = parts[:] |
| 444 | while parts_copy: |
| 445 | try: |
| 446 | module = __import__(string.join(parts_copy,'.')) |
| 447 | break |
| 448 | except ImportError: |
| 449 | del parts_copy[-1] |
| 450 | if not parts_copy: raise |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 451 | parts = parts[1:] |
| 452 | obj = module |
| 453 | for part in parts: |
| 454 | obj = getattr(obj, part) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 455 | |
Steve Purcell | e00dde2 | 2001-08-08 07:57:26 +0000 | [diff] [blame] | 456 | import unittest |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 457 | if type(obj) == types.ModuleType: |
| 458 | return self.loadTestsFromModule(obj) |
Steve Purcell | e00dde2 | 2001-08-08 07:57:26 +0000 | [diff] [blame] | 459 | elif type(obj) == types.ClassType and issubclass(obj, unittest.TestCase): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 460 | return self.loadTestsFromTestCase(obj) |
| 461 | elif type(obj) == types.UnboundMethodType: |
| 462 | return obj.im_class(obj.__name__) |
| 463 | elif callable(obj): |
| 464 | test = obj() |
Steve Purcell | e00dde2 | 2001-08-08 07:57:26 +0000 | [diff] [blame] | 465 | if not isinstance(test, unittest.TestCase) and \ |
| 466 | not isinstance(test, unittest.TestSuite): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 467 | raise ValueError, \ |
Steve Purcell | 4bc8085 | 2001-05-10 01:28:40 +0000 | [diff] [blame] | 468 | "calling %s returned %s, not a test" % (obj,test) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 469 | return test |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 470 | else: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 471 | raise ValueError, "don't know how to make test from: %s" % obj |
| 472 | |
| 473 | def loadTestsFromNames(self, names, module=None): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 474 | """Return a suite of all tests cases found using the given sequence |
| 475 | of string specifiers. See 'loadTestsFromName()'. |
| 476 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 477 | suites = [] |
| 478 | for name in names: |
| 479 | suites.append(self.loadTestsFromName(name, module)) |
| 480 | return self.suiteClass(suites) |
| 481 | |
| 482 | def getTestCaseNames(self, testCaseClass): |
Steve Purcell | 15d8927 | 2001-04-12 09:05:01 +0000 | [diff] [blame] | 483 | """Return a sorted sequence of method names found within testCaseClass |
| 484 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 485 | testFnNames = filter(lambda n,p=self.testMethodPrefix: n[:len(p)] == p, |
| 486 | dir(testCaseClass)) |
| 487 | for baseclass in testCaseClass.__bases__: |
| 488 | for testFnName in self.getTestCaseNames(baseclass): |
| 489 | if testFnName not in testFnNames: # handle overridden methods |
| 490 | testFnNames.append(testFnName) |
| 491 | if self.sortTestMethodsUsing: |
| 492 | testFnNames.sort(self.sortTestMethodsUsing) |
| 493 | return testFnNames |
| 494 | |
| 495 | |
| 496 | |
| 497 | defaultTestLoader = TestLoader() |
| 498 | |
| 499 | |
| 500 | ############################################################################## |
| 501 | # Patches for old functions: these functions should be considered obsolete |
| 502 | ############################################################################## |
| 503 | |
| 504 | def _makeLoader(prefix, sortUsing, suiteClass=None): |
| 505 | loader = TestLoader() |
| 506 | loader.sortTestMethodsUsing = sortUsing |
| 507 | loader.testMethodPrefix = prefix |
| 508 | if suiteClass: loader.suiteClass = suiteClass |
| 509 | return loader |
| 510 | |
| 511 | def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp): |
| 512 | return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) |
| 513 | |
| 514 | def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite): |
| 515 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass) |
| 516 | |
| 517 | def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite): |
| 518 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 519 | |
| 520 | |
| 521 | ############################################################################## |
| 522 | # Text UI |
| 523 | ############################################################################## |
| 524 | |
| 525 | class _WritelnDecorator: |
| 526 | """Used to decorate file-like objects with a handy 'writeln' method""" |
| 527 | def __init__(self,stream): |
| 528 | self.stream = stream |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 529 | |
| 530 | def __getattr__(self, attr): |
| 531 | return getattr(self.stream,attr) |
| 532 | |
| 533 | def writeln(self, *args): |
| 534 | if args: apply(self.write, args) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 535 | self.write('\n') # text-mode streams translate to \r\n if needed |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 536 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 537 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 538 | class _TextTestResult(TestResult): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 539 | """A test result class that can print formatted text results to a stream. |
| 540 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 541 | Used by TextTestRunner. |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 542 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 543 | separator1 = '=' * 70 |
| 544 | separator2 = '-' * 70 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 545 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 546 | def __init__(self, stream, descriptions, verbosity): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 547 | TestResult.__init__(self) |
| 548 | self.stream = stream |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 549 | self.showAll = verbosity > 1 |
| 550 | self.dots = verbosity == 1 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 551 | self.descriptions = descriptions |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 552 | |
| 553 | def getDescription(self, test): |
| 554 | if self.descriptions: |
| 555 | return test.shortDescription() or str(test) |
| 556 | else: |
| 557 | return str(test) |
| 558 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 559 | def startTest(self, test): |
| 560 | TestResult.startTest(self, test) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 561 | if self.showAll: |
| 562 | self.stream.write(self.getDescription(test)) |
| 563 | self.stream.write(" ... ") |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 564 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 565 | def addSuccess(self, test): |
| 566 | TestResult.addSuccess(self, test) |
| 567 | if self.showAll: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 568 | self.stream.writeln("ok") |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 569 | elif self.dots: |
| 570 | self.stream.write('.') |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 571 | |
| 572 | def addError(self, test, err): |
| 573 | TestResult.addError(self, test, err) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 574 | if self.showAll: |
| 575 | self.stream.writeln("ERROR") |
| 576 | elif self.dots: |
| 577 | self.stream.write('E') |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 578 | |
| 579 | def addFailure(self, test, err): |
| 580 | TestResult.addFailure(self, test, err) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 581 | if self.showAll: |
| 582 | self.stream.writeln("FAIL") |
| 583 | elif self.dots: |
| 584 | self.stream.write('F') |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 585 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 586 | def printErrors(self): |
| 587 | if self.dots or self.showAll: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 588 | self.stream.writeln() |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 589 | self.printErrorList('ERROR', self.errors) |
| 590 | self.printErrorList('FAIL', self.failures) |
| 591 | |
| 592 | def printErrorList(self, flavour, errors): |
| 593 | for test, err in errors: |
| 594 | self.stream.writeln(self.separator1) |
| 595 | self.stream.writeln("%s: %s" % (flavour,self.getDescription(test))) |
| 596 | self.stream.writeln(self.separator2) |
Steve Purcell | 7b06570 | 2001-09-06 08:24:40 +0000 | [diff] [blame] | 597 | self.stream.writeln("%s" % err) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 598 | |
| 599 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 600 | class TextTestRunner: |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 601 | """A test runner class that displays results in textual form. |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 602 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 603 | It prints out the names of tests as they are run, errors as they |
| 604 | occur, and a summary of the results at the end of the test run. |
| 605 | """ |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 606 | def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 607 | self.stream = _WritelnDecorator(stream) |
| 608 | self.descriptions = descriptions |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 609 | self.verbosity = verbosity |
| 610 | |
| 611 | def _makeResult(self): |
| 612 | return _TextTestResult(self.stream, self.descriptions, self.verbosity) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 613 | |
| 614 | def run(self, test): |
| 615 | "Run the given test case or test suite." |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 616 | result = self._makeResult() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 617 | startTime = time.time() |
| 618 | test(result) |
| 619 | stopTime = time.time() |
| 620 | timeTaken = float(stopTime - startTime) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 621 | result.printErrors() |
| 622 | self.stream.writeln(result.separator2) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 623 | run = result.testsRun |
| 624 | self.stream.writeln("Ran %d test%s in %.3fs" % |
Neal Norwitz | 7616504 | 2002-05-31 14:15:11 +0000 | [diff] [blame] | 625 | (run, run != 1 and "s" or "", timeTaken)) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 626 | self.stream.writeln() |
| 627 | if not result.wasSuccessful(): |
| 628 | self.stream.write("FAILED (") |
| 629 | failed, errored = map(len, (result.failures, result.errors)) |
| 630 | if failed: |
| 631 | self.stream.write("failures=%d" % failed) |
| 632 | if errored: |
| 633 | if failed: self.stream.write(", ") |
| 634 | self.stream.write("errors=%d" % errored) |
| 635 | self.stream.writeln(")") |
| 636 | else: |
| 637 | self.stream.writeln("OK") |
| 638 | return result |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 639 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 640 | |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 641 | |
| 642 | ############################################################################## |
| 643 | # Facilities for running tests from the command line |
| 644 | ############################################################################## |
| 645 | |
| 646 | class TestProgram: |
| 647 | """A command-line program that runs a set of tests; this is primarily |
| 648 | for making test modules conveniently executable. |
| 649 | """ |
| 650 | USAGE = """\ |
Steve Purcell | 17a781b | 2001-04-09 15:37:31 +0000 | [diff] [blame] | 651 | Usage: %(progName)s [options] [test] [...] |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 652 | |
| 653 | Options: |
| 654 | -h, --help Show this message |
| 655 | -v, --verbose Verbose output |
| 656 | -q, --quiet Minimal output |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 657 | |
| 658 | Examples: |
| 659 | %(progName)s - run default set of tests |
| 660 | %(progName)s MyTestSuite - run suite 'MyTestSuite' |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 661 | %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething |
| 662 | %(progName)s MyTestCase - run all 'test*' test methods |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 663 | in MyTestCase |
| 664 | """ |
| 665 | def __init__(self, module='__main__', defaultTest=None, |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 666 | argv=None, testRunner=None, testLoader=defaultTestLoader): |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 667 | if type(module) == type(''): |
| 668 | self.module = __import__(module) |
| 669 | for part in string.split(module,'.')[1:]: |
| 670 | self.module = getattr(self.module, part) |
| 671 | else: |
| 672 | self.module = module |
| 673 | if argv is None: |
| 674 | argv = sys.argv |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 675 | self.verbosity = 1 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 676 | self.defaultTest = defaultTest |
| 677 | self.testRunner = testRunner |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 678 | self.testLoader = testLoader |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 679 | self.progName = os.path.basename(argv[0]) |
| 680 | self.parseArgs(argv) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 681 | self.runTests() |
| 682 | |
| 683 | def usageExit(self, msg=None): |
| 684 | if msg: print msg |
| 685 | print self.USAGE % self.__dict__ |
| 686 | sys.exit(2) |
| 687 | |
| 688 | def parseArgs(self, argv): |
| 689 | import getopt |
| 690 | try: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 691 | options, args = getopt.getopt(argv[1:], 'hHvq', |
| 692 | ['help','verbose','quiet']) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 693 | for opt, value in options: |
| 694 | if opt in ('-h','-H','--help'): |
| 695 | self.usageExit() |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 696 | if opt in ('-q','--quiet'): |
| 697 | self.verbosity = 0 |
| 698 | if opt in ('-v','--verbose'): |
| 699 | self.verbosity = 2 |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 700 | if len(args) == 0 and self.defaultTest is None: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 701 | self.test = self.testLoader.loadTestsFromModule(self.module) |
| 702 | return |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 703 | if len(args) > 0: |
| 704 | self.testNames = args |
| 705 | else: |
| 706 | self.testNames = (self.defaultTest,) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 707 | self.createTests() |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 708 | except getopt.error, msg: |
| 709 | self.usageExit(msg) |
| 710 | |
| 711 | def createTests(self): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 712 | self.test = self.testLoader.loadTestsFromNames(self.testNames, |
| 713 | self.module) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 714 | |
| 715 | def runTests(self): |
| 716 | if self.testRunner is None: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 717 | self.testRunner = TextTestRunner(verbosity=self.verbosity) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 718 | result = self.testRunner.run(self.test) |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 719 | sys.exit(not result.wasSuccessful()) |
Fred Drake | 0253820 | 2001-03-21 18:09:46 +0000 | [diff] [blame] | 720 | |
| 721 | main = TestProgram |
| 722 | |
| 723 | |
| 724 | ############################################################################## |
| 725 | # Executing this module from the command line |
| 726 | ############################################################################## |
| 727 | |
| 728 | if __name__ == "__main__": |
| 729 | main(module=None) |