Mercurial > p > roundup > code
annotate test/unittest.py @ 212:862dafca2a72
changes
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Mon, 06 Aug 2001 23:58:33 +0000 |
| parents | 40d7be9708f6 |
| children | 6978960e8e06 |
| 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 print 'hi' |
|
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 |
|
40d7be9708f6
Am now bundling unittest with the package so everyone can use the unit tests.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
49 __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
|
50 __email__ = "stephen_purcell at yahoo dot com" |
|
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 __version__ = "$Revision: 1.1 $"[11:-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
|
52 |
|
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 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 |
|
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 # 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
|
62 ############################################################################## |
|
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 |
|
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 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
|
65 """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
|
66 |
|
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 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
|
68 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
|
69 |
|
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 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
|
71 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
|
72 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
|
73 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
|
74 """ |
|
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 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 |
|
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 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
|
82 "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
|
83 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
|
84 |
|
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 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
|
86 "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
|
87 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
|
88 |
|
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 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
|
90 "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
|
91 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
|
92 |
|
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 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
|
94 "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
|
95 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
|
96 |
|
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 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
|
98 "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
|
99 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
|
100 |
|
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 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
|
102 "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
|
103 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
|
104 |
|
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 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
|
106 "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
|
107 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
|
108 |
|
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 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
|
110 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
|
111 (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
|
112 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
|
113 |
|
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 |
|
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 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
|
116 """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
|
117 |
|
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 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
|
119 '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
|
120 |
|
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 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
|
122 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
|
123 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
|
124 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
|
125 |
|
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 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
|
127 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
|
128 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
|
129 |
|
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 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
|
131 __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
|
132 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
|
133 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
|
134 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
|
135 """ |
|
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 |
|
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 # 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
|
138 # 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
|
139 # 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
|
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 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
|
142 |
|
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 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
|
144 """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
|
145 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
|
146 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
|
147 """ |
|
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 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 (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
|
155 |
|
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 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
|
157 "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
|
158 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
|
159 |
|
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 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
|
161 "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
|
162 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
|
163 |
|
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 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
|
165 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
|
166 |
|
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 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
|
168 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
|
169 |
|
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 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
|
171 """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
|
172 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
|
173 |
|
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 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
|
175 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
|
176 """ |
|
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 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
|
178 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
|
179 |
|
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 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
|
181 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
|
182 |
|
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 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
|
184 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
|
185 |
|
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 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
|
187 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
|
188 (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
|
189 |
|
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 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
|
191 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
|
192 |
|
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 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 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
|
203 |
|
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 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 |
|
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 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 |
|
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 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
|
223 """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
|
224 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
|
225 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
|
226 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
|
227 |
|
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 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
|
229 """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
|
230 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
|
231 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
|
232 """ |
|
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 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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 |
|
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 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
|
242 """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
|
243 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
|
244 |
|
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 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
|
246 "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
|
247 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
|
248 |
|
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 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
|
250 """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
|
251 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
|
252 |
|
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 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
|
254 """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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 """ |
|
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 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
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 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
|
268 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
|
269 |
|
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 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
|
271 """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
|
272 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
|
273 """ |
|
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 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
|
275 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
|
276 |
|
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 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
|
278 """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
|
279 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
|
280 """ |
|
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 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
|
282 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
|
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 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
|
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 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
|
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 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
|
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 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
|
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 |
|
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 |
|
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 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
|
295 """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
|
296 |
|
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 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
|
298 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
|
299 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
|
300 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
|
301 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
|
302 """ |
|
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 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
|
304 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
|
305 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
|
306 |
|
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 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
|
308 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
|
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 __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
|
311 |
|
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 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 |
|
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 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
|
319 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
|
320 |
|
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 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
|
322 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
|
323 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
|
324 |
|
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 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
|
326 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
|
327 |
|
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 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 |
|
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 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
|
336 """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
|
337 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
|
338 |
|
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 |
|
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 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
|
341 """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
|
342 |
|
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 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
|
344 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
|
345 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
|
346 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
|
347 """ |
|
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 |
|
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 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
|
350 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
|
351 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
|
352 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
|
353 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
|
354 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
|
355 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
|
356 |
|
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 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
|
358 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
|
359 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
|
360 |
|
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 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
|
362 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
|
363 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
|
364 |
|
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 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
|
366 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
|
367 |
|
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 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
|
369 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
|
370 |
|
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 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
|
372 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
|
373 |
|
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 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
|
375 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
|
376 |
|
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 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
|
378 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
|
379 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
|
380 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
|
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 |
|
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 # 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
|
386 ############################################################################## |
|
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 |
|
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 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
|
389 """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
|
390 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
|
391 """ |
|
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 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
|
393 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
|
394 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
|
395 |
|
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 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
|
397 """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
|
398 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
|
399 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
|
400 |
|
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 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
|
402 """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
|
403 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
|
404 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
|
405 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
|
406 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
|
407 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
|
408 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
|
409 |
|
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 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
|
411 """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
|
412 |
|
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 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
|
414 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
|
415 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
|
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 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
|
418 """ |
|
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 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
|
420 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
|
421 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
|
422 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
|
423 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
|
424 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
|
425 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
|
426 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
|
427 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
|
428 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
|
429 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
|
430 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
|
431 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
|
432 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
|
433 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
|
434 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
|
435 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
|
436 |
|
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 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
|
438 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
|
439 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
|
440 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
|
441 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
|
442 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
|
443 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
|
444 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
|
445 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
|
446 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
|
447 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
|
448 "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
|
449 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
|
450 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
|
451 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
|
452 |
|
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 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
|
454 """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
|
455 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
|
456 """ |
|
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 = [] |
|
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 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
|
459 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
|
460 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
|
461 |
|
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 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
|
463 """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
|
464 """ |
|
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 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
|
466 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
|
467 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
|
468 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
|
469 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
|
470 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
|
471 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
|
472 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
|
473 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
|
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 |
|
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 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
|
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 |
|
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 # 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
|
482 ############################################################################## |
|
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 |
|
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 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
|
485 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
|
486 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
|
487 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
|
488 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
|
489 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
|
490 |
|
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 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
|
492 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
|
493 |
|
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 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
|
495 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
|
496 |
|
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 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
|
498 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
|
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 |
|
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 # 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
|
503 ############################################################################## |
|
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 |
|
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 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
|
506 """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
|
507 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
|
508 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
|
509 |
|
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 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
|
511 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
|
512 |
|
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 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
|
514 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
|
515 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
|
516 |
|
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 |
|
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 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
|
519 """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
|
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 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
|
522 """ |
|
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 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
|
524 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
|
525 |
|
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 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
|
527 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
|
528 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
|
529 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
|
530 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
|
531 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
|
532 |
|
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 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
|
534 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
|
535 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
|
536 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
|
537 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
|
538 |
|
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 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
|
540 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
|
541 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
|
542 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
|
543 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
|
544 |
|
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 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 |
|
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 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
|
553 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
|
554 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
|
555 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
|
556 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
|
557 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
|
558 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
|
559 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
|
560 |
|
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 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
|
562 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
|
563 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
|
564 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
|
565 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
|
566 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
|
567 |
|
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 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
|
569 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
|
570 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
|
571 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
|
572 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
|
573 |
|
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 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
|
575 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
|
576 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
|
577 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
|
578 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
|
579 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
|
580 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
|
581 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
|
582 |
|
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 |
|
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 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
|
585 """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
|
586 |
|
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 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
|
588 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
|
589 """ |
|
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 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
|
591 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
|
592 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
|
593 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
|
594 |
|
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 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
|
596 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
|
597 |
|
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 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
|
599 "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
|
600 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
|
601 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
|
602 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
|
603 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
|
604 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
|
605 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
|
606 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
|
607 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
|
608 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
|
609 (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
|
610 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
|
611 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
|
612 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
|
613 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
|
614 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
|
615 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
|
616 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
|
617 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
|
618 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
|
619 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
|
620 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
|
621 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
|
622 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
|
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 |
|
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 # 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
|
628 ############################################################################## |
|
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 |
|
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 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
|
631 """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
|
632 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
|
633 """ |
|
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 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
|
635 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
|
636 |
|
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 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
|
638 -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
|
639 -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
|
640 -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
|
641 |
|
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 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
|
643 %(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
|
644 %(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
|
645 %(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
|
646 %(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
|
647 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
|
648 """ |
|
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 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
|
650 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
|
651 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
|
652 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
|
653 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
|
654 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
|
655 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
|
656 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
|
657 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
|
658 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
|
659 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
|
660 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
|
661 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
|
662 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
|
663 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
|
664 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
|
665 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
|
666 |
|
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 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
|
668 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
|
669 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
|
670 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
|
671 |
|
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 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
|
673 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
|
674 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
|
675 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
|
676 ['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
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 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
|
683 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
|
684 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
|
685 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
|
686 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
|
687 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
|
688 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
|
689 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
|
690 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
|
691 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
|
692 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
|
693 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
|
694 |
|
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 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
|
696 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
|
697 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
|
698 |
|
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 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
|
700 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
|
701 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
|
702 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
|
703 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
|
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 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
|
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 |
|
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 # 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
|
710 ############################################################################## |
|
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 |
|
40d7be9708f6
Am now bundling unittest with the package so everyone can use the unit tests.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
712 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
|
713 main(module=None) |
