Commit 823cd196475ed6b6468580eb0ca4838b15ca1179

Authored by Jürgen Knödlseder
1 parent d89b2921

Use Python unit tests in the ctools.test() function.

This avoids code duplication and makes use to the standard unit tests within the installation.
pyext/Makefile.am
... ... @@ -30,13 +30,36 @@
30 30 if WITH_PYTHON
31 31  
32 32 # Test scripts to be distributed in $(pythondir)/ctools/tests
33   -testscripts = ctools/tests/__init__.py \
34   - ctools/tests/unbinned.py
  33 +testscripts = $(abs_top_srcdir)/test/test_python.py \
  34 + $(abs_top_srcdir)/test/test_ctobssim.py \
  35 + $(abs_top_srcdir)/test/test_ctselect.py \
  36 + $(abs_top_srcdir)/test/test_ctbin.py \
  37 + $(abs_top_srcdir)/test/test_ctlike.py \
  38 + $(abs_top_srcdir)/test/test_cttsmap.py \
  39 + $(abs_top_srcdir)/test/test_ctmodel.py \
  40 + $(abs_top_srcdir)/test/test_ctskymap.py \
  41 + $(abs_top_srcdir)/test/test_ctexpcube.py \
  42 + $(abs_top_srcdir)/test/test_ctpsfcube.py \
  43 + $(abs_top_srcdir)/test/test_ctbkgcube.py \
  44 + $(abs_top_srcdir)/test/test_ctcubemask.py \
  45 + $(abs_top_srcdir)/test/test_ctbutterfly.py \
  46 + $(abs_top_srcdir)/test/test_ctulimit.py \
  47 + $(abs_top_srcdir)/test/test_cterror.py \
  48 + $(abs_top_srcdir)/test/test_pipelines.py
  49 +testdata = $(abs_top_srcdir)/test/data/crab.xml \
  50 + $(abs_top_srcdir)/test/data/crab_events.fits.gz \
  51 + $(abs_top_srcdir)/test/data/crab_cntmap.fits.gz \
  52 + $(abs_top_srcdir)/test/data/exclusion.reg
  53 +testirf = $(abs_top_srcdir)/test/irf/cta_dummy_irf.dat
35 54  
36 55 # Distribute scripts
37   -EXTRA_DIST = $(testscripts)
  56 +EXTRA_DIST = $(testscripts) $(testdata) $(testirf)
38 57 testscriptsdir = $(pythondir)/ctools/tests
39 58 testscripts_DATA = $(testscripts)
  59 +testdatadir = $(pythondir)/ctools/tests/data
  60 +testdata_DATA = $(testdata)
  61 +testirfdir = $(pythondir)/ctools/tests/irf
  62 +testirf_DATA = $(testirf)
40 63  
41 64 # Set SWIG interface filename
42 65 SWIG_INTERFACE = ctools/tools.i
... ...
pyext/ctools/__init__.py
  1 +# ==========================================================================
  2 +# ctools Python module
  3 +#
  4 +# Copyright (C) 2015 Juergen Knoedlseder
  5 +#
  6 +# This program is free software: you can redistribute it and/or modify
  7 +# it under the terms of the GNU General Public License as published by
  8 +# the Free Software Foundation, either version 3 of the License, or
  9 +# (at your option) any later version.
  10 +#
  11 +# This program is distributed in the hope that it will be useful,
  12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 +# GNU General Public License for more details.
  15 +#
  16 +# You should have received a copy of the GNU General Public License
  17 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  18 +#
  19 +# ==========================================================================
1 20 # import gammalib first
2 21 import gammalib
3 22  
... ... @@ -15,23 +34,3 @@ del tools
15 34 del gammalib
16 35 del bad_entries
17 36 del entry
18   -
19   -# Add test function
20   -def test(verbosity=2):
21   - """
22   - Run ctools tests.
23   - """
24   - # Imports
25   - import unittest
26   - from .tests.unbinned import pipelines
27   -
28   - # Create a test suite
29   - test_suite = unittest.TestSuite()
30   -
31   - # Test cases must be listed manually here ...
32   - # We currently don't use unittest for test collection
33   - test_suite.addTest(unittest.makeSuite(pipelines))
34   -
35   - # Create a test runner and run the test
36   - test_runner = unittest.TextTestRunner(verbosity=verbosity)
37   - test_runner.run(test_suite)
... ...
pyext/ctools/tools.i
... ... @@ -60,3 +60,10 @@
60 60 %include "ctbutterfly.i"
61 61 %include "ctulimit.i"
62 62 %include "cterror.i"
  63 +
  64 +/* __ Test function ______________________________________________________ */
  65 +%pythoncode %{
  66 +def test():
  67 + from ctools.tests import test_python
  68 + test_python.test(installed=True)
  69 +%}
63 70 \ No newline at end of file
... ...
test/test_python.py
... ... @@ -39,10 +39,10 @@ import test_cterror
39 39 import test_pipelines
40 40  
41 41  
42   -# ======================== #
43   -# Main routine entry point #
44   -# ======================== #
45   -if __name__ == '__main__':
  42 +# ================== #
  43 +# Perform unit tests #
  44 +# ================== #
  45 +def test(installed=False):
46 46 """
47 47 Perform unit testing for Python interface.
48 48 """
... ... @@ -100,20 +100,41 @@ if __name__ == &#39;__main__&#39;:
100 100 suites.append(suite_cterror)
101 101 suites.append(suite_pipelines)
102 102  
  103 + # If we have an installed version then create a temporary
  104 + # directory and copy over all information that is needed
  105 + if installed:
  106 +
  107 + # Create temporary working directory
  108 + import tempfile
  109 + path = tempfile.mkdtemp()
  110 + os.chdir(path)
  111 +
  112 + # Get test directory
  113 + import inspect
  114 + testdir = inspect.getfile(ctools.tests)
  115 + head, tail = os.path.split(testdir)
  116 +
  117 + # Copy over test data and irf
  118 + os.system("cp -r %s %s" % (head+"/data", "data"))
  119 + os.system("cp -r %s %s" % (head+"/irf", "irf"))
  120 +
103 121 # Set PFILES environment variable
104 122 try:
105 123 os.mkdir("pfiles")
106 124 except:
107 125 pass
108   - os.system("cp -r ../src/*/*.par pfiles/")
109 126 os.environ['PFILES'] = "pfiles"
110 127  
  128 + # If we have a non-installed version then copy over pfiles
  129 + if not installed:
  130 + os.system("cp -r ../src/*/*.par pfiles/")
  131 +
111 132 # Run test suite
112 133 success = suites.run()
113 134  
114   -
115   - # Save test results
116   - suites.save("reports/ctools.xml")
  135 + # If we have a non-installed version then save test results
  136 + if not installed:
  137 + suites.save("reports/ctools.xml")
117 138  
118 139 # Set return code
119 140 if success:
... ... @@ -121,5 +142,19 @@ if __name__ == &#39;__main__&#39;:
121 142 else:
122 143 rc = 1
123 144  
  145 + # Return
  146 + return rc
  147 +
  148 +
  149 +# ======================== #
  150 +# Main routine entry point #
  151 +# ======================== #
  152 +if __name__ == '__main__':
  153 + """
  154 + Perform unit testing for Python interface.
  155 + """
  156 + # Run tests
  157 + rc = test()
  158 +
124 159 # Exit with return code
125 160 sys.exit(rc)
... ...