Merge commit 'v3.3.1' into try-3.3.1
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr_unittest.py
index a48343c6bf4970d7814b1277bec62aa7bbd6527f..c2c4df2ba5f7a0fd4d26bfe7bdd4cf5329299bb2 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# Copyright 2004 Free Software Foundation, Inc.
+# Copyright 2004,2010 Free Software Foundation, Inc.
 # 
 # This file is part of GNU Radio
 # 
@@ -21,7 +21,8 @@
 # 
 
 import unittest
-import sys
+import gr_xmlrunner
+import sys, os, stat
 
 class TestCase(unittest.TestCase):
     """A subclass of unittest.TestCase that adds additional assertions
@@ -37,7 +38,7 @@ class TestCase(unittest.TestCase):
 
            Note that decimal places (from zero) is usually not the same
            as significant digits (measured from the most signficant digit).
-        """
+       """
         if round(second.real-first.real, places) != 0:
             raise self.failureException, \
                   (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
@@ -106,6 +107,58 @@ TextTestRunner = unittest.TextTestRunner
 TestProgram = unittest.TestProgram
 main = TestProgram
 
+def run(PUT, filename=None):
+    ''' 
+    Runs the unittest on a TestCase and produces an optional XML report
+    PUT:      the program under test and should be a gr_unittest.TestCase
+    filename: an optional filename to save the XML report of the tests
+              this will live in ./.unittests/python
+    '''
+
+    # Run this is given a file name
+    if(filename is not None):
+        basepath = "./.unittests"
+        path = basepath + "/python"
+
+        if not os.path.exists(basepath):
+            os.makedirs(basepath, 0750)
+
+        xmlrunner = None
+        # only proceed if .unittests is writable
+        st = os.stat(basepath)[stat.ST_MODE]
+        if(st & stat.S_IWUSR > 0):
+            # Test if path exists; if not, build it
+            if not os.path.exists(path):
+                os.makedirs(path, 0750)
+
+            # Just for safety: make sure we can write here, too
+            st = os.stat(path)[stat.ST_MODE]
+            if(st & stat.S_IWUSR > 0):
+                # Create an XML runner to filename
+                fout = file(path+"/"+filename, "w")
+                xmlrunner = gr_xmlrunner.XMLTestRunner(fout)
+
+        txtrunner = TextTestRunner(verbosity=1)
+
+        # Run the test; runner also creates XML output file
+        # FIXME: make xmlrunner output to screen so we don't have to do run and main
+        suite = TestLoader().loadTestsFromTestCase(PUT)
+
+        # use the xmlrunner if we can write the the directory
+        if(xmlrunner is not None):
+            xmlrunner.run(suite)
+
+        main()
+        
+        # This will run and fail make check if problem
+        # but does not output to screen.
+        #main(testRunner = xmlrunner)
+
+    else:
+        # If no filename is given, just run the test
+        main()
+
+
 ##############################################################################
 # Executing this module from the command line
 ##############################################################################