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-2011 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Python module imports. 
 24  import dep_check 
 25  try: 
 26      from cStringIO import StringIO 
 27  except ImportError: 
 28      from StringIO import StringIO 
 29  import sys 
 30  try: 
 31      from unittest import TextTestResult    # Python 2.7 and above. 
 32  except ImportError: 
 33      from unittest import _TextTestResult as TextTestResult    # Python 2.6 and below. 
 34  from unittest import TextTestRunner 
 35  if dep_check.wx_module: 
 36      import wx 
 37   
 38  # relax module imports. 
 39  from status import Status; status = Status() 
 40   
 41   
42 -class RelaxTestResult(TextTestResult):
43 """A replacement for the TextTestResult class. 44 45 This class is designed to catch STDOUT and STDERR during the execution of each test and to 46 prepend the output to the failure and error reports normally generated by TextTestRunner. 47 """ 48
49 - def startTest(self, test):
50 """Override of the TextTestResult.startTest() method. 51 52 The start of STDOUT and STDERR capture occurs here. 53 """ 54 55 # Store the original STDOUT and STDERR for restoring later on. 56 self.orig_stdout = sys.stdout 57 self.orig_stderr = sys.stderr 58 59 # Catch stdout and stderr. 60 self.capt = StringIO() 61 if not status.debug: 62 sys.stdout = self.capt 63 sys.stderr = self.capt 64 65 # Place the test name in the status object. 66 status.exec_lock.test_name = str(test) 67 68 # Execute the normal startTest method. 69 TextTestResult.startTest(self, test)
70 71
72 - def stopTest(self, test):
73 """Override of the TextTestResult.stopTest() method. 74 75 The end of STDOUT and STDERR capture occurs here. 76 """ 77 78 # Restore the IO streams. 79 sys.stdout = self.orig_stdout 80 sys.stderr = self.orig_stderr
81 82
83 - def addError(self, test, err):
84 """Override of the TestResult.addError() method. 85 86 The STDOUT and STDERR captured text is prepended to the error text here. 87 """ 88 89 # Execute the normal addError method. 90 TextTestResult.addError(self, test, err) 91 92 # Prepend the STDOUT and STDERR messages to the second element of the tuple. 93 self.errors[-1] = (self.errors[-1][0], self.capt.getvalue() + self.errors[-1][1])
94 95
96 - def addFailure(self, test, err):
97 """Override of the TestResult.addFailure() method. 98 99 The STDOUT and STDERR captured text is prepended to the failure text here. 100 """ 101 102 # Execute the normal addFailure method. 103 TextTestResult.addFailure(self, test, err) 104 105 # Prepend the STDOUT and STDERR messages to the second element of the tuple. 106 self.failures[-1] = (self.failures[-1][0], self.capt.getvalue() + self.failures[-1][1])
107 108 109
110 -class GuiTestResult(RelaxTestResult):
111 """A replacement for the TextTestResult class for the GUI.""" 112
113 - def stopTest(self, test):
114 """Override of the RelaxTestResult.stopTest() method. 115 116 The end of STDOUT and STDERR capture occurs here. 117 """ 118 119 # Execute the RelaxTestResult.stopTest() method. 120 super(GuiTestResult, self).stopTest(test) 121 122 # Yield to allow the GUI to be updated. 123 wx.GetApp().Yield(True)
124 125 126
127 -class RelaxTestRunner(TextTestRunner):
128 """A replacement unittest runner. 129 130 This runner is designed to catch STDOUT during the execution of each test and to prepend the 131 output to the failure and error reports normally generated by TextTestRunner. 132 """ 133
134 - def _makeResult(self):
135 """Override of the TextTestRunner._makeResult() method.""" 136 137 return RelaxTestResult(self.stream, self.descriptions, self.verbosity)
138 139 140
141 -class GuiTestRunner(TextTestRunner):
142 """A replacement unittest runner. 143 144 This runner is designed to catch STDOUT during the execution of each test and to prepend the 145 output to the failure and error reports normally generated by TextTestRunner. 146 """ 147
148 - def _makeResult(self):
149 """Override of the TextTestRunner._makeResult() method.""" 150 151 return GuiTestResult(self.stream, self.descriptions, self.verbosity)
152