Modifying the unittest output. XML files are no longer written outside of the build...
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr_unittest.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2010 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio 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, or (at your option)
10 # any later version.
11
12 # GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 import unittest
24 import gr_xmlrunner
25 import sys, os, stat
26
27 class TestCase(unittest.TestCase):
28     """A subclass of unittest.TestCase that adds additional assertions
29
30     Adds new methods assertComplexAlmostEqual,
31     assertComplexTuplesAlmostEqual and assertFloatTuplesAlmostEqual
32     """
33
34     def assertComplexAlmostEqual (self, first, second, places=7, msg=None):
35         """Fail if the two complex objects are unequal as determined by their
36            difference rounded to the given number of decimal places
37            (default 7) and comparing to zero.
38
39            Note that decimal places (from zero) is usually not the same
40            as significant digits (measured from the most signficant digit).
41        """
42         if round(second.real-first.real, places) != 0:
43             raise self.failureException, \
44                   (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
45         if round(second.imag-first.imag, places) != 0:
46             raise self.failureException, \
47                   (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
48         
49     def assertComplexAlmostEqual2 (self, ref, x, abs_eps=1e-12, rel_eps=1e-6, msg=None):
50         """
51         Fail if the two complex objects are unequal as determined by...
52         """
53         if abs(ref - x) < abs_eps:
54             return
55
56         if abs(ref) > abs_eps:
57             if abs(ref-x)/abs(ref) > rel_eps:
58                 raise self.failureException, \
59                       (msg or '%s != %s rel_error = %s rel_limit = %s' % (
60                     `ref`, `x`, abs(ref-x)/abs(ref), `rel_eps` ))
61         else:
62             raise self.failureException, \
63                       (msg or '%s != %s rel_error = %s rel_limit = %s' % (
64                     `ref`, `x`, abs(ref-x)/abs(ref), `rel_eps` ))
65
66
67         
68     def assertComplexTuplesAlmostEqual (self, a, b, places=7, msg=None):
69         self.assertEqual (len(a), len(b))
70         for i in xrange (len(a)):
71             self.assertComplexAlmostEqual (a[i], b[i], places, msg)
72
73     def assertComplexTuplesAlmostEqual2 (self, ref, x,
74                                          abs_eps=1e-12, rel_eps=1e-6, msg=None):
75         self.assertEqual (len(ref), len(x))
76         for i in xrange (len(ref)):
77             try:
78                 self.assertComplexAlmostEqual2 (ref[i], x[i], abs_eps, rel_eps, msg)
79             except self.failureException, e:
80                 #sys.stderr.write("index = %d " % (i,))
81                 #sys.stderr.write("%s\n" % (e,))
82                 raise
83
84     def assertFloatTuplesAlmostEqual (self, a, b, places=7, msg=None):
85         self.assertEqual (len(a), len(b))
86         for i in xrange (len(a)):
87             self.assertAlmostEqual (a[i], b[i], places, msg)
88
89
90     def assertFloatTuplesAlmostEqual2 (self, ref, x,
91                                        abs_eps=1e-12, rel_eps=1e-6, msg=None):
92         self.assertEqual (len(ref), len(x))
93         for i in xrange (len(ref)):
94             try:
95                 self.assertComplexAlmostEqual2 (ref[i], x[i], abs_eps, rel_eps, msg)
96             except self.failureException, e:
97                 #sys.stderr.write("index = %d " % (i,))
98                 #sys.stderr.write("%s\n" % (e,))
99                 raise
100
101
102 TestResult = unittest.TestResult
103 TestSuite = unittest.TestSuite
104 FunctionTestCase = unittest.FunctionTestCase
105 TestLoader = unittest.TestLoader
106 TextTestRunner = unittest.TextTestRunner
107 TestProgram = unittest.TestProgram
108 main = TestProgram
109
110 def run(PUT, filename=None):
111     ''' 
112     Runs the unittest on a TestCase and produces an optional XML report
113     PUT:      the program under test and should be a gr_unittest.TestCase
114     filename: an optional filename to save the XML report of the tests
115               this will live in ./.unittests/python
116     '''
117
118     # Run this is given a file name
119     if(filename is not None):
120         basepath = "./.unittests"
121         path = basepath + "/python"
122
123         if not os.path.exists(basepath):
124             os.makedirs(basepath, 0750)
125
126         xmlrunner = None
127         # only proceed if .unittests is writable
128         st = os.stat(basepath)[stat.ST_MODE]
129         if(st & stat.S_IWUSR > 0):
130             # Test if path exists; if not, build it
131             if not os.path.exists(path):
132                 os.makedirs(path, 0750)
133
134             # Just for safety: make sure we can write here, too
135             st = os.stat(path)[stat.ST_MODE]
136             if(st & stat.S_IWUSR > 0):
137                 # Create an XML runner to filename
138                 fout = file(path+"/"+filename, "w")
139                 xmlrunner = gr_xmlrunner.XMLTestRunner(fout)
140
141         txtrunner = TextTestRunner(verbosity=1)
142
143         # Run the test; runner also creates XML output file
144         # FIXME: make xmlrunner output to screen so we don't have to do run and main
145         suite = TestLoader().loadTestsFromTestCase(PUT)
146
147         # use the xmlrunner if we can write the the directory
148         if(xmlrunner is not None):
149             xmlrunner.run(suite)
150
151         main()
152         
153         # This will run and fail make check if problem
154         # but does not output to screen.
155         #main(testRunner = xmlrunner)
156
157     else:
158         # If no filename is given, just run the test
159         main()
160
161
162 ##############################################################################
163 # Executing this module from the command line
164 ##############################################################################
165
166 if __name__ == "__main__":
167     main(module=None)