Salome HOME
f8152534c4a761e2eb7c6dae5df2a45fe6cd69cd
[modules/kernel.git] / src / KERNEL_PY / kernel / unittester.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2014  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, or (at your option) any later version.
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 ## \defgroup unittester unittester
22 #  \{ 
23 #  \details Run very basic unit tests
24 #  \}
25
26 __author__="gboulant"
27 __date__ ="$1 avr. 2010 09:45:21$"
28
29 import sys
30
31 ## This function should be used for very basic unit tests and only for a
32 #  rapid development. %A better way should be the pyunit framework.
33 #  The function functionName is supposed here to return a boolean value
34 #  indicating if the test is OK (True) or NOT OK (False)
35 #  \ingroup unittester
36 def run(modulePath, functionName):
37     """
38     This function should be used for very basic unit tests and only for a
39     rapid development. A better way should be the pyunit framework.
40     The function functionName is supposed here to return a boolean value
41     indicating if the test is OK (True) or NOT OK (False)
42     """
43     moduleName = modulePath.replace('/','.') 
44     __import__ (moduleName)
45     module=sys.modules[moduleName]
46     func = getattr(module,functionName)
47     tabsize = 70-len(moduleName)-len(functionName)
48     print "[TEST] %s.%s %s test in progress" % (moduleName, functionName,"."*tabsize) 
49     ok = func()
50     if ( ok ):
51         print "[TEST] %s.%s %s OK" % (moduleName, functionName,"."*tabsize)
52     else:
53         print "[TEST] %s.%s %s NOT OK" % (moduleName, functionName,"."*tabsize)
54
55 ## This function is for debug only. It executes the specified function with the
56 #  specified arguments in a try/except bloc so that to display the exception in
57 #  the case where an exception is raised (usefull to debug server side of a CORBA
58 #  process).
59 #  \ingroup unittester
60 def tryfunction(function,*argv):
61     """
62     This function is for debug only. It executes the specified function with the
63     specified arguments in a try/except bloc so that to display the exception in
64     the case where an exception is raised (usefull to debug server side of a CORBA
65     process).
66     """
67     print "[TEST] trying the function %s" % function
68     try:
69         return function(*argv)
70     except Exception, e:
71         print e
72         raise e
73
74
75 #
76 # ==============================================================================
77 # Simple use cases
78 # ==============================================================================
79 #
80 def TEST_myTestIsOk():
81     return True
82
83 def TEST_myTestIsNotOk():
84     return False
85
86 if __name__ == "__main__":
87     run("salome/kernel/unittester","TEST_myTestIsOk")
88     run("salome/kernel/unittester","TEST_myTestIsNotOk")