annotate test/unittest.py @ 670:e06ee3dd5ee8 0.4.1

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

Roundup Issue Tracker: http://roundup-tracker.org/