Back to index

salome-kernel  6.5.0
pyunittester.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2011-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 import sys
00022 from salome.kernel import termcolor
00023 def printfile(filename):
00024     stream = open(filename,'r')
00025     lines = stream.readlines()
00026     stream.close()
00027     for line in lines:
00028        if not termcolor.canDisplayColor(sys.stdout):
00029            msg = line.split('\n')[0]
00030        else:
00031             msg = termcolor.makeColoredMessage(line.split('\n')[0], termcolor.BLUE)
00032         print msg
00033 
00034 import os
00035 import unittest
00036 from unittest import TestCase
00037 from uiexception import DevelException
00038 
00039 class PyUnitTester():
00040     '''
00041     This class is a simple wrapper of the pyunit framework.
00042     '''
00043     def __init__(self):
00044         self.__listTestSuite=[]
00045         self.setLogFilename("testlog.txt")
00046 
00047     def addTestCase(self,testCaseClass):
00048         # We test some attributes to validate that the argument
00049         # corresponds to a unittest.TestCase class (WARN: it's not an
00050         # instance but a class keyword)
00051         if "assertTrue" not in dir(testCaseClass):
00052             raise DevelException("Not a pyunit test case")
00053 
00054         suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass)
00055         self.__listTestSuite.append(suite)
00056 
00057     def setLogFilename(self, filename):
00058         self.__logfilename = filename
00059 
00060     def run(self):
00061         # We first open the output stream
00062         if os.path.exists(self.__logfilename):
00063             os.remove(self.__logfilename)
00064         logfile = open(self.__logfilename,'w')
00065         
00066         # Then, define and execute the runner to play the test suites
00067         runner = unittest.TextTestRunner(stream=logfile,verbosity=2)
00068         for suite in self.__listTestSuite:
00069             runner.run(suite)
00070 
00071         # Finally close the ouput stream and print the report
00072         logfile.close()
00073         self.printreport()
00074 
00075     def printreport(self):
00076         printfile(self.__logfilename)
00077 
00078 def execAndConvertExceptionToBoolean(function):
00079     """
00080     This can be used to just execute a test function that is
00081     considered as OK simply if no exception is raised (can be test by
00082     an assertTrue).
00083     """
00084     try:
00085         result = function()
00086         if result is None:
00087             return True
00088         return result
00089     except Exception, e:
00090         print e
00091         return False
00092 
00093 # Simple helper function for most cases where there is only one
00094 # TestCase classe.
00095 def run(testCaseClass):
00096     """
00097     Run the test suite provided by the specified TestCase class.
00098     """
00099     tester = PyUnitTester()
00100     tester.addTestCase(testCaseClass)
00101     tester.run()
00102 
00103 #
00104 # ==============================================================================
00105 # Simple use cases
00106 # ==============================================================================
00107 #
00108 class MyTestCase(unittest.TestCase):
00109     def test_myTestOk_01(self):
00110         r=True
00111         print "myTestOk_01: should be OK"
00112         self.assertTrue(r)
00113 
00114     def test_myTestOk_02(self):
00115         r=True
00116         print "myTestOk_02: should be OK"
00117         self.assertTrue(r)
00118 
00119     def test_myTestNotOk(self):
00120         r=False
00121         print "myTestNotOk: should be NOT OK"
00122         self.assertTrue(r)
00123 
00124 def functionRaisingAnException():
00125     raise Exception("An error occurs")
00126 
00127 def functionReturningFalse():
00128     return False
00129 
00130 def functionReturningTrue():
00131     return True
00132 
00133 def functionReturningNothing():
00134     print "functionReturningNothing"
00135 
00136 class MyTestCase2(unittest.TestCase):
00137     def test_myTest_01(self):
00138         r=execAndConvertExceptionToBoolean(functionRaisingAnException)
00139         print "test 01: this test should be NOT OK"
00140         self.assertTrue(r)
00141 
00142     def test_myTest_02(self):
00143         r=execAndConvertExceptionToBoolean(functionReturningFalse)
00144         print "test 02: this test should be NOT OK"
00145         self.assertTrue(r)
00146 
00147     def test_myTest_03(self):
00148         r=execAndConvertExceptionToBoolean(functionReturningTrue)
00149         print "test 03: this test should be OK"
00150         self.assertTrue(r)
00151 
00152     def test_myTest_04(self):
00153         r=execAndConvertExceptionToBoolean(functionReturningNothing)
00154         print "test 04: this test should be OK"
00155         self.assertTrue(r)
00156 
00157     def test_myTest_05(self):
00158         r=True
00159         print "test 05: this test should be OK"
00160         self.assertTrue(r)
00161 
00162 
00163 def TEST_basic_usecase():
00164     tester = PyUnitTester()
00165     # Note that the class keywords must be passed
00166     tester.addTestCase(MyTestCase)
00167     tester.addTestCase(MyTestCase2)
00168     tester.run()
00169     
00170 if __name__ == "__main__":
00171     TEST_basic_usecase()