Salome HOME
82e79599f70fb147ff2d79ca5e7c6c98e341e3cf
[tools/sat.git] / src / test / scriptTemplate.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 """
5 This script is automatically generated by 'command sat test etc...'
6 from ...salomeTools/src/test/scriptTemplate.py
7 """
8
9 import os
10 import sys
11 import traceback
12 import os.path
13 import time as THEBIGTIME
14
15
16 # set path
17 toolsWay = r'${toolsWay}'
18 resourcesWay = r'${resourcesWay}'
19 outWay = r'${sessionDir}'
20 tmpDir = r'${tmpDir}'
21
22 listTest = ${listTest}
23 ignore = ${ignore}
24
25 sys.path.append(toolsWay)
26 from TOOLS import TOOLS_class
27 my_tools = TOOLS_class(resourcesWay, tmpDir, toolsWay)
28
29 from TOOLS import SatNotApplicableError
30
31 # set environement variables
32 os.environ['TT_BASE_RESSOURCES'] = resourcesWay
33 sys.path.append(resourcesWay)
34
35 __stdout__ = sys.stdout
36 __stderr__ = sys.stderr
37
38 with open(r'${resultFile}', 'w') as exec_result:
39   exec_result.write('Open\n')
40
41   for test in listTest:
42     with open(os.path.join(outWay, test[:-3] + ".result.py"), "w") as pylog:
43       with open(os.path.join(outWay, test[:-3] + ".out.py"), "w") as testout:
44         my_tools.init()
45         sys.stdout = testout
46         sys.stderr = testout
47
48         pylog.write('#-*- coding:utf-8 -*-\n')
49         exec_result.write("Run %s " % test)
50         exec_result.flush()
51
52         try:
53             timeStart = THEBIGTIME.time()
54             execfile(os.path.join(outWay, test), globals(), locals())
55             timeTest = THEBIGTIME.time() - timeStart
56         except SatNotApplicableError as ex:
57             status = "NA"
58             reason = str(ex)
59             exec_result.write("NA\n")
60             timeTest = THEBIGTIME.time() - timeStart
61             pylog.write('status = "NA"\n')
62             pylog.write('time = "' + timeTest.__str__() + '"\n')
63             pylog.write('callback = "%s"\n' % reason)
64         except Exception as ex:
65             status = "KO"
66             reason = ""
67             if ignore.has_key(test):
68                 status = "KF"
69                 reason = "Known Failure = %s\n\n" % ignore[test]
70             exec_result.write("%s\n" % status)
71             timeTest = THEBIGTIME.time() - timeStart
72             pylog.write('status = "%s" \n' % status)
73             pylog.write('time = "' + timeTest.__str__() + '"\n')
74             pylog.write('callback="""' + reason)
75             exc_type, exc_value, exc_traceback = sys.exc_info()
76             traceback.print_exception(exc_type,
77                                       exc_value,
78                                       exc_traceback,
79                                       None,
80                                       file=pylog)
81             pylog.write('"""\n')
82         else:
83             exec_result.write("OK\n")
84             pylog.write('status = "OK"\n')
85             pylog.write('time = "' + timeTest.__str__() + '"\n')
86
87         pass
88         # testout.close()
89
90       sys.stdout = __stdout__
91       sys.stderr = __stderr__
92       my_tools.writeInFiles(pylog)
93       pass
94       # pylog.close()
95
96   exec_result.write('Close\n')
97   pass
98   # exec_result.close()
99
100 if 'PY' not in '${sessionName}':
101     import salome_utils
102     #killScript = os.path.join(os.environ['KERNEL_ROOT_DIR'],
103     #                          'bin',
104     #                          'salome',
105     #                          'killSalome.py')
106     #cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'],
107     #                                       killScript=killScript,
108     #                                       port=salome_utils.getPortNumber())
109     cmd = 'killSalome.py {port}'.format( port=salome_utils.getPortNumber())
110     os.system(cmd)
111