Commit ef19160d4127c97e00a909f69a10dbb5647c53ea

Authored by Jürgen Knödlseder
1 parent 31a1247f

Add unit tests for example scripts

So far, all pipeline example scripts are included in the unit test. Results are not yet checked, but it is assured that all scripts execute without any error.
.gitignore
@@ -81,6 +81,7 @@ test/*.reg @@ -81,6 +81,7 @@ test/*.reg
81 test/*.trs 81 test/*.trs
82 test/*.txt 82 test/*.txt
83 test/test_cscripts.sh 83 test/test_cscripts.sh
  84 +test/test_examples.sh
84 test/reports/*.xml 85 test/reports/*.xml
85 test/butterfly.txt 86 test/butterfly.txt
86 test/.coverage 87 test/.coverage
configure.ac
@@ -1025,6 +1025,7 @@ AC_CONFIG_FILES([Makefile @@ -1025,6 +1025,7 @@ AC_CONFIG_FILES([Makefile
1025 test/test_cscripts.sh 1025 test/test_cscripts.sh
1026 test/test_python_ctools.sh 1026 test/test_python_ctools.sh
1027 test/test_python_cscripts.sh 1027 test/test_python_cscripts.sh
  1028 + test/test_examples.sh
1028 pyext/Makefile 1029 pyext/Makefile
1029 pyext/setup.py 1030 pyext/setup.py
1030 pyext/ctools/__init__.py 1031 pyext/ctools/__init__.py
@@ -1038,6 +1039,7 @@ AC_CONFIG_FILES([Makefile @@ -1038,6 +1039,7 @@ AC_CONFIG_FILES([Makefile
1038 [if test -f test/test_cscripts.sh; then chmod +x test/test_cscripts.sh; fi; 1039 [if test -f test/test_cscripts.sh; then chmod +x test/test_cscripts.sh; fi;
1039 if test -f test/test_python_ctools.sh; then chmod +x test/test_python_ctools.sh; fi; 1040 if test -f test/test_python_ctools.sh; then chmod +x test/test_python_ctools.sh; fi;
1040 if test -f test/test_python_cscripts.sh; then chmod +x test/test_python_cscripts.sh; fi; 1041 if test -f test/test_python_cscripts.sh; then chmod +x test/test_python_cscripts.sh; fi;
  1042 + if test -f test/test_examples.sh; then chmod +x test/test_examples.sh; fi;
1041 if test -f testconf.sh; then chmod +x testconf.sh; fi]) 1043 if test -f testconf.sh; then chmod +x testconf.sh; fi])
1042 AC_OUTPUT 1044 AC_OUTPUT
1043 1045
examples/pipeline_binned_disk.py
@@ -27,7 +27,7 @@ from cscripts import obsutils @@ -27,7 +27,7 @@ from cscripts import obsutils
27 # Setup observations # 27 # Setup observations #
28 # ================== # 28 # ================== #
29 def setup_observations(pattern='four', ra=83.63, dec=22.01, offset=1.5, 29 def setup_observations(pattern='four', ra=83.63, dec=22.01, offset=1.5,
30 - emin=0.1, emax=100.0, rad=5.0, duration=1800.0, 30 + emin=0.1, emax=100.0, rad=5.0, duration=180.0,
31 deadc=0.95, caldb='prod2', irf='South_0.5h'): 31 deadc=0.95, caldb='prod2', irf='South_0.5h'):
32 """ 32 """
33 Returns an observation container. 33 Returns an observation container.
examples/pipeline_binned_mem.py
@@ -34,7 +34,7 @@ from cscripts import obsutils @@ -34,7 +34,7 @@ from cscripts import obsutils
34 # Setup observations # 34 # Setup observations #
35 # ================== # 35 # ================== #
36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5, 36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5,
37 - emin=0.1, emax=100.0, rad=5.0, duration=1800.0, 37 + emin=0.1, emax=100.0, rad=5.0, duration=180.0,
38 deadc=0.95, 38 deadc=0.95,
39 caldb="prod2", irf="South_50h"): 39 caldb="prod2", irf="South_50h"):
40 """ 40 """
examples/pipeline_stacked_disk.py
@@ -34,7 +34,7 @@ from cscripts import obsutils @@ -34,7 +34,7 @@ from cscripts import obsutils
34 # Setup observations # 34 # Setup observations #
35 # ================== # 35 # ================== #
36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5, 36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5,
37 - emin=0.1, emax=100.0, rad=5.0, duration=1800.0, 37 + emin=0.1, emax=100.0, rad=5.0, duration=180.0,
38 deadc=0.95, 38 deadc=0.95,
39 caldb="prod2", irf="South_50h"): 39 caldb="prod2", irf="South_50h"):
40 """ 40 """
examples/pipeline_stacked_mem.py
@@ -34,7 +34,7 @@ from cscripts import obsutils @@ -34,7 +34,7 @@ from cscripts import obsutils
34 # Setup observations # 34 # Setup observations #
35 # ================== # 35 # ================== #
36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5, 36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5,
37 - emin=0.1, emax=100.0, rad=5.0, duration=1800.0, 37 + emin=0.1, emax=100.0, rad=5.0, duration=180.0,
38 deadc=0.95, 38 deadc=0.95,
39 caldb="prod2", irf="South_50h"): 39 caldb="prod2", irf="South_50h"):
40 """ 40 """
examples/pipeline_unbinned_disk.py
@@ -34,7 +34,7 @@ from cscripts import obsutils @@ -34,7 +34,7 @@ from cscripts import obsutils
34 # Setup observations # 34 # Setup observations #
35 # ================== # 35 # ================== #
36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5, 36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5,
37 - emin=0.1, emax=100.0, rad=5.0, duration=1800.0, 37 + emin=0.1, emax=100.0, rad=5.0, duration=180.0,
38 deadc=0.95, 38 deadc=0.95,
39 caldb="prod2", irf="South_50h"): 39 caldb="prod2", irf="South_50h"):
40 """ 40 """
examples/pipeline_unbinned_mem.py
@@ -34,7 +34,7 @@ from cscripts import obsutils @@ -34,7 +34,7 @@ from cscripts import obsutils
34 # Setup observations # 34 # Setup observations #
35 # ================== # 35 # ================== #
36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5, 36 def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5,
37 - emin=0.1, emax=100.0, rad=5.0, duration=1800.0, 37 + emin=0.1, emax=100.0, rad=5.0, duration=180.0,
38 deadc=0.95, 38 deadc=0.95,
39 caldb="prod2", irf="South_50h"): 39 caldb="prod2", irf="South_50h"):
40 """ 40 """
test/Makefile.am
@@ -9,6 +9,7 @@ if WITH_PYTHON @@ -9,6 +9,7 @@ if WITH_PYTHON
9 TEST_CSCRIPTS = test_cscripts.sh 9 TEST_CSCRIPTS = test_cscripts.sh
10 TEST_PYTHON_CTOOLS = test_python_ctools.sh 10 TEST_PYTHON_CTOOLS = test_python_ctools.sh
11 TEST_PYTHON_CSCRIPTS = test_python_cscripts.sh 11 TEST_PYTHON_CSCRIPTS = test_python_cscripts.sh
  12 + TEST_PYTHON_EXAMPLES = test_examples.sh
12 TEST_PYTHON_ENV = PYTHONPATH=$(top_builddir):$(top_builddir)/pyext:$(top_builddir)/pyext/build/ctools/ctools:$(pythondir):$(pyexecdir):$(PYTHONPATH) 13 TEST_PYTHON_ENV = PYTHONPATH=$(top_builddir):$(top_builddir)/pyext:$(top_builddir)/pyext/build/ctools/ctools:$(pythondir):$(pyexecdir):$(PYTHONPATH)
13 endif 14 endif
14 15
@@ -18,7 +19,8 @@ TESTS_ENVIRONMENT = @RUNSHARED@=$(top_builddir)/src/.libs$(TEST_ENV_DIR):$(LIBDI @@ -18,7 +19,8 @@ TESTS_ENVIRONMENT = @RUNSHARED@=$(top_builddir)/src/.libs$(TEST_ENV_DIR):$(LIBDI
18 19
19 # Tests (these are the tests to be executed) 20 # Tests (these are the tests to be executed)
20 TESTS = $(TEST_PYTHON_CTOOLS) \ 21 TESTS = $(TEST_PYTHON_CTOOLS) \
21 - $(TEST_PYTHON_CSCRIPTS) 22 + $(TEST_PYTHON_CSCRIPTS) \
  23 + $(TEST_PYTHON_EXAMPLES)
22 24
23 # Add Valgrind rule 25 # Add Valgrind rule
24 valgrind: 26 valgrind:
test/test_examples.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# Performs unit tests for the ctools example scripts
  4 +#
  5 +# Copyright (C) 2016 Juergen Knoedlseder
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
  21 +import os
  22 +import sys
  23 +import gammalib
  24 +import ctools
  25 +import cscripts
  26 +
  27 +
  28 +# =============================================== #
  29 +# Test class for example executables unit testing #
  30 +# =============================================== #
  31 +class Test(gammalib.GPythonTestSuite):
  32 + """
  33 + Test class for GammaLib example executables
  34 + """
  35 + # Constructor
  36 + def __init__(self):
  37 + """
  38 + Constructor.
  39 + """
  40 + # Call base class constructor
  41 + gammalib.GPythonTestSuite.__init__(self)
  42 +
  43 + # Return
  44 + return
  45 +
  46 + # Execute Python script
  47 + def _execute_python(self, name):
  48 + """
  49 + Execute Python script
  50 +
  51 + Parameters
  52 + ----------
  53 + name : str
  54 + Python script name with .py extension.
  55 + """
  56 + # Setup command
  57 + cmd = '../examples/' + name + '.py'
  58 +
  59 + # Execute Python script, make sure we catch any exception
  60 + try:
  61 + rc = os.system(cmd+' > example_'+name+'.log 2>&1')
  62 + except:
  63 + pass
  64 +
  65 + # Check if execution was successful
  66 + self.test_assert(rc == 0, 'Check "'+name+'" script from command line')
  67 +
  68 + # Return
  69 + return
  70 +
  71 + # Set test functions
  72 + def set(self):
  73 + """
  74 + Set all test functions
  75 + """
  76 + # Set test name
  77 + self.name('examples')
  78 +
  79 + # Append tests
  80 + self.append(self.test_pipeline_binned_disk, 'Test pipeline_binned_disk')
  81 + self.append(self.test_pipeline_binned_mem, 'Test pipeline_binned_mem')
  82 + self.append(self.test_pipeline_stacked_disk, 'Test pipeline_stacked_disk')
  83 + self.append(self.test_pipeline_stacked_mem, 'Test pipeline_stacked_mem')
  84 + self.append(self.test_pipeline_unbinned_disk, 'Test pipeline_unbinned_disk')
  85 + self.append(self.test_pipeline_unbinned_mem, 'Test pipeline_unbinned_mem')
  86 +
  87 + # Return
  88 + return
  89 +
  90 + # Test pipeline_binned_disk
  91 + def test_pipeline_binned_disk(self):
  92 + """
  93 + Test pipeline_binned_disk
  94 + """
  95 + # Execute script
  96 + self._execute_python('pipeline_binned_disk')
  97 +
  98 + #TODO: Do any testing
  99 +
  100 + # Return
  101 + return
  102 +
  103 + # Test pipeline_binned_mem
  104 + def test_pipeline_binned_mem(self):
  105 + """
  106 + Test pipeline_binned_mem
  107 + """
  108 + # Execute script
  109 + self._execute_python('pipeline_binned_mem')
  110 +
  111 + #TODO: Do any testing
  112 +
  113 + # Return
  114 + return
  115 +
  116 + # Test pipeline_stacked_disk
  117 + def test_pipeline_stacked_disk(self):
  118 + """
  119 + Test pipeline_stacked_disk
  120 + """
  121 + # Execute script
  122 + self._execute_python('pipeline_stacked_disk')
  123 +
  124 + #TODO: Do any testing
  125 +
  126 + # Return
  127 + return
  128 +
  129 + # Test pipeline_stacked_mem
  130 + def test_pipeline_stacked_mem(self):
  131 + """
  132 + Test pipeline_stacked_mem
  133 + """
  134 + # Execute script
  135 + self._execute_python('pipeline_stacked_mem')
  136 +
  137 + #TODO: Do any testing
  138 +
  139 + # Return
  140 + return
  141 +
  142 + # Test pipeline_unbinned_disk
  143 + def test_pipeline_unbinned_disk(self):
  144 + """
  145 + Test pipeline_unbinned_disk
  146 + """
  147 + # Execute script
  148 + self._execute_python('pipeline_unbinned_disk')
  149 +
  150 + #TODO: Do any testing
  151 +
  152 + # Return
  153 + return
  154 +
  155 + # Test pipeline_unbinned_mem
  156 + def test_pipeline_unbinned_mem(self):
  157 + """
  158 + Test pipeline_unbinned_mem
  159 + """
  160 + # Execute script
  161 + self._execute_python('pipeline_unbinned_mem')
  162 +
  163 + #TODO: Do any testing
  164 +
  165 + # Return
  166 + return
  167 +
  168 +
  169 +# ======================== #
  170 +# Main routine entry point #
  171 +# ======================== #
  172 +if __name__ == '__main__':
  173 +
  174 + # Set calibration database
  175 + os.environ['CALDB'] = '../caldb'
  176 +
  177 + # Set PFILES environment variable
  178 + try:
  179 + os.mkdir('pfiles')
  180 + except:
  181 + pass
  182 + os.environ['PFILES'] = 'pfiles'
  183 +
  184 + # Copy over pfiles
  185 + os.system('cp -r ../src/*/*.par pfiles/')
  186 + os.system('cp -r ../cscripts/*.par pfiles/')
  187 +
  188 + # Allocate test suites
  189 + suites = gammalib.GTestSuites('Examples testing')
  190 +
  191 + # Allocate test suite, setup tests and append them to the container
  192 + suite = Test()
  193 + suite.set()
  194 + suites.append(suite)
  195 +
  196 + # Run test suite
  197 + success = suites.run()
  198 +
  199 + # Save test results
  200 + suites.save('reports/examples.xml')
  201 +
  202 + # Set return code
  203 + if success:
  204 + rc = 0
  205 + else:
  206 + rc = 1
  207 +
  208 + # Exit with return code
  209 + sys.exit(rc)
test/test_examples.sh.in 0 → 100755
  1 +#! /bin/sh
  2 +# ==========================================================================
  3 +# This script launches the examples unit tests
  4 +#
  5 +# Copyright (C) 2016 Juergen Knoedlseder
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
  21 +
  22 +#
  23 +# Run the examples test
  24 +# =====================
  25 +@PYTHON_COVERAGE@./test_examples.py