Module EDTestSuite
[hide private]
[frames] | no frames]

Source Code for Module EDTestSuite

  1  # coding: utf8 
  2  # 
  3  #    Project: The EDNA Kernel 
  4  #             http://www.edna-site.org 
  5  # 
  6  #    Copyright (C) 2008-2010 European Synchrotron Radiation Facility 
  7  #                            Grenoble, France 
  8  # 
  9  #    Principal authors: Marie-Francoise Incardona (incardon@esrf.fr) 
 10  #                       Olof Svensson (svensson@esrf.fr)  
 11  #                       Jérôme Kieffer (Jerome.kieffer@esrf.fr) 
 12  # 
 13  #    This program is free software: you can redistribute it and/or modify 
 14  #    it under the terms of the GNU Lesser General Public License as published 
 15  #    by the Free Software Foundation, either version 3 of the License, or 
 16  #    (at your option) any later version. 
 17  # 
 18  #    This program is distributed in the hope that it will be useful, 
 19  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21  #    GNU Lesser General Public License for more details. 
 22  # 
 23  #    You should have received a copy of the GNU General Public License 
 24  #    and the GNU Lesser General Public License  along with this program.   
 25  #    If not, see <http://www.gnu.org/licenses/>. 
 26  # 
 27   
 28  """ 
 29  EDNA test suite module ... 
 30   
 31  a TestSuite is a sequence of EDTestCase and/or EDTestSuite 
 32   
 33  """ 
 34   
 35  __authors__ = "Marie-Francoise Incardona, Olof Svensson, Jerome Kieffer" 
 36  __contact__ = "svensson@esrf.eu" 
 37  __license__ = "LGPLv3+" 
 38  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
 39  __date__ = "20120216" 
 40   
 41  from EDVerbose          import EDVerbose 
 42  from EDTest             import EDTest 
 43  from EDUtilsTest        import EDUtilsTest 
 44   
 45   
46 -class EDTestSuite(EDTest):
47 """ 48 A test suite can contain a list of test cases and/or other test suites. 49 At the end of the execution of a test suite a summary is written containing: 50 - The number of test suites in the test suite(if any) 51 - If there are test suites not executed a list of these 52 - The total number of test cases not executed due to e.g. missing configuration file and a list of these 53 - The total number of test cases ending with success 54 - The total number of test cases ending with failure 55 - If there are test methods ending with failure a list of the test cases, test methods and the failures. 56 - The total number of test methods ending with success 57 - The total number of test methods ending with failure 58 """ 59 60
61 - def __init__(self, _strTestSuiteName=None):
62 EDTest.__init__(self, _strTestSuiteName) 63 self.__listTestCase = [] 64 self.__listTestSuite = [] 65 self.__bTopLevel = True 66 self.__iNumberTestSuite = 0 67 self.__iNumberTestMethodSuccess = 0 68 self.__iNumberTestMethodFailure = 0 69 self.__iNumberTestCaseSuccess = 0 70 self.__iNumberTestCaseFailure = 0 71 self.__dictTestCaseFailureMessages = {} 72 self.__dictTestCaseNotExecuted = {} 73 self.__dictTestSuiteNotExecuted = {}
74 75
76 - def addTestCaseFromName(self, _strTestCaseName):
77 """ 78 This method adds a test case give it's name to the test suite. If the test case 79 cannot be loaded an error message is issued and it's name is added to the list of not 80 executed test cases. 81 @return: NumberTestCaseFailure 82 @rtype: integer 83 """ 84 edTestCase = None 85 exceptionObject = None 86 try: 87 edTestCase = EDUtilsTest.getFactoryPluginTest().loadPlugin(_strTestCaseName) 88 except ImportError, exceptionObject: 89 strWarningMessage = "Could not create the test case: %s, reason: %s" % (_strTestCaseName, exceptionObject) 90 EDVerbose.WARNING(strWarningMessage) 91 self.__dictTestCaseNotExecuted[_strTestCaseName] = "%s : %s" % (self.getClassName(), strWarningMessage) 92 if edTestCase is None: 93 if exceptionObject is None: 94 EDVerbose.error("EDTestSuite.addTestCaseFromName: Could not create the test case: " + _strTestCaseName) 95 self.__dictTestCaseNotExecuted[_strTestCaseName] = "%s : Could not create the test case" % self.getClassName() 96 else: 97 edTestCase.setTestSuiteName(self.getClassName()) 98 self.__listTestCase.append(edTestCase)
99 100
101 - def addTestSuiteFromName(self, _strTestSuiteName):
102 """ 103 This method adds a test suite give it's name to the test suite. If the test case 104 cannot be loaded an error message is issued and it's name is added to the list of not 105 executed test suites. 106 @return: NumberTestCaseFailure 107 @rtype: integer 108 """ 109 edTestSuite = None 110 exceptionObject = None 111 try: 112 edTestSuite = EDUtilsTest.getFactoryPluginTest().loadPlugin(_strTestSuiteName) 113 except ImportError, exceptionObject: 114 strWarningMessage = "Could not create the test suite: %s, reason: %s" % (_strTestSuiteName, exceptionObject) 115 EDVerbose.WARNING(strWarningMessage) 116 self.__dictTestCaseNotExecuted[_strTestSuiteName] = "%s : %s" % (self.getClassName(), strWarningMessage) 117 if edTestSuite is None: 118 if exceptionObject is None: 119 EDVerbose.error("EDTestSuitePluginUnit.addTestSuiteFromName: Could not create test suite: " + _strTestSuiteName) 120 self.__dictTestSuiteNotExecuted[_strTestSuiteName] = "%s : Could not create the test suite" % self.getClassName() 121 else: 122 self.__listTestSuite.append(edTestSuite)
123 124 125
126 - def processKernel(self):
127 """ 128 This method executes the test suite. 129 """ 130 EDVerbose.DEBUG("Execution: EDTestSuite.processKernel()") 131 EDVerbose.screen("") 132 EDVerbose.screen("") 133 EDVerbose.unitTest("###################################################################") 134 EDVerbose.unitTest("STARTING::" + self.getTestName()) 135 EDVerbose.unitTest("###################################################################") 136 self.setTimeInit() 137 for edTestCase in self.__listTestCase: 138 edTestCase.execute() 139 if edTestCase.isExecuted(): 140 self.__iNumberTestMethodSuccess += edTestCase.getNumberTestMethodSuccess() 141 if edTestCase.getNumberTestMethodFailure() == 0: 142 self.__iNumberTestCaseSuccess += 1 143 else: 144 self.__iNumberTestCaseFailure += 1 145 self.__iNumberTestMethodFailure += edTestCase.getNumberTestMethodFailure() 146 self.__dictTestCaseFailureMessages[edTestCase.getClassName()] = edTestCase.getMethodFailureMessages() 147 else: 148 self.__dictTestCaseNotExecuted[edTestCase.getClassName()] = edTestCase.getReasonForNotBeingExectuted() 149 EDVerbose.screen() 150 for edTestSuite in self.__listTestSuite: 151 edTestSuite.execute() 152 self.__iNumberTestSuite += 1 153 self.__iNumberTestMethodSuccess += edTestSuite.getNumberTestMethodSuccess() 154 self.__iNumberTestMethodFailure += edTestSuite.getNumberTestMethodFailure() 155 self.__iNumberTestCaseSuccess += edTestSuite.getNumberTestCaseSuccess() 156 self.__iNumberTestCaseFailure += edTestSuite.getNumberTestCaseFailure() 157 self.extendDictionary(self.__dictTestCaseNotExecuted, edTestSuite.getDictTestCaseNotExecuted()) 158 self.extendDictionary(self.__dictTestSuiteNotExecuted, edTestSuite.getDictTestSuiteNotExecuted()) 159 self.extendDictionary(self.__dictTestCaseFailureMessages, edTestSuite.getDictTestCaseFailureMessages()) 160 self.setTimeEnd()
161 162 163
164 - def postProcess(self):
165 """ 166 This method writes out the results of the test suite. 167 """ 168 EDVerbose.screen() 169 EDVerbose.screen() 170 EDVerbose.unitTest("###################################################################") 171 strResult = None 172 if self.__iNumberTestCaseFailure == 0 and self.__iNumberTestMethodFailure == 0: 173 strResult = "SUCCESS" 174 else: 175 strResult = "FAILURE" 176 EDVerbose.unitTest("Result for %s : %s" % (self.getTestName(), strResult)) 177 EDVerbose.unitTest() 178 if self.__iNumberTestSuite > 0: 179 EDVerbose.unitTest(" Number of executed test suites in this test suite : %d" % self.__iNumberTestSuite) 180 EDVerbose.unitTest() 181 if self.__dictTestSuiteNotExecuted != {}: 182 EDVerbose.unitTest() 183 EDVerbose.unitTest("OBS! The following test suites were not executed due to errors:") 184 EDVerbose.unitTest() 185 for strTestSuiteName in self.__dictTestSuiteNotExecuted.keys(): 186 EDVerbose.unitTest(" %s : %s" % (self.__dictTestSuiteNotExecuted[strTestSuiteName], strTestSuiteName)) 187 EDVerbose.unitTest() 188 if self.__dictTestCaseNotExecuted != {}: 189 EDVerbose.unitTest() 190 EDVerbose.unitTest("OBS! The following test cases not executed due to errors:") 191 EDVerbose.unitTest() 192 for strTestCaseName in self.__dictTestCaseNotExecuted.keys(): 193 EDVerbose.unitTest(" %s :" % strTestCaseName) 194 EDVerbose.unitTest(" %s" % self.__dictTestCaseNotExecuted[strTestCaseName]) 195 EDVerbose.unitTest() 196 EDVerbose.unitTest(" Total number of test cases NOT EXECUTED : %d" % len(self.__dictTestCaseNotExecuted)) 197 EDVerbose.unitTest() 198 if self.__iNumberTestCaseSuccess != 0 or self.__iNumberTestCaseFailure != 0: 199 EDVerbose.unitTest(" Total number of test cases executed with SUCCESS : %d" % self.__iNumberTestCaseSuccess) 200 EDVerbose.unitTest(" Total number of test cases executed with FAILURE : %d" % self.__iNumberTestCaseFailure) 201 EDVerbose.unitTest() 202 if self.__dictTestCaseFailureMessages != {}: 203 EDVerbose.unitTest("") 204 EDVerbose.unitTest("OBS! The following test methods ended with failure:") 205 EDVerbose.unitTest("") 206 for strTestCaseName in self.__dictTestCaseFailureMessages.keys(): 207 EDVerbose.unitTest(" %s :" % strTestCaseName) 208 for strMethodName in self.__dictTestCaseFailureMessages[strTestCaseName].keys(): 209 EDVerbose.unitTest(" %s :" % strMethodName.split(".")[1]) 210 EDVerbose.unitTest(" %s" % self.__dictTestCaseFailureMessages[strTestCaseName][strMethodName]) 211 EDVerbose.unitTest("") 212 EDVerbose.unitTest("") 213 if self.__iNumberTestMethodSuccess != 0 or self.__iNumberTestMethodFailure != 0: 214 EDVerbose.unitTest("Total number of test methods executed with SUCCESS : %d" % self.__iNumberTestMethodSuccess) 215 EDVerbose.unitTest("Total number of test methods executed with FAILURE : %d" % self.__iNumberTestMethodFailure) 216 EDVerbose.unitTest() 217 EDVerbose.unitTest(" Runtime : %.3f [s]" % self.getRunTime()) 218 EDVerbose.unitTest("###################################################################")
219 220
221 - def extendDictionary(self, _dictBase, _dictExtend):
222 """ 223 Helper function for "extending" a dictionary. Example: 224 >>> a={"a":1, "b": 2} 225 >>> b={"c":3, "d": 4} 226 >>> extendDictionary(a,b) 227 >>>a 228 {"a":1, "b": 2, "c":3, "d": 4} 229 230 @param _dictBase: The dictionary to be extended 231 @type _dictBase: dict 232 233 @param _dictExtend: The dictionary that will extend _dictBase 234 @type _dictExtend: dict 235 """ 236 for key in _dictExtend.keys(): 237 _dictBase[key] = _dictExtend[key]
238 239
240 - def getNumberTestCaseFailure(self):
241 """ 242 Total number of test cases ended with failure. 243 @return: NumberTestCaseFailure 244 @rtype: integer 245 """ 246 return self.__iNumberTestCaseFailure
247 248
249 - def getNumberTestCaseSuccess(self):
250 """ 251 Total number of test cases ended with success. 252 @return: NumberTestCaseSuccess 253 @rtype: integer 254 """ 255 return self.__iNumberTestCaseSuccess
256 257
259 """ 260 Total number of test methods ending with failure. 261 @return: NumberTestMethodFailure 262 @rtype: integer 263 """ 264 return self.__iNumberTestMethodFailure
265 266
268 """ 269 Total number of test methods ending with success. 270 @return: NumberTestMethodSuccess 271 @rtype: integer 272 """ 273 return self.__iNumberTestMethodSuccess
274 275 276
278 """ 279 Returns a dictionary containing: 280 Key : Test case name with failed test method(s) 281 Value : Dictionary with method names (keys) and error messages (values). 282 @return: TestCaseFailureMessages 283 @rtype: dict 284 """ 285 return self.__dictTestCaseFailureMessages
286 287
289 """ 290 Returns a dictionary containing: 291 Key : Test case name 292 Value : Description of why the test case was not executed 293 @return: TestCaseNotExecuted 294 @rtype: dict 295 """ 296 return self.__dictTestCaseNotExecuted
297 298
300 """ 301 Returns a dictionary containing: 302 Key : Test suite name 303 Value : Description of why the test suite was not executed 304 @return: TestCaseNotExecuted 305 @rtype: list 306 """ 307 return self.__dictTestSuiteNotExecuted
308