[GRASS-SVN] r61198 - sandbox/wenzeslaus/gunittest

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 8 10:10:14 PDT 2014


Author: wenzeslaus
Date: 2014-07-08 10:10:14 -0700 (Tue, 08 Jul 2014)
New Revision: 61198

Modified:
   sandbox/wenzeslaus/gunittest/case.py
   sandbox/wenzeslaus/gunittest/testing.rst
Log:
gunittest: allow to run modules also with parameters dictionary and allow PyGRASS Module in assert key-value methods

Modified: sandbox/wenzeslaus/gunittest/case.py
===================================================================
--- sandbox/wenzeslaus/gunittest/case.py	2014-07-08 16:09:50 UTC (rev 61197)
+++ sandbox/wenzeslaus/gunittest/case.py	2014-07-08 17:10:14 UTC (rev 61198)
@@ -144,12 +144,14 @@
     # TODO: auto-determine precision based on the map type
     # TODO: we can have also more general function without the subset reference
     # TODO: implement this also for PyGRASS Module
-    def assertCommandKeyValue(self, module, parameters, reference, sep,
-                              precision=None, msg=None):
+    def assertCommandKeyValue(self, module, reference, sep,
+                              precision=None, msg=None, **parameters):
         if isinstance(reference, basestring):
             reference = text_to_keyvalue(reference, sep=sep, skip_empty=True)
-        stdout = call_module(module, **parameters)
-        raster_univar = text_to_keyvalue(stdout, sep=sep, skip_empty=True)
+        module = _module_from_parameters(module, **parameters)
+        self.runModule(module)
+        raster_univar = text_to_keyvalue(module.outputs.stdout,
+                                         sep=sep, skip_empty=True)
         if not compare_keyvalue(dict_a=reference, dict_b=raster_univar,
                                 a_is_subset=True, precision=precision):
             unused, missing, mismatch = diff_keyvalue(dict_a=reference,
@@ -192,9 +194,9 @@
         for the full interface of arbitrary module.
         """
         self.assertCommandKeyValue(module='r.univar',
-                                   parameters=dict(map=raster,
-                                                   separator='=',
-                                                   flags='g'),
+                                   map=raster,
+                                   separator='=',
+                                   flags='g',
                                    reference=reference, msg=msg, sep='=',
                                    precision=precision)
 
@@ -215,7 +217,7 @@
         -e (extended metadata) flags.
         """
         self.assertCommandKeyValue(module='r.info',
-                                   parameters=dict(map=raster, flags='gre'),
+                                   map=raster, flags='gre',
                                    reference=reference, msg=msg, sep='=',
                                    precision=precision)
 
@@ -246,9 +248,9 @@
         if where:
             parameters.update(where=where)
         self.assertCommandKeyValue(module='v.univar',
-                                   parameters=parameters,
                                    reference=reference, msg=msg, sep='=',
-                                   precision=precision)
+                                   precision=precision,
+                                   **parameters)
 
     # TODO: use precision?
     # TODO: write a test for this method with r.in.ascii
@@ -358,10 +360,7 @@
 
         :raises CalledModuleError: if the module failed
         """
-        if kwargs:
-            if not isinstance(module, basestring):
-                raise ValueError('module can be only string or PyGRASS Module')
-            module = Module(module, run_=False, **kwargs)
+        module = _module_from_parameters(module, **kwargs)
 
         if module.run_:
             raise ValueError('Do not run the module manually, set run_=False')
@@ -415,12 +414,7 @@
         Runs the module and causes test failure if module ends with
         non-zero return code.
         """
-        if kwargs:
-            if not isinstance(module, basestring):
-                raise ValueError('module can be only string or PyGRASS Module')
-            if isinstance(module, Module):
-                raise ValueError('module can be only string if other parameters are given')
-            module = Module(module, run_=False, **kwargs)
+        module = _module_from_parameters(module, **kwargs)
 
         # TODO: merge stderr to stdout? if caller gives PIPE, for sure not
         if module.run_:
@@ -445,7 +439,14 @@
         if module.popen.returncode:
             # TODO: message format
             # TODO: stderr?
-            stdmsg = 'Running <%s> ended with non-zero return code' % module.get_python()
+            stdmsg = ('Running <{m.name}> module ended'
+                      ' with non-zero return code ({m.popen.returncode})\n'
+                      'Called: {code}\n'
+                      'See the folowing errors:\n'
+                      '{errors}'.format(
+                          m=module, code=module.get_python(),
+                          errors=module.outputs["stderr"].value
+                      ))
             self.fail(self._formatMessage(msg, stdmsg))
 
         # log these to final report
@@ -455,7 +456,9 @@
         # module.outputs['stderr'].value
 
     # TODO: should we merge stderr to stdout in this case?
-    def assertModuleFail(self, module, msg=None):
+    def assertModuleFail(self, module, msg=None, **kwargs):
+        module = _module_from_parameters(module, **kwargs)
+
         if module.run_:
             raise ValueError('Do not run the module manually, set run_=False')
         if not module.finish_:
@@ -479,3 +482,17 @@
             stdmsg = ('Running <%s> ended with zero (successful) return code'
                       ' when expecting module to fail' % module.get_python())
             self.fail(self._formatMessage(msg, stdmsg))
+
+
+def _module_from_parameters(module, **kwargs):
+    if kwargs:
+        if not isinstance(module, basestring):
+            raise ValueError('module can be only string or PyGRASS Module')
+        if isinstance(module, Module):
+            raise ValueError('module can be only string if other'
+                             ' parameters are given')
+            # allow to pass all parameters in one dictionary called parameters
+        if kwargs.keys() == ['parameters']:
+            kwargs = kwargs['parameters']
+        module = Module(module, run_=False, **kwargs)
+    return module

Modified: sandbox/wenzeslaus/gunittest/testing.rst
===================================================================
--- sandbox/wenzeslaus/gunittest/testing.rst	2014-07-08 16:09:50 UTC (rev 61197)
+++ sandbox/wenzeslaus/gunittest/testing.rst	2014-07-08 17:10:14 UTC (rev 61198)
@@ -46,6 +46,7 @@
 
 All test files in all ``testsuite`` directories will be executed and
 a report will be created in a newly created ``testreport`` directory.
+Open the file ``testreport/index.html`` to browse though the results.
 You need to be in GRASS session to run the tests.
 
 The test_data_category parameter serves to filter tests accoring to data



More information about the grass-commit mailing list