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  import sys 
 25  from time import time 
 26  from unittest import TextTestRunner 
 27  if dep_check.wx_module: 
 28      import wx 
 29   
 30   
 31  from lib.compat import StringIO 
 32  from lib.compat import TextTestResult 
 33  from status import Status; status = Status() 
 34   
 35   
 37      """A replacement for the TextTestResult class. 
 38   
 39      This class is designed to catch STDOUT and STDERR during the execution of each test and to 
 40      prepend the output to the failure and error reports normally generated by TextTestRunner. 
 41      """ 
 42   
 43 -    def __init__(self, stream, descriptions, verbosity, timing=False, category=None): 
  44          """Initialise the RelaxTestResult object with relax specific variables. 
 45   
 46          @keyword timing:    A flag which if True will enable timing of individual tests. 
 47          @type timing:       bool 
 48          @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. 
 49          @type category:     str 
 50          """ 
 51   
 52           
 53          super(RelaxTestResult, self).__init__(stream, descriptions, verbosity) 
 54   
 55           
 56          self.timing_flag = timing 
 57          self.category = category 
  58   
 59   
 61          """Override of the TestResult.addError() method. 
 62   
 63          The STDOUT and STDERR captured text is prepended to the error text here. 
 64   
 65   
 66          @param test:    The test object. 
 67          @type test:     TestCase instance 
 68          @param err:     A tuple of values as returned by sys.exc_info(). 
 69          @type err:      tuple of values 
 70          """ 
 71   
 72           
 73          super(RelaxTestResult, self).addError(test, err) 
 74   
 75           
 76          self.errors[-1] = (self.errors[-1][0], self.capt.getvalue() + self.errors[-1][1]) 
 77   
 78           
 79          if self.timing_flag: 
 80              self.write_time(test.id()) 
  81   
 82   
 84          """Override of the TestResult.addFailure() method. 
 85   
 86          The STDOUT and STDERR captured text is prepended to the failure text here. 
 87   
 88   
 89          @param test:    The test object. 
 90          @type test:     TestCase instance 
 91          @param err:     A tuple of values as returned by sys.exc_info(). 
 92          @type err:      tuple of values 
 93          """ 
 94   
 95           
 96          super(RelaxTestResult, self).addFailure(test, err) 
 97   
 98           
 99          self.failures[-1] = (self.failures[-1][0], self.capt.getvalue() + self.failures[-1][1]) 
100   
101           
102          if self.timing_flag: 
103              self.write_time(test.id()) 
 104   
105   
107          """The method for a successful test. 
108   
109          @param test:    The test object. 
110          @type test:     TestCase instance 
111          """ 
112   
113           
114          super(RelaxTestResult, self).addSuccess(test) 
115   
116           
117          if self.timing_flag: 
118              self.write_time(test.id()) 
 119   
120   
122          """Override of the TextTestResult.startTest() method. 
123   
124          The start of STDOUT and STDERR capture occurs here. 
125          """ 
126   
127           
128          self.orig_stdout = sys.stdout 
129          self.orig_stderr = sys.stderr 
130   
131           
132          self.capt = StringIO() 
133          if not status.debug: 
134              sys.stdout = self.capt 
135              sys.stderr = self.capt 
136   
137           
138          status.exec_lock.test_name = str(test) 
139   
140           
141          if self.timing_flag: 
142              self.time = time() 
143   
144           
145          super(RelaxTestResult, self).startTest(test) 
 146   
147   
149          """Override of the TextTestResult.stopTest() method. 
150   
151          The end of STDOUT and STDERR capture occurs here. 
152          """ 
153   
154           
155          sys.stdout = self.orig_stdout 
156          sys.stderr = self.orig_stderr 
 157   
158   
160          """Write the timing of the test to the stream. 
161   
162          @param test_name:   The TestCase name. 
163          @type test_name:    str 
164          """ 
165   
166           
167          self.time -= time() 
168   
169           
170          if self.category != 'unit': 
171              test_name = test_name.split('.') 
172              test_name = "%s.%s" % (test_name[-2], test_name[-1]) 
173   
174           
175          self.stream.write('  %7.2f s for %s\n' % (-self.time, test_name)) 
  176   
177   
178   
180      """A replacement for the TextTestResult class for the GUI.""" 
181   
183          """Override of the RelaxTestResult.stopTest() method. 
184   
185          The end of STDOUT and STDERR capture occurs here. 
186          """ 
187   
188           
189          super(GuiTestResult, self).stopTest(test) 
190   
191           
192          wx.GetApp().Yield(True) 
  193   
194   
195   
197      """A replacement unittest runner. 
198   
199      This runner is designed to catch STDOUT during the execution of each test and to prepend the 
200      output to the failure and error reports normally generated by TextTestRunner. 
201      """ 
202   
203       
204      category = None 
205   
206 -    def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, timing=False): 
 207          """Initialise the class, storing the timing flag. 
208   
209          @keyword timing:        A flag which if True will enable timing of individual tests. 
210          @type timing:           bool 
211          """ 
212   
213           
214          if (sys.version_info[0] == 3 and sys.version_info[1] == 1) or (sys.version_info[0] == 2 and sys.version_info[1] <= 6): 
215              super(RelaxTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity) 
216          else: 
217              super(RelaxTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity, failfast=failfast, buffer=buffer, resultclass=resultclass) 
218   
219           
220          self.timing_flag = timing 
 221   
222   
 227   
228   
229   
231      """A replacement unittest runner. 
232   
233      This runner is designed to catch STDOUT during the execution of each test and to prepend the 
234      output to the failure and error reports normally generated by TextTestRunner. 
235      """ 
236   
237 -    def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, timing=False): 
 238          """Initialise the class, storing the timing flag. 
239   
240          @keyword timing:        A flag which if True will enable timing of individual tests. 
241          @type timing:           bool 
242          """ 
243   
244           
245          if (sys.version_info[0] == 3 and sys.version_info[1] == 1) or (sys.version_info[0] == 2 and sys.version_info[1] <= 6): 
246              super(GuiTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity) 
247          else: 
248              super(GuiTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity, failfast=failfast, buffer=buffer, resultclass=resultclass) 
249   
250           
251          self.timing_flag = timing 
 252   
253   
255          """Override of the TextTestRunner._makeResult() method.""" 
256   
257          return GuiTestResult(self.stream, self.descriptions, self.verbosity, timing=self.timing_flag) 
  258