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-2013 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 compat import StringIO 
 32  from 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):
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 """ 49 50 # Normal setup. 51 super(RelaxTestResult, self).__init__(stream, descriptions, verbosity) 52 53 # Store the timing flag. 54 self.timing_flag = timing
55 56
57 - def addError(self, test, err):
58 """Override of the TestResult.addError() method. 59 60 The STDOUT and STDERR captured text is prepended to the error text here. 61 62 63 @param test: The test object. 64 @type test: TestCase instance 65 @param err: A tuple of values as returned by sys.exc_info(). 66 @type err: tuple of values 67 """ 68 69 # Execute the base class method to print the 'E' and handle the error. 70 super(RelaxTestResult, self).addError(test, err) 71 72 # Prepend the STDOUT and STDERR messages to the second element of the tuple. 73 self.errors[-1] = (self.errors[-1][0], self.capt.getvalue() + self.errors[-1][1]) 74 75 # Write out timing info. 76 if self.timing_flag: 77 self.write_time(test.id())
78 79
80 - def addFailure(self, test, err):
81 """Override of the TestResult.addFailure() method. 82 83 The STDOUT and STDERR captured text is prepended to the failure text here. 84 85 86 @param test: The test object. 87 @type test: TestCase instance 88 @param err: A tuple of values as returned by sys.exc_info(). 89 @type err: tuple of values 90 """ 91 92 # Execute the base class method to print the 'F' and handle the failure. 93 super(RelaxTestResult, self).addFailure(test, err) 94 95 # Prepend the STDOUT and STDERR messages to the second element of the tuple. 96 self.failures[-1] = (self.failures[-1][0], self.capt.getvalue() + self.failures[-1][1]) 97 98 # Write out timing info. 99 if self.timing_flag: 100 self.write_time(test.id())
101 102
103 - def addSuccess(self, test):
104 """The method for a successful test. 105 106 @param test: The test object. 107 @type test: TestCase instance 108 """ 109 110 # Execute the base class method to print the '.'. 111 super(RelaxTestResult, self).addSuccess(test) 112 113 # Write out timing info. 114 if self.timing_flag: 115 self.write_time(test.id())
116 117
118 - def startTest(self, test):
119 """Override of the TextTestResult.startTest() method. 120 121 The start of STDOUT and STDERR capture occurs here. 122 """ 123 124 # Store the original STDOUT and STDERR for restoring later on. 125 self.orig_stdout = sys.stdout 126 self.orig_stderr = sys.stderr 127 128 # Catch stdout and stderr. 129 self.capt = StringIO() 130 if not status.debug: 131 sys.stdout = self.capt 132 sys.stderr = self.capt 133 134 # Place the test name in the status object. 135 status.exec_lock.test_name = str(test) 136 137 # Store the starting time. 138 if self.timing_flag: 139 self.time = time() 140 141 # Execute the normal startTest method. 142 super(RelaxTestResult, self).startTest(test)
143 144
145 - def stopTest(self, test):
146 """Override of the TextTestResult.stopTest() method. 147 148 The end of STDOUT and STDERR capture occurs here. 149 """ 150 151 # Restore the IO streams. 152 sys.stdout = self.orig_stdout 153 sys.stderr = self.orig_stderr
154 155
156 - def write_time(self, test_name):
157 """Write the timing of the test to the stream. 158 159 @param test_name: The TestCase name. 160 @type test_name: str 161 """ 162 163 # Subtract the end time from the start time. 164 self.time -= time() 165 166 # Change the test name. 167 test_name = test_name.split('.') 168 test_name = "%s.%s" % (test_name[-2], test_name[-1]) 169 170 # The printout. 171 self.stream.write(' %7.2f s for %s.\n' % (-self.time, test_name))
172 173 174
175 -class GuiTestResult(RelaxTestResult):
176 """A replacement for the TextTestResult class for the GUI.""" 177
178 - def stopTest(self, test):
179 """Override of the RelaxTestResult.stopTest() method. 180 181 The end of STDOUT and STDERR capture occurs here. 182 """ 183 184 # Execute the RelaxTestResult.stopTest() method. 185 super(GuiTestResult, self).stopTest(test) 186 187 # Yield to allow the GUI to be updated. 188 wx.GetApp().Yield(True)
189 190 191
192 -class RelaxTestRunner(TextTestRunner):
193 """A replacement unittest runner. 194 195 This runner is designed to catch STDOUT during the execution of each test and to prepend the 196 output to the failure and error reports normally generated by TextTestRunner. 197 """ 198
199 - def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, timing=False):
200 """Initialise the class, storing the timing flag. 201 202 @keyword timing: A flag which if True will enable timing of individual tests. 203 @type timing: bool 204 """ 205 206 # Execute the base method (with different Python version compatibility). 207 if (sys.version_info[0] == 3 and sys.version_info[1] == 1) or (sys.version_info[0] == 2 and sys.version_info[1] <= 6): 208 super(RelaxTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity) 209 else: 210 super(RelaxTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity, failfast=failfast, buffer=buffer, resultclass=resultclass) 211 212 # Store the flag. 213 self.timing_flag = timing
214 215
216 - def _makeResult(self):
217 """Override of the TextTestRunner._makeResult() method.""" 218 219 return RelaxTestResult(self.stream, self.descriptions, self.verbosity, timing=self.timing_flag)
220 221 222
223 -class GuiTestRunner(TextTestRunner):
224 """A replacement unittest runner. 225 226 This runner is designed to catch STDOUT during the execution of each test and to prepend the 227 output to the failure and error reports normally generated by TextTestRunner. 228 """ 229
230 - def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, timing=False):
231 """Initialise the class, storing the timing flag. 232 233 @keyword timing: A flag which if True will enable timing of individual tests. 234 @type timing: bool 235 """ 236 237 # Execute the base method. 238 if (sys.version_info[0] == 3 and sys.version_info[1] == 1) or (sys.version_info[0] == 2 and sys.version_info[1] <= 6): 239 super(GuiTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity) 240 else: 241 super(GuiTestRunner, self).__init__(stream=stream, descriptions=descriptions, verbosity=verbosity, failfast=failfast, buffer=buffer, resultclass=resultclass) 242 243 # Store the flag. 244 self.timing_flag = timing
245 246
247 - def _makeResult(self):
248 """Override of the TextTestRunner._makeResult() method.""" 249 250 return GuiTestResult(self.stream, self.descriptions, self.verbosity, timing=self.timing_flag)
251