Salome HOME
[MEDCalc] Moved test directory up one level
[modules/med.git] / src / MEDCalc / test / test_qttesting.py
1 # Copyright (C) 2011-2016  CEA/DEN, EDF R&D
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 # Author: A. Bruneton (CEA)
20
21 import unittest, os
22 from posixpath import basename
23
24 class MEDGUITest(unittest.TestCase):
25   def __init__(self, methodName='runTest'):
26     unittest.TestCase.__init__(self, methodName=methodName)
27     self._tmpDir = ""
28     self._removeDir = True
29     
30   def setUp(self):
31     import tempfile
32     self._tmpDir = tempfile.mkdtemp(prefix="med_gui_tests_")
33     self._removeDir = True  # reset for each new test in the TestCase
34
35   def tearDown(self):
36     import shutil
37     unittest.TestCase.tearDown(self)
38     if self._removeDir:
39       shutil.rmtree(self._tmpDir, False)
40     
41   def getTestName(self):
42     """ Return name of the test being currently executed. """
43     return self.id().split(".")[-1]
44
45   def launchSalomeWithScript(self, scriptname, baseline):
46     """ TODO: review this - what is the nicest way to launch SALOME GUI from a Python script? """
47     import shutil, subprocess
48     from medcalc_testutils import GetScriptDir
49     # TODO: review this!
50     salomeCommand = os.path.join(os.environ.get("KERNEL_ROOT_DIR", ""), "bin", "salome", "runSalome.py")
51     pth = os.path.join(GetScriptDir(), scriptname)
52     # Remove a potentially already present image file from the tmp directory:
53     gen_image = os.path.join("/tmp", baseline)
54     try:
55       shutil.rmtree(gen_image)
56     except OSError:
57       pass
58     # Launch SALOME with the test script:
59     status = subprocess.call([salomeCommand, pth])
60     if status:
61       raise Exception("SALOME exited abnormally for this test!")
62     try:
63       # Move generated image to the temporary test directory - ideally test should produce image there directly ...
64       shutil.move(gen_image, self._tmpDir)
65     except IOError:
66       raise Exception("Test script didn't produce expected image '%s'!" % gen_image)
67
68   def compareSnapshot(self, basename):
69     import filecmp
70     from medcalc_testutils import GetBaselineDir
71     
72     base_pth = os.path.join(GetBaselineDir(), basename)
73     gen_path = os.path.join(self._tmpDir, basename)
74     print base_pth, gen_path
75     try:
76       ret = filecmp.cmp(base_pth, gen_path, shallow=False)
77     except OSError:
78       ret = False
79     if not ret:
80       # Keep file if comparison fails
81       self._removeDir = False
82       self.assertTrue(ret, "[%s] -- Failed screenshot equality, or unable to open baseline file - directory is kept alive: %s" % (self.getTestName(), self._tmpDir))
83     return ret
84
85   ##
86   ## Now the tests themselves
87   ##
88   
89   def testScalarMap(self):
90     baseline = "test_scalarmap.png"
91     self.launchSalomeWithScript("test_scalarmap.py", baseline)
92     self.compareSnapshot(baseline)
93
94
95 if __name__ == "__main__":
96   suite = unittest.TestSuite()
97   suite.addTest(MEDGUITest('testScalarMap'))
98 #  suite.addTest(MEDGUITest('testIsoContour'))
99   unittest.TextTestRunner().run(suite)
100