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