21 |
thisdir = os.path.dirname(__file__) |
thisdir = os.path.dirname(__file__) |
22 |
return os.path.join(thisdir, os.pardir) |
return os.path.join(thisdir, os.pardir) |
23 |
|
|
24 |
|
def resource_dir(): |
25 |
|
return os.path.join(thuban_dir(), "Resources") |
26 |
|
|
27 |
def add_thuban_dir_to_path(): |
def add_thuban_dir_to_path(): |
28 |
"""Insert the Thuban directory at the beginning of the python path. |
"""Insert the Thuban directory at the beginning of the python path. |
60 |
# This has to be in a finally clause because unittest.main() |
# This has to be in a finally clause because unittest.main() |
61 |
# 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 |
62 |
# an appropriate exit code |
# an appropriate exit code |
63 |
print_garbage_information() |
print_additional_summary() |
64 |
|
|
65 |
|
def print_additional_summary(): |
66 |
|
"""Print some additional summary information after tests have been run""" |
67 |
|
print_garbage_information() |
68 |
|
import xmlsupport |
69 |
|
xmlsupport.print_summary_message() |
70 |
|
|
71 |
def print_garbage_information(): |
def print_garbage_information(): |
72 |
"""Print information about things that haven't been cleaned up. |
"""Print information about things that haven't been cleaned up. |
116 |
return create_temp_dir() |
return create_temp_dir() |
117 |
|
|
118 |
|
|
119 |
class FloatComparisonMixin: |
|
120 |
|
class FileLoadTestCase(unittest.TestCase, FileTestMixin): |
121 |
|
|
122 |
|
"""Base class for test case that test loading files. |
123 |
|
|
124 |
|
This base class provides some common infrastructure for testing the |
125 |
|
reading of files. |
126 |
|
|
127 |
|
Each test case should be its own class derived from this one. There |
128 |
|
is one file associated with each class. The contents are defined by |
129 |
|
the file_contents class attribute and its name by the filename |
130 |
|
method. |
131 |
|
|
132 |
|
Derived classes usually only have to provide appropriate values for |
133 |
|
the file_contents and file_extension class attributes. |
134 |
|
""" |
135 |
|
|
136 |
|
file_contents = None |
137 |
|
file_extension = "" |
138 |
|
|
139 |
|
def filename(self): |
140 |
|
"""Return the name of the test file to use. |
141 |
|
|
142 |
|
The default implementation simply calls self.volatile_file_name |
143 |
|
with a basename derived from the class name by stripping off a |
144 |
|
leading 'test_' and appending self.file_extension. |
145 |
|
""" |
146 |
|
name = self.__class__.__name__ |
147 |
|
if name.startswith("test_"): |
148 |
|
name = name[5:] |
149 |
|
return self.temp_file_name(name + self.file_extension) |
150 |
|
|
151 |
|
def setUp(self): |
152 |
|
"""Create the volatile file for the test. |
153 |
|
|
154 |
|
Write self.contents (which should be a string) to the file named |
155 |
|
by self.filename(). |
156 |
|
""" |
157 |
|
filename = self.filename() |
158 |
|
file = open(filename, "w") |
159 |
|
file.write(self.file_contents) |
160 |
|
file.close() |
161 |
|
|
162 |
|
|
163 |
|
class FloatComparisonMixin(unittest.TestCase): |
164 |
|
|
165 |
""" |
""" |
166 |
Mixin class for tests comparing floating point numbers. |
Mixin class for tests comparing floating point numbers. |
170 |
""" |
""" |
171 |
|
|
172 |
fp_epsilon = 1e-6 |
fp_epsilon = 1e-6 |
173 |
|
fp_inf = float('1e1000') # FIXME: hack for infinite |
174 |
|
|
175 |
def assertFloatEqual(self, test, value): |
def assertFloatEqual(self, test, value): |
176 |
"""Assert equality of test and value with some tolerance. |
"""Assert equality of test and value with some tolerance. |
178 |
Assert that the absolute difference between test and value is |
Assert that the absolute difference between test and value is |
179 |
less than self.fp_epsilon. |
less than self.fp_epsilon. |
180 |
""" |
""" |
181 |
self.assert_(self.fp_epsilon > abs(test - value), |
if abs(test) == self.fp_inf: |
182 |
|
self.assertEqual(test, value) |
183 |
|
else: |
184 |
|
self.assert_(self.fp_epsilon > abs(test - value), |
185 |
"abs(%g - %g) >= %g" % (test, value, self.fp_epsilon)) |
"abs(%g - %g) >= %g" % (test, value, self.fp_epsilon)) |
186 |
|
|
187 |
def assertFloatSeqEqual(self, test, value, epsilon = None): |
def assertFloatSeqEqual(self, test, value, epsilon = None): |
237 |
def check_messages(self, messages): |
def check_messages(self, messages): |
238 |
"""Check whether the messages received match the list messages""" |
"""Check whether the messages received match the list messages""" |
239 |
self.assertEquals(messages, self.received_messages) |
self.assertEquals(messages, self.received_messages) |
240 |
|
|