1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.com
22 from salome.kernel import termcolor
23 def printfile(filename):
24 stream = open(filename,'r')
25 lines = stream.readlines()
28 if not termcolor.canDisplayColor(sys.stdout):
29 msg = line.split('\n')[0]
31 msg = termcolor.makeColoredMessage(line.split('\n')[0], termcolor.BLUE)
36 from unittest import TestCase
37 from uiexception import DevelException
41 This class is a simple wrapper of the pyunit framework.
44 self.__listTestSuite=[]
45 self.setLogFilename("testlog.txt")
47 def addTestCase(self,testCaseClass):
48 # We test some attributes to validate that the argument
49 # corresponds to a unittest.TestCase class (WARN: it's not an
50 # instance but a class keyword)
51 if "assertTrue" not in dir(testCaseClass):
52 raise DevelException("Not a pyunit test case")
54 suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass)
55 self.__listTestSuite.append(suite)
57 def setLogFilename(self, filename):
58 self.__logfilename = filename
61 # We first open the output stream
62 if os.path.exists(self.__logfilename):
63 os.remove(self.__logfilename)
64 logfile = open(self.__logfilename,'w')
66 # Then, define and execute the runner to play the test suites
67 runner = unittest.TextTestRunner(stream=logfile,verbosity=2)
68 for suite in self.__listTestSuite:
71 # Finally close the ouput stream and print the report
75 def printreport(self):
76 printfile(self.__logfilename)
78 def execAndConvertExceptionToBoolean(function):
80 This can be used to just execute a test function that is
81 considered as OK simply if no exception is raised (can be test by
93 # Simple helper function for most cases where there is only one
95 def run(testCaseClass):
97 Run the test suite provided by the specified TestCase class.
99 tester = PyUnitTester()
100 tester.addTestCase(testCaseClass)
104 # ==============================================================================
106 # ==============================================================================
108 class MyTestCase(unittest.TestCase):
109 def test_myTestOk_01(self):
111 print "myTestOk_01: should be OK"
114 def test_myTestOk_02(self):
116 print "myTestOk_02: should be OK"
119 def test_myTestNotOk(self):
121 print "myTestNotOk: should be NOT OK"
124 def functionRaisingAnException():
125 raise Exception("An error occurs")
127 def functionReturningFalse():
130 def functionReturningTrue():
133 def functionReturningNothing():
134 print "functionReturningNothing"
136 class MyTestCase2(unittest.TestCase):
137 def test_myTest_01(self):
138 r=execAndConvertExceptionToBoolean(functionRaisingAnException)
139 print "test 01: this test should be NOT OK"
142 def test_myTest_02(self):
143 r=execAndConvertExceptionToBoolean(functionReturningFalse)
144 print "test 02: this test should be NOT OK"
147 def test_myTest_03(self):
148 r=execAndConvertExceptionToBoolean(functionReturningTrue)
149 print "test 03: this test should be OK"
152 def test_myTest_04(self):
153 r=execAndConvertExceptionToBoolean(functionReturningNothing)
154 print "test 04: this test should be OK"
157 def test_myTest_05(self):
159 print "test 05: this test should be OK"
163 def TEST_basic_usecase():
164 tester = PyUnitTester()
165 # Note that the class keywords must be passed
166 tester.addTestCase(MyTestCase)
167 tester.addTestCase(MyTestCase2)
170 if __name__ == "__main__":