Commit 6f04fe268a7fba64510e9a8b9b91cb57b213a8de

Authored by Jürgen Knödlseder
1 parent 2bab4887

Add cscripts unit test base class and refactor test_cssens.py script

cscripts/Makefile.am
@@ -70,6 +70,7 @@ linkscripts = cscaldb \ @@ -70,6 +70,7 @@ linkscripts = cscaldb \
70 # Test scripts to be distributed in $(pyexecdir)/cscripts/tests 70 # Test scripts to be distributed in $(pyexecdir)/cscripts/tests
71 testscripts = tests/__init__.py \ 71 testscripts = tests/__init__.py \
72 ../test/test_python_cscripts.py \ 72 ../test/test_python_cscripts.py \
  73 + ../test/test_cscripts_class.py \
73 ../test/test_cscaldb.py \ 74 ../test/test_cscaldb.py \
74 ../test/test_csmodelinfo.py \ 75 ../test/test_csmodelinfo.py \
75 ../test/test_csmodelmerge.py \ 76 ../test/test_csmodelmerge.py \
pyext/Makefile.am
@@ -48,7 +48,6 @@ testscripts = ctools/tests/__init__.py \ @@ -48,7 +48,6 @@ testscripts = ctools/tests/__init__.py \
48 ../test/test_ctbutterfly.py \ 48 ../test/test_ctbutterfly.py \
49 ../test/test_ctulimit.py \ 49 ../test/test_ctulimit.py \
50 ../test/test_cterror.py \ 50 ../test/test_cterror.py \
51 - ../test/test_cscaldb.py \  
52 ../test/test_pipelines.py 51 ../test/test_pipelines.py
53 testdata = ../test/data/crab.xml \ 52 testdata = ../test/data/crab.xml \
54 ../test/data/crab_events.fits.gz \ 53 ../test/data/crab_events.fits.gz \
test/test_cscripts_class.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# cscripts test base class
  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 gammalib
  23 +
  24 +
  25 +# ======================= #
  26 +# Test class for cscripts #
  27 +# ======================= #
  28 +class cscripts_test(gammalib.GPythonTestSuite):
  29 + """
  30 + Test class for scripts
  31 + """
  32 + # Constructor
  33 + def __init__(self):
  34 + """
  35 + Constructor
  36 + """
  37 + # Return
  38 + return
  39 +
  40 + # Set script name
  41 + def _script(self, name):
  42 + """
  43 + Set script name
  44 + """
  45 + # Kluge to set the command. The installed version has no README file
  46 + if os.path.isfile('README.md'):
  47 + script = '../cscripts/'+name+'.py'
  48 + else:
  49 + script = name
  50 +
  51 + # Return script name
  52 + return name
  53 +
  54 + # Execute command and catch any exception
  55 + def _execute(self, cmd):
  56 + """
  57 + Execute command
  58 + """
  59 + # Execute command, make sure we catch any exception
  60 + try:
  61 + rc = os.system(cmd+' >/dev/null 2>&1')
  62 + except:
  63 + rc = -1
  64 + pass
  65 +
  66 + # Return return code
  67 + return rc
test/test_cssens.py
@@ -21,14 +21,15 @@ @@ -21,14 +21,15 @@
21 import os 21 import os
22 import gammalib 22 import gammalib
23 import cscripts 23 import cscripts
  24 +import test_cscripts_class
24 25
25 26
26 # ============================ # 27 # ============================ #
27 # Test class for cssens script # 28 # Test class for cssens script #
28 # ============================ # 29 # ============================ #
29 -class Test(gammalib.GPythonTestSuite): 30 +class Test(test_cscripts_class.cscripts_test):
30 """ 31 """
31 - Test class for cssens script. 32 + Test class for cssens script
32 33
33 This test class makes unit tests for the cssens script by using it 34 This test class makes unit tests for the cssens script by using it
34 from the command line and from Python. 35 from the command line and from Python.
@@ -37,7 +38,7 @@ class Test(gammalib.GPythonTestSuite): @@ -37,7 +38,7 @@ class Test(gammalib.GPythonTestSuite):
37 # Constructor 38 # Constructor
38 def __init__(self): 39 def __init__(self):
39 """ 40 """
40 - Constructor. 41 + Constructor
41 """ 42 """
42 # Call base class constructor 43 # Call base class constructor
43 gammalib.GPythonTestSuite.__init__(self) 44 gammalib.GPythonTestSuite.__init__(self)
@@ -48,7 +49,7 @@ class Test(gammalib.GPythonTestSuite): @@ -48,7 +49,7 @@ class Test(gammalib.GPythonTestSuite):
48 # Set test functions 49 # Set test functions
49 def set(self): 50 def set(self):
50 """ 51 """
51 - Set all test functions. 52 + Set all test functions
52 """ 53 """
53 # Set test name 54 # Set test name
54 self.name('cssens') 55 self.name('cssens')
@@ -63,13 +64,10 @@ class Test(gammalib.GPythonTestSuite): @@ -63,13 +64,10 @@ class Test(gammalib.GPythonTestSuite):
63 # Test cssens on command line 64 # Test cssens on command line
64 def _test_cmd(self): 65 def _test_cmd(self):
65 """ 66 """
66 - Test cssens on the command line. 67 + Test cssens on the command line
67 """ 68 """
68 - # Kluge to set the command (installed version has no README file)  
69 - if os.path.isfile('README.md'):  
70 - cssens = '../cscripts/cssens.py'  
71 - else:  
72 - cssens = 'cssens' 69 + # Set script name
  70 + cssens = self._script('cssens')
73 71
74 # Setup cssens command 72 # Setup cssens command
75 cmd = cssens+' inmodel="data/crab.xml"'+ \ 73 cmd = cssens+' inmodel="data/crab.xml"'+ \
@@ -78,15 +76,13 @@ class Test(gammalib.GPythonTestSuite): @@ -78,15 +76,13 @@ class Test(gammalib.GPythonTestSuite):
78 ' outfile="cssens_cmd1.dat"'+ \ 76 ' outfile="cssens_cmd1.dat"'+ \
79 ' logfile="cssens_cmd1.log" chatter=1' 77 ' logfile="cssens_cmd1.log" chatter=1'
80 78
81 - # Execute cssens, make sure we catch any exception  
82 - try:  
83 - rc = os.system(cmd+' >/dev/null 2>&1')  
84 - except:  
85 - pass 79 + # Check if execution of wrong command fails
  80 + self.test_assert(self._execute('command that does not exist') != 0,
  81 + 'Self test of test script')
86 82
87 # Check if execution was successful 83 # Check if execution was successful
88 - self.test_assert(rc == 0,  
89 - 'Successful cssens execution on command line') 84 + self.test_assert(self._execute(cmd) == 0,
  85 + 'Check successful execution from command line')
90 86
91 # Check result file 87 # Check result file
92 self._check_result_file('cssens_cmd1.dat') 88 self._check_result_file('cssens_cmd1.dat')
@@ -98,15 +94,9 @@ class Test(gammalib.GPythonTestSuite): @@ -98,15 +94,9 @@ class Test(gammalib.GPythonTestSuite):
98 ' outfile="cssens_cmd2.dat"'+ \ 94 ' outfile="cssens_cmd2.dat"'+ \
99 ' logfile="cssens_cmd2.log" chatter=2' 95 ' logfile="cssens_cmd2.log" chatter=2'
100 96
101 - # Execute cssens, make sure we catch any exception  
102 - try:  
103 - rc = os.system(cmd+' >/dev/null 2>&1')  
104 - except:  
105 - pass  
106 -  
107 # Check if execution failed 97 # Check if execution failed
108 - self.test_assert(rc != 0,  
109 - 'Failure of cssens execution on command line') 98 + self.test_assert(self._execute(cmd) != 0,
  99 + 'Check invalid input file when executed from command line')
110 100
111 # Return 101 # Return
112 return 102 return