From 823cd196475ed6b6468580eb0ca4838b15ca1179 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 17 Oct 2015 11:07:11 +0200 Subject: [PATCH] Use Python unit tests in the ctools.test() function. --- pyext/Makefile.am | 29 ++++++++++++++++++++++++++--- pyext/ctools/__init__.py | 39 +++++++++++++++++++-------------------- pyext/ctools/tools.i | 7 +++++++ test/test_python.py | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 95 insertions(+), 31 deletions(-) diff --git a/pyext/Makefile.am b/pyext/Makefile.am index a9a6de0..6553a14 100755 --- a/pyext/Makefile.am +++ b/pyext/Makefile.am @@ -30,13 +30,36 @@ if WITH_PYTHON # Test scripts to be distributed in $(pythondir)/ctools/tests -testscripts = ctools/tests/__init__.py \ - ctools/tests/unbinned.py +testscripts = $(abs_top_srcdir)/test/test_python.py \ + $(abs_top_srcdir)/test/test_ctobssim.py \ + $(abs_top_srcdir)/test/test_ctselect.py \ + $(abs_top_srcdir)/test/test_ctbin.py \ + $(abs_top_srcdir)/test/test_ctlike.py \ + $(abs_top_srcdir)/test/test_cttsmap.py \ + $(abs_top_srcdir)/test/test_ctmodel.py \ + $(abs_top_srcdir)/test/test_ctskymap.py \ + $(abs_top_srcdir)/test/test_ctexpcube.py \ + $(abs_top_srcdir)/test/test_ctpsfcube.py \ + $(abs_top_srcdir)/test/test_ctbkgcube.py \ + $(abs_top_srcdir)/test/test_ctcubemask.py \ + $(abs_top_srcdir)/test/test_ctbutterfly.py \ + $(abs_top_srcdir)/test/test_ctulimit.py \ + $(abs_top_srcdir)/test/test_cterror.py \ + $(abs_top_srcdir)/test/test_pipelines.py +testdata = $(abs_top_srcdir)/test/data/crab.xml \ + $(abs_top_srcdir)/test/data/crab_events.fits.gz \ + $(abs_top_srcdir)/test/data/crab_cntmap.fits.gz \ + $(abs_top_srcdir)/test/data/exclusion.reg +testirf = $(abs_top_srcdir)/test/irf/cta_dummy_irf.dat # Distribute scripts -EXTRA_DIST = $(testscripts) +EXTRA_DIST = $(testscripts) $(testdata) $(testirf) testscriptsdir = $(pythondir)/ctools/tests testscripts_DATA = $(testscripts) +testdatadir = $(pythondir)/ctools/tests/data +testdata_DATA = $(testdata) +testirfdir = $(pythondir)/ctools/tests/irf +testirf_DATA = $(testirf) # Set SWIG interface filename SWIG_INTERFACE = ctools/tools.i diff --git a/pyext/ctools/__init__.py b/pyext/ctools/__init__.py index 10a8c36..d8626e7 100644 --- a/pyext/ctools/__init__.py +++ b/pyext/ctools/__init__.py @@ -1,3 +1,22 @@ +# ========================================================================== +# ctools Python module +# +# Copyright (C) 2015 Juergen Knoedlseder +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# ========================================================================== # import gammalib first import gammalib @@ -15,23 +34,3 @@ del tools del gammalib del bad_entries del entry - -# Add test function -def test(verbosity=2): - """ - Run ctools tests. - """ - # Imports - import unittest - from .tests.unbinned import pipelines - - # Create a test suite - test_suite = unittest.TestSuite() - - # Test cases must be listed manually here ... - # We currently don't use unittest for test collection - test_suite.addTest(unittest.makeSuite(pipelines)) - - # Create a test runner and run the test - test_runner = unittest.TextTestRunner(verbosity=verbosity) - test_runner.run(test_suite) diff --git a/pyext/ctools/tools.i b/pyext/ctools/tools.i index bc3a0ce..cde2403 100755 --- a/pyext/ctools/tools.i +++ b/pyext/ctools/tools.i @@ -60,3 +60,10 @@ %include "ctbutterfly.i" %include "ctulimit.i" %include "cterror.i" + +/* __ Test function ______________________________________________________ */ +%pythoncode %{ +def test(): + from ctools.tests import test_python + test_python.test(installed=True) +%} \ No newline at end of file diff --git a/test/test_python.py b/test/test_python.py index 4f6daf6..e603201 100755 --- a/test/test_python.py +++ b/test/test_python.py @@ -39,10 +39,10 @@ import test_cterror import test_pipelines -# ======================== # -# Main routine entry point # -# ======================== # -if __name__ == '__main__': +# ================== # +# Perform unit tests # +# ================== # +def test(installed=False): """ Perform unit testing for Python interface. """ @@ -100,20 +100,41 @@ if __name__ == '__main__': suites.append(suite_cterror) suites.append(suite_pipelines) + # If we have an installed version then create a temporary + # directory and copy over all information that is needed + if installed: + + # Create temporary working directory + import tempfile + path = tempfile.mkdtemp() + os.chdir(path) + + # Get test directory + import inspect + testdir = inspect.getfile(ctools.tests) + head, tail = os.path.split(testdir) + + # Copy over test data and irf + os.system("cp -r %s %s" % (head+"/data", "data")) + os.system("cp -r %s %s" % (head+"/irf", "irf")) + # Set PFILES environment variable try: os.mkdir("pfiles") except: pass - os.system("cp -r ../src/*/*.par pfiles/") os.environ['PFILES'] = "pfiles" + # If we have a non-installed version then copy over pfiles + if not installed: + os.system("cp -r ../src/*/*.par pfiles/") + # Run test suite success = suites.run() - - # Save test results - suites.save("reports/ctools.xml") + # If we have a non-installed version then save test results + if not installed: + suites.save("reports/ctools.xml") # Set return code if success: @@ -121,5 +142,19 @@ if __name__ == '__main__': else: rc = 1 + # Return + return rc + + +# ======================== # +# Main routine entry point # +# ======================== # +if __name__ == '__main__': + """ + Perform unit testing for Python interface. + """ + # Run tests + rc = test() + # Exit with return code sys.exit(rc) -- libgit2 0.21.4