15 |
|
|
16 |
import os, sys |
import os, sys |
17 |
import unittest |
import unittest |
18 |
|
import traceback |
19 |
|
|
20 |
def thuban_dir(): |
def thuban_dir(): |
21 |
"""Return the directory containing the Thuban package""" |
"""Return the directory containing the Thuban package""" |
49 |
import thubaninit |
import thubaninit |
50 |
_initthuban_done = 1 |
_initthuban_done = 1 |
51 |
|
|
|
def run_tests(): |
|
|
"""Frontend for unittest.main that prints some additional debug information |
|
52 |
|
|
53 |
After calling unittest.main, run the garbage collector and print |
# |
54 |
uncollected objects. Also print any un-unsubscribed messages. |
# Special test runner and result that support skipping tests |
55 |
|
# |
56 |
|
|
57 |
|
class SkipTest(Exception): |
58 |
|
"""Exception to raise in tests that are skipped for some reason |
59 |
|
|
60 |
|
For instance, since gdal support is optional, test cases that |
61 |
|
require gdal raise this exception to indicate that they are skipped. |
62 |
|
Skipped is different from failure or error in that it is expected |
63 |
|
under certain circumstances. |
64 |
|
""" |
65 |
|
|
66 |
|
class ThubanTestResult(unittest._TextTestResult): |
67 |
|
|
68 |
|
def __init__(self, stream, descriptions, verbosity): |
69 |
|
unittest._TextTestResult.__init__(self, stream, descriptions, |
70 |
|
verbosity) |
71 |
|
self.skipped_tests = {} |
72 |
|
|
73 |
|
def add_skipped_test(self, test, exc): |
74 |
|
reason = str(exc) |
75 |
|
self.skipped_tests.setdefault(reason, []).append(test) |
76 |
|
|
77 |
|
def count_skipped(self): |
78 |
|
sum = 0 |
79 |
|
for tests in self.skipped_tests.values(): |
80 |
|
sum += len(tests) |
81 |
|
return sum |
82 |
|
|
83 |
|
def addError(self, test, err): |
84 |
|
"""Extend inherited method to handle SkipTest exceptions specially |
85 |
|
""" |
86 |
|
#print "addError", test, err |
87 |
|
if isinstance(err[1], SkipTest): |
88 |
|
self.add_skipped_test(test, err[1]) |
89 |
|
if self.showAll: |
90 |
|
self.stream.writeln("skipped") |
91 |
|
elif self.dots: |
92 |
|
self.stream.write('S') |
93 |
|
else: |
94 |
|
unittest._TextTestResult.addError(self, test, err) |
95 |
|
|
96 |
|
def printErrors(self): |
97 |
|
if self.skipped_tests: |
98 |
|
if self.dots or self.showAll: |
99 |
|
self.stream.writeln() |
100 |
|
self.stream.writeln("Skipped tests:") |
101 |
|
for reason, tests in self.skipped_tests.items(): |
102 |
|
self.stream.writeln(" %s:" % reason) |
103 |
|
for test in tests: |
104 |
|
self.stream.writeln(" " + test.id()) |
105 |
|
unittest._TextTestResult.printErrors(self) |
106 |
|
|
107 |
|
|
108 |
|
class ThubanTestRunner(unittest.TextTestRunner): |
109 |
|
|
110 |
|
def _makeResult(self): |
111 |
|
return ThubanTestResult(self.stream, self.descriptions, self.verbosity) |
112 |
|
|
113 |
|
def run(self, test): |
114 |
|
result = unittest.TextTestRunner.run(self, test) |
115 |
|
self.stream.writeln("skipped = %d" % result.count_skipped()) |
116 |
|
return result |
117 |
|
|
118 |
|
|
119 |
|
class ThubanTestProgram(unittest.TestProgram): |
120 |
|
|
121 |
|
def runTests(self): |
122 |
|
"""Extend inherited method so that we use a ThubanTestRunner""" |
123 |
|
print "ThubanTestProgram.runTests" |
124 |
|
self.testRunner = ThubanTestRunner(verbosity = self.verbosity) |
125 |
|
unittest.TestProgram.runTests(self) |
126 |
|
|
127 |
|
|
128 |
|
def execute_as_testsuite(callable, *args, **kw): |
129 |
|
"""Call callable with args as if it were the entry point to the test suite |
130 |
|
|
131 |
|
Return watever callable returns. |
132 |
|
|
133 |
|
This is a helper function for run_tests and runtests.py. Call |
134 |
|
callable in a try-finally block and run some cleanup and print some |
135 |
|
additional information in the finally block. |
136 |
|
|
137 |
|
The additionaly information include: |
138 |
|
|
139 |
|
- A list of uncollected objects (after an explicit garbage |
140 |
|
collector call) |
141 |
|
|
142 |
|
- any unsubscribed messages |
143 |
""" |
""" |
144 |
try: |
try: |
145 |
unittest.main() |
return callable(*args, **kw) |
146 |
finally: |
finally: |
147 |
# This has to be in a finally clause because unittest.main() |
# This has to be in a finally clause because unittest.main() |
148 |
# ends with a sys.exit to make sure that the process exits with |
# ends with a sys.exit to make sure that the process exits with |
149 |
# an appropriate exit code |
# an appropriate exit code |
150 |
|
|
151 |
|
# Print additional information |
152 |
print_additional_summary() |
print_additional_summary() |
153 |
|
|
154 |
|
def run_tests(): |
155 |
|
"""Frontend for unittest.main that prints some additional debug information |
156 |
|
|
157 |
|
After calling unittest.main, run the garbage collector and print |
158 |
|
uncollected objects. Also print any un-unsubscribed messages. |
159 |
|
""" |
160 |
|
execute_as_testsuite(ThubanTestProgram) |
161 |
|
|
162 |
|
|
163 |
def print_additional_summary(): |
def print_additional_summary(): |
164 |
"""Print some additional summary information after tests have been run""" |
"""Print some additional summary information after tests have been run""" |
165 |
print_garbage_information() |
print_garbage_information() |
180 |
print gc.garbage |
print gc.garbage |
181 |
Thuban.Lib.connector._the_connector.print_connections() |
Thuban.Lib.connector._the_connector.print_connections() |
182 |
|
|
183 |
|
# |
184 |
|
|
185 |
def create_temp_dir(): |
def create_temp_dir(): |
186 |
"""Create a temporary directory and return its name. |
"""Create a temporary directory and return its name. |
259 |
file.close() |
file.close() |
260 |
|
|
261 |
|
|
262 |
class FloatComparisonMixin(unittest.TestCase): |
class FloatComparisonMixin: |
263 |
|
|
264 |
""" |
""" |
265 |
Mixin class for tests comparing floating point numbers. |
Mixin class for tests comparing floating point numbers. |
270 |
|
|
271 |
fp_epsilon = 1e-6 |
fp_epsilon = 1e-6 |
272 |
fp_inf = float('1e1000') # FIXME: hack for infinite |
fp_inf = float('1e1000') # FIXME: hack for infinite |
273 |
|
|
274 |
def assertFloatEqual(self, test, value, epsilon = None): |
def assertFloatEqual(self, test, value, epsilon = None): |
275 |
"""Assert equality of test and value with some tolerance. |
"""Assert equality of test and value with some tolerance. |
276 |
|
|
295 |
for i in range(len(test)): |
for i in range(len(test)): |
296 |
self.assertFloatEqual(test[i], value[i], epsilon) |
self.assertFloatEqual(test[i], value[i], epsilon) |
297 |
|
|
298 |
|
def assertPointListEquals(self, test, value): |
299 |
|
"""Assert equality of two lists of lists of tuples of float |
300 |
|
|
301 |
|
This assertion is usually used to compare the geometry of shapes |
302 |
|
as returned by a Shape object's Points() method, hence the name. |
303 |
|
""" |
304 |
|
for i in range(len(test)): |
305 |
|
self.assertEquals(len(test[i]), len(value[i])) |
306 |
|
for j in range(len(test[i])): |
307 |
|
self.assertFloatSeqEqual(test[i][j], value[i][j]) |
308 |
|
|
309 |
|
|
310 |
class SubscriberMixin: |
class SubscriberMixin: |
311 |
|
|
312 |
"""Mixin class for tests for messages sent through the Connector |
"""Mixin class for tests for messages sent through the Connector |