Salome HOME
CppUnit tests updated to the new test procedure based on salome_test_driver.
[modules/kernel.git] / bin / appliskel / salome_tester / salome_test_driver.py
1 # Copyright (C) 2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21 Usage: salome_test_helper.py <timeout_delay> <test_file.py> [test file arguments]
22 """
23
24 import sys
25 import os
26 import subprocess
27 import signal
28
29 # Run test
30 def runTest(command):
31   print "Running:", " ".join(command)
32   p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
33   out, err = p.communicate()
34   res = p.returncode
35
36   return res, out, err
37 #
38
39 # Display output and errors
40 def processResult(res, out, err):
41   if out:
42     print out
43     pass
44   if err:
45     print err
46   print "Status code: ", res
47 #
48
49 # Timeout management
50 class TimeoutException(Exception):
51   """Execption raised when test timeout is reached."""
52 #
53 def timeoutHandler(signum, frame):
54   raise TimeoutException()
55 #
56
57 if __name__ == "__main__":
58   timeout_delay = sys.argv[1]
59   args = sys.argv[2:]
60
61   # Add explicit call to python executable if a Python script is passed as
62   # first argument
63   if not args:
64     print "Invalid arguments for salome_test_helper.py. No command defined."
65     exit(1)
66   _, ext = os.path.splitext(args[0])
67   if ext == ".py":
68     test_and_args = [sys.executable] + args
69   else:
70     test_and_args = args
71
72   # Ensure OMNIORB_USER_PATH is set
73   from salomeContextUtils import setOmniOrbUserPath
74   setOmniOrbUserPath()
75
76   # Set timeout handler
77   print "Test timeout explicitely set to: %s seconds"%timeout_delay
78   signal.alarm(abs(int(timeout_delay)-10))
79   signal.signal(signal.SIGALRM, timeoutHandler)
80
81   # Run test in a new SALOME session
82   from salome_test_session import startSession, terminateSession
83   res = 1
84   try:
85     port = startSession()
86     res, out, err = runTest(test_and_args)
87     processResult(res, out, err)
88   except TimeoutException:
89     print "FAILED : timeout(%s) is reached"%timeout_delay
90   except:
91     import traceback
92     traceback.print_exc()
93     pass
94
95   terminateSession(port)
96   exit(res)
97 #