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