1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import dep_check
24 from re import search, split
25 import sys
26 from time import time
27 from unittest import TextTestRunner
28 if dep_check.wx_module:
29 import wx
30
31
32 from lib.compat import StringIO
33 from lib.compat import TextTestResult
34 from status import Status; status = Status()
35
36
38 """A replacement for the TextTestResult class.
39
40 This class is designed to catch STDOUT and STDERR during the execution of each test and to
41 prepend the output to the failure and error reports normally generated by TextTestRunner.
42 """
43
44 - def __init__(self, stream, descriptions, verbosity, timing=False, category=None):
45 """Initialise the RelaxTestResult object with relax specific variables.
46
47 @keyword timing: A flag which if True will enable timing of individual tests.
48 @type timing: bool
49 @keyword category: The type of test being performed, to allow the printouts to be changed. This can be one of 'system', 'unit', 'gui', or 'verification'. the printout.
50 @type category: str
51 """
52
53
54 super(RelaxTestResult, self).__init__(stream, descriptions, verbosity)
55
56
57 self.timing_flag = timing
58 self.category = category
59
60
62 """Override of the TestResult.addError() method.
63
64 The STDOUT and STDERR captured text is prepended to the error text here.
65
66
67 @param test: The test object.
68 @type test: TestCase instance
69 @param err: A tuple of values as returned by sys.exc_info().
70 @type err: tuple of values
71 """
72
73
74 super(RelaxTestResult, self).addError(test, err)
75
76
77 self.errors[-1] = (self.errors[-1][0], self.capt.getvalue() + self.errors[-1][1])
78
79
80 if self.timing_flag:
81 self.write_time(test.id())
82
83
85 """Override of the TestResult.addFailure() method.
86
87 The STDOUT and STDERR captured text is prepended to the failure text here.
88
89
90 @param test: The test object.
91 @type test: TestCase instance
92 @param err: A tuple of values as returned by sys.exc_info().
93 @type err: tuple of values
94 """
95
96
97 super(RelaxTestResult, self).addFailure(test, err)
98
99
100 self.failures[-1] = (self.failures[-1][0], self.capt.getvalue() + self.failures[-1][1])
101
102
103 if self.timing_flag:
104 self.write_time(test.id())
105
106
108 """The method for a successful test.
109
110 @param test: The test object.
111 @type test: TestCase instance
112 """
113
114
115 super(RelaxTestResult, self).addSuccess(test)
116
117
118 if self.timing_flag:
119 self.write_time(test.id())
120
121
123 """Override of the TextTestResult.startTest() method.
124
125 The start of STDOUT and STDERR capture occurs here.
126 """
127
128
129 self.orig_stdout = sys.stdout
130 self.orig_stderr = sys.stderr
131
132
133 self.capt = StringIO()
134 if not status.debug:
135 sys.stdout = self.capt
136 sys.stderr = self.capt
137
138
139 status.exec_lock.test_name = str(test)
140
141
142 if self.timing_flag:
143 self.time = time()
144
145
146 super(RelaxTestResult, self).startTest(test)
147
148
150 """Override of the TextTestResult.stopTest() method.
151
152 The end of STDOUT and STDERR capture occurs here.
153 """
154
155
156 sys.stdout = self.orig_stdout
157 sys.stderr = self.orig_stderr
158
159
161 """Write the timing of the test to the stream.
162
163 @param test_name: The TestCase name.
164 @type test_name: str
165 """
166
167
168 self.time -= time()
169
170
171 if self.category != 'unit':
172 test_name = test_name.split('.')
173 test_name = "%s.%s" % (test_name[-2], test_name[-1])
174
175
176 elif search('Error', test_name):
177 pass
178
179
180 else:
181
182 test_name = test_name.replace('test_suite.unit_tests.', '')
183
184
185 module_name, test_name = split('.Test_', test_name)
186
187
188 test_name = "module %s, test Test_%s" % (module_name, test_name)
189
190
191 if self.category == 'unit':
192 time_str = '%7.2f ms' % (abs(self.time) * 1000)
193 else:
194 time_str = '%7.2f s' % abs(self.time)
195 self.stream.write(' %s for %s\n' % (time_str, test_name))
196
197
198
200 """A replacement for the TextTestResult class for the GUI."""
201
203 """Override of the RelaxTestResult.stopTest() method.
204
205 The end of STDOUT and STDERR capture occurs here.
206 """
207
208
209 super(GuiTestResult, self).stopTest(test)
210
211
212 wx.GetApp().Yield(True)
213
214
215
217 """A replacement unittest runner.
218
219 This runner is designed to catch STDOUT during the execution of each test and to prepend the
220 output to the failure and error reports normally generated by TextTestRunner.
221 """
222
223
224 category = None
225
226 - def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, timing=False):
227 """Initialise the class, storing the timing flag.
228
229 @keyword timing: A flag which if True will enable timing of individual tests.
230 @type timing: bool
231 """
232
233
234 if (sys.version_info[0] == 3 and sys.version_info[1] == 1) or (sys.version_info[0] == 2 and sys.version_info[1] <= 6):
235 super(RelaxTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity)
236 else:
237 super(RelaxTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity, failfast=failfast, buffer=buffer, resultclass=resultclass)
238
239
240 self.timing_flag = timing
241
242
247
248
249
251 """A replacement unittest runner.
252
253 This runner is designed to catch STDOUT during the execution of each test and to prepend the
254 output to the failure and error reports normally generated by TextTestRunner.
255 """
256
257 - def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, timing=False):
258 """Initialise the class, storing the timing flag.
259
260 @keyword timing: A flag which if True will enable timing of individual tests.
261 @type timing: bool
262 """
263
264
265 if (sys.version_info[0] == 3 and sys.version_info[1] == 1) or (sys.version_info[0] == 2 and sys.version_info[1] <= 6):
266 super(GuiTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity)
267 else:
268 super(GuiTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity, failfast=failfast, buffer=buffer, resultclass=resultclass)
269
270
271 self.timing_flag = timing
272
273
275 """Override of the TextTestRunner._makeResult() method."""
276
277 return GuiTestResult(self.stream, self.descriptions, self.verbosity, timing=self.timing_flag)
278