1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011 EDF R&D, CEA
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.co
21 from salome.kernel import termcolor
22 def printfile(filename):
23 stream = open(filename,'r')
24 lines = stream.readlines()
27 if not termcolor.canDisplayColor(sys.stdout):
28 msg = line.split('\n')[0]
30 msg = termcolor.makeColoredMessage(line.split('\n')[0], termcolor.BLUE)
35 from unittest import TestCase
36 from uiexception import DevelException
40 This class is a simple wrapper of the pyunit framework.
43 self.__listTestSuite=[]
44 self.setLogFilename("testlog.txt")
46 def addTestCase(self,testCaseClass):
47 # We test some attributes to validate that the argument
48 # corresponds to a unittest.TestCase class (WARN: it's not an
49 # instance but a class keyword)
50 if "assertTrue" not in dir(testCaseClass):
51 raise DevelException("Not a pyunit test case")
53 suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass)
54 self.__listTestSuite.append(suite)
56 def setLogFilename(self, filename):
57 self.__logfilename = filename
60 # We first open the output stream
61 if os.path.exists(self.__logfilename):
62 os.remove(self.__logfilename)
63 logfile = open(self.__logfilename,'w')
65 # Then, define and execute the runner to play the test suites
66 runner = unittest.TextTestRunner(stream=logfile,verbosity=2)
67 for suite in self.__listTestSuite:
70 # Finally close the ouput stream and print the report
74 def printreport(self):
75 printfile(self.__logfilename)
77 def execAndConvertExceptionToBoolean(function):
79 This can be used to just execute a test function that is
80 considered as OK simply if no exception is raised (can be test by
92 # Simple helper function for most cases where there is only one
94 def run(testCaseClass):
96 Run the test suite provided by the specified TestCase class.
98 tester = PyUnitTester()
99 tester.addTestCase(testCaseClass)
103 # ==============================================================================
105 # ==============================================================================
107 class MyTestCase(unittest.TestCase):
108 def test_myTestOk_01(self):
110 print "myTestOk_01: should be OK"
113 def test_myTestOk_02(self):
115 print "myTestOk_02: should be OK"
118 def test_myTestNotOk(self):
120 print "myTestNotOk: should be NOT OK"
123 def functionRaisingAnException():
124 raise Exception("An error occurs")
126 def functionReturningFalse():
129 def functionReturningTrue():
132 def functionReturningNothing():
133 print "functionReturningNothing"
135 class MyTestCase2(unittest.TestCase):
136 def test_myTest_01(self):
137 r=execAndConvertExceptionToBoolean(functionRaisingAnException)
138 print "test 01: this test should be NOT OK"
141 def test_myTest_02(self):
142 r=execAndConvertExceptionToBoolean(functionReturningFalse)
143 print "test 02: this test should be NOT OK"
146 def test_myTest_03(self):
147 r=execAndConvertExceptionToBoolean(functionReturningTrue)
148 print "test 03: this test should be OK"
151 def test_myTest_04(self):
152 r=execAndConvertExceptionToBoolean(functionReturningNothing)
153 print "test 04: this test should be OK"
156 def test_myTest_05(self):
158 print "test 05: this test should be OK"
162 def TEST_basic_usecase():
163 tester = PyUnitTester()
164 # Note that the class keywords must be passed
165 tester.addTestCase(MyTestCase)
166 tester.addTestCase(MyTestCase2)
169 if __name__ == "__main__":