Package test_suite :: Module relax_test_runner
[hide private]
[frames] | no frames]

Source Code for Module test_suite.relax_test_runner

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2014 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Python module imports. 
 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  # relax module imports. 
 31  from lib.compat import StringIO 
 32  from lib.compat import TextTestResult 
 33  from status import Status; status = Status() 
 34   
 35   
36 -class RelaxTestResult(TextTestResult):
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 # Normal setup. 53 super(RelaxTestResult, self).__init__(stream, descriptions, verbosity) 54 55 # Store the timing flag and category. 56 self.timing_flag = timing 57 self.category = category
58 59
60 - def addError(self, test, err):
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 # Execute the base class method to print the 'E' and handle the error. 73 super(RelaxTestResult, self).addError(test, err) 74 75 # Prepend the STDOUT and STDERR messages to the second element of the tuple. 76 self.errors[-1] = (self.errors[-1][0], self.capt.getvalue() + self.errors[-1][1]) 77 78 # Write out timing info. 79 if self.timing_flag: 80 self.write_time(test.id())
81 82
83 - def addFailure(self, test, err):
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 # Execute the base class method to print the 'F' and handle the failure. 96 super(RelaxTestResult, self).addFailure(test, err) 97 98 # Prepend the STDOUT and STDERR messages to the second element of the tuple. 99 self.failures[-1] = (self.failures[-1][0], self.capt.getvalue() + self.failures[-1][1]) 100 101 # Write out timing info. 102 if self.timing_flag: 103 self.write_time(test.id())
104 105
106 - def addSuccess(self, test):
107 """The method for a successful test. 108 109 @param test: The test object. 110 @type test: TestCase instance 111 """ 112 113 # Execute the base class method to print the '.'. 114 super(RelaxTestResult, self).addSuccess(test) 115 116 # Write out timing info. 117 if self.timing_flag: 118 self.write_time(test.id())
119 120
121 - def startTest(self, test):
122 """Override of the TextTestResult.startTest() method. 123 124 The start of STDOUT and STDERR capture occurs here. 125 """ 126 127 # Store the original STDOUT and STDERR for restoring later on. 128 self.orig_stdout = sys.stdout 129 self.orig_stderr = sys.stderr 130 131 # Catch stdout and stderr. 132 self.capt = StringIO() 133 if not status.debug: 134 sys.stdout = self.capt 135 sys.stderr = self.capt 136 137 # Place the test name in the status object. 138 status.exec_lock.test_name = str(test) 139 140 # Store the starting time. 141 if self.timing_flag: 142 self.time = time() 143 144 # Execute the normal startTest method. 145 super(RelaxTestResult, self).startTest(test)
146 147
148 - def stopTest(self, test):
149 """Override of the TextTestResult.stopTest() method. 150 151 The end of STDOUT and STDERR capture occurs here. 152 """ 153 154 # Restore the IO streams. 155 sys.stdout = self.orig_stdout 156 sys.stderr = self.orig_stderr
157 158
159 - def write_time(self, test_name):
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 # Subtract the end time from the start time. 167 self.time -= time() 168 169 # Change the test name. 170 if self.category != 'unit': 171 test_name = test_name.split('.') 172 test_name = "%s.%s" % (test_name[-2], test_name[-1]) 173 174 # The printout. 175 self.stream.write(' %7.2f s for %s\n' % (-self.time, test_name))
176 177 178
179 -class GuiTestResult(RelaxTestResult):
180 """A replacement for the TextTestResult class for the GUI.""" 181
182 - def stopTest(self, test):
183 """Override of the RelaxTestResult.stopTest() method. 184 185 The end of STDOUT and STDERR capture occurs here. 186 """ 187 188 # Execute the RelaxTestResult.stopTest() method. 189 super(GuiTestResult, self).stopTest(test) 190 191 # Yield to allow the GUI to be updated. 192 wx.GetApp().Yield(True)
193 194 195
196 -class RelaxTestRunner(TextTestRunner):
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 # Variable for specifying the type of test being performed, to change the printout. 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 # Execute the base method (with different Python version compatibility). 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 # Store the flag. 220 self.timing_flag = timing
221 222
223 - def _makeResult(self):
224 """Override of the TextTestRunner._makeResult() method.""" 225 226 return RelaxTestResult(self.stream, self.descriptions, self.verbosity, timing=self.timing_flag, category=self.category)
227 228 229
230 -class GuiTestRunner(TextTestRunner):
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 # Execute the base method. 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 # Store the flag. 251 self.timing_flag = timing
252 253
254 - def _makeResult(self):
255 """Override of the TextTestRunner._makeResult() method.""" 256 257 return GuiTestResult(self.stream, self.descriptions, self.verbosity, timing=self.timing_flag)
258