1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2016 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, or (at your option) any later version.
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
21 ## \defgroup pyunittester pyunittester
23 # \details Simple wrapper of the pyunit framework
27 from salome.kernel import termcolor
28 def printfile(filename):
29 stream = open(filename,'r')
30 lines = stream.readlines()
33 if not termcolor.canDisplayColor(sys.stdout):
34 msg = line.split('\n')[0]
36 msg = termcolor.makeColoredMessage(line.split('\n')[0], termcolor.BLUE)
41 from unittest import TestCase
42 from .uiexception import DevelException
44 ## This class is a simple wrapper of the pyunit framework.
45 # \ingroup pyunittester
48 This class is a simple wrapper of the pyunit framework.
51 self.__listTestSuite=[]
52 self.setLogFilename("testlog.txt")
54 def addTestCase(self,testCaseClass):
55 # We test some attributes to validate that the argument
56 # corresponds to a unittest.TestCase class (WARN: it's not an
57 # instance but a class keyword)
58 if "assertTrue" not in dir(testCaseClass):
59 raise DevelException("Not a pyunit test case")
61 suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass)
62 self.__listTestSuite.append(suite)
64 def setLogFilename(self, filename):
65 self.__logfilename = filename
68 # We first open the output stream
69 if os.path.exists(self.__logfilename):
70 os.remove(self.__logfilename)
71 logfile = open(self.__logfilename,'w')
73 # Then, define and execute the runner to play the test suites
74 runner = unittest.TextTestRunner(stream=logfile,verbosity=2)
75 for suite in self.__listTestSuite:
78 # Finally close the output stream and print the report
82 def printreport(self):
83 printfile(self.__logfilename)
85 ## This can be used to just execute a test function that is
86 # considered as OK simply if no exception is raised (can be test by
88 # \ingroup pyunittester
89 def execAndConvertExceptionToBoolean(function):
91 This can be used to just execute a test function that is
92 considered as OK simply if no exception is raised (can be test by
100 except Exception as e:
104 # Simple helper function for most cases where there is only one
106 ## Run the test suite provided by the specified TestCase class.
107 # \ingroup pyunittester
108 def run(testCaseClass):
110 Run the test suite provided by the specified TestCase class.
112 tester = PyUnitTester()
113 tester.addTestCase(testCaseClass)
117 # ==============================================================================
119 # ==============================================================================
121 class MyTestCase(unittest.TestCase):
122 def test_myTestOk_01(self):
124 print("myTestOk_01: should be OK")
127 def test_myTestOk_02(self):
129 print("myTestOk_02: should be OK")
132 def test_myTestNotOk(self):
134 print("myTestNotOk: should be NOT OK")
137 def functionRaisingAnException():
138 raise Exception("An error occurs")
140 def functionReturningFalse():
143 def functionReturningTrue():
146 def functionReturningNothing():
147 print("functionReturningNothing")
149 class MyTestCase2(unittest.TestCase):
150 def test_myTest_01(self):
151 r=execAndConvertExceptionToBoolean(functionRaisingAnException)
152 print("test 01: this test should be NOT OK")
155 def test_myTest_02(self):
156 r=execAndConvertExceptionToBoolean(functionReturningFalse)
157 print("test 02: this test should be NOT OK")
160 def test_myTest_03(self):
161 r=execAndConvertExceptionToBoolean(functionReturningTrue)
162 print("test 03: this test should be OK")
165 def test_myTest_04(self):
166 r=execAndConvertExceptionToBoolean(functionReturningNothing)
167 print("test 04: this test should be OK")
170 def test_myTest_05(self):
172 print("test 05: this test should be OK")
176 def TEST_basic_usecase():
177 tester = PyUnitTester()
178 # Note that the class keywords must be passed
179 tester.addTestCase(MyTestCase)
180 tester.addTestCase(MyTestCase2)
183 if __name__ == "__main__":