Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / src / KERNEL_PY / kernel / pyunittester.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2012  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import sys
22 from salome.kernel import termcolor
23 def printfile(filename):
24     stream = open(filename,'r')
25     lines = stream.readlines()
26     stream.close()
27     for line in lines:
28         if not termcolor.canDisplayColor(sys.stdout):
29             msg = line.split('\n')[0]
30         else:
31             msg = termcolor.makeColoredMessage(line.split('\n')[0], termcolor.BLUE)
32         print msg
33
34 import os
35 import unittest
36 from unittest import TestCase
37 from uiexception import DevelException
38
39 class PyUnitTester():
40     '''
41     This class is a simple wrapper of the pyunit framework.
42     '''
43     def __init__(self):
44         self.__listTestSuite=[]
45         self.setLogFilename("testlog.txt")
46
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")
53
54         suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass)
55         self.__listTestSuite.append(suite)
56
57     def setLogFilename(self, filename):
58         self.__logfilename = filename
59
60     def run(self):
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')
65         
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:
69             runner.run(suite)
70
71         # Finally close the ouput stream and print the report
72         logfile.close()
73         self.printreport()
74
75     def printreport(self):
76         printfile(self.__logfilename)
77
78 def execAndConvertExceptionToBoolean(function):
79     """
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
82     an assertTrue).
83     """
84     try:
85         result = function()
86         if result is None:
87             return True
88         return result
89     except Exception, e:
90         print e
91         return False
92
93 # Simple helper function for most cases where there is only one
94 # TestCase classe.
95 def run(testCaseClass):
96     """
97     Run the test suite provided by the specified TestCase class.
98     """
99     tester = PyUnitTester()
100     tester.addTestCase(testCaseClass)
101     tester.run()
102
103 #
104 # ==============================================================================
105 # Simple use cases
106 # ==============================================================================
107 #
108 class MyTestCase(unittest.TestCase):
109     def test_myTestOk_01(self):
110         r=True
111         print "myTestOk_01: should be OK"
112         self.assertTrue(r)
113
114     def test_myTestOk_02(self):
115         r=True
116         print "myTestOk_02: should be OK"
117         self.assertTrue(r)
118
119     def test_myTestNotOk(self):
120         r=False
121         print "myTestNotOk: should be NOT OK"
122         self.assertTrue(r)
123
124 def functionRaisingAnException():
125     raise Exception("An error occurs")
126
127 def functionReturningFalse():
128     return False
129
130 def functionReturningTrue():
131     return True
132
133 def functionReturningNothing():
134     print "functionReturningNothing"
135
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"
140         self.assertTrue(r)
141
142     def test_myTest_02(self):
143         r=execAndConvertExceptionToBoolean(functionReturningFalse)
144         print "test 02: this test should be NOT OK"
145         self.assertTrue(r)
146
147     def test_myTest_03(self):
148         r=execAndConvertExceptionToBoolean(functionReturningTrue)
149         print "test 03: this test should be OK"
150         self.assertTrue(r)
151
152     def test_myTest_04(self):
153         r=execAndConvertExceptionToBoolean(functionReturningNothing)
154         print "test 04: this test should be OK"
155         self.assertTrue(r)
156
157     def test_myTest_05(self):
158         r=True
159         print "test 05: this test should be OK"
160         self.assertTrue(r)
161
162
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)
168     tester.run()
169     
170 if __name__ == "__main__":
171     TEST_basic_usecase()