[GRASS-SVN] r61195 - in sandbox/wenzeslaus/gunittest: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 8 07:59:49 PDT 2014


Author: wenzeslaus
Date: 2014-07-08 07:59:49 -0700 (Tue, 08 Jul 2014)
New Revision: 61195

Modified:
   sandbox/wenzeslaus/gunittest/case.py
   sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
Log:
gunittest: set and use temporary region as class methods; ignore empty lines in key-value text

Modified: sandbox/wenzeslaus/gunittest/case.py
===================================================================
--- sandbox/wenzeslaus/gunittest/case.py	2014-07-08 14:49:12 UTC (rev 61194)
+++ sandbox/wenzeslaus/gunittest/case.py	2014-07-08 14:59:49 UTC (rev 61195)
@@ -35,10 +35,10 @@
     the firt two, it is recommened to use keyword arguments but not required.
     """
     longMessage = True  # to get both standard and custom message
+    _temp_region = None  # to control the temporary region
 
     def __init__(self, methodName):
         super(TestCase, self).__init__(methodName)
-        self._temp_region = None
 
     def _formatMessage(self, msg, standardMsg):
         """Honour the longMessage attribute when generating failure messages.
@@ -66,39 +66,49 @@
         except UnicodeDecodeError:
             return '%s \n%s' % (safe_repr(msg), safe_repr(standardMsg))
 
-    def use_temp_region(self):
+    @classmethod
+    def use_temp_region(cls):
         """Use temporary region instead of the standard one for this process.
 
-        If you use this method, you have to call it in `setUp()` and call
-        `del_temp_region()` in `tearDown()`. By this you ensure that each test
-        method will have its own region and will not influece others.
+        If you use this method, you have to call it in `setUpClass()`
+        and call `del_temp_region()` in `tearDownClass()`. By this you
+        ensure that each test method will have its own region and will
+        not influece other classes.
 
         ::
 
-            def setUp(self):
+            @classmethod
+            def setUpClass(self):
                 self.use_temp_region()
 
-            def tearDown(self):
+            @classmethod
+            def tearDownClass(self):
                 self.del_temp_region()
 
+        You can also call the methods in `setUp()` and `tearDown()` if
+        you are using them.
+
         Copies the current region to a temporary region with
         ``g.region save=``, then sets ``WIND_OVERRIDE`` to refer
         to that region.
         """
-        # this should be unique for each object because method name is used
-        name = "tmp.%s" % (self.id())
+        # we use just the class name since we rely on the invokation system
+        # where each test file is separate process and nothing runs
+        # in parallel inside
+        name = "tmp.%s" % (cls.__name__)
         call_module("g.region", save=name, overwrite=True)
         os.environ['WIND_OVERRIDE'] = name
-        self._temp_region = name
+        cls._temp_region = name
 
-    def del_temp_region(self):
+    @classmethod
+    def del_temp_region(cls):
         """Remove the temporary region.
 
         Unsets WIND_OVERRIDE and removes any region named by it.
         """
-        assert self._temp_region
+        assert cls._temp_region
         name = os.environ.pop('WIND_OVERRIDE')
-        if name != self._temp_region:
+        if name != cls._temp_region:
             # be strict about usage of region
             raise RuntimeError("Inconsistent use of"
                                " TestCase.use_temp_region, WIND_OVERRIDE"
@@ -107,12 +117,12 @@
                                " by TestCase class"
                                "does not corresond to currently set"
                                " WIND_OVERRIDE ({c})",
-                               n=self._temp_region, c=name)
+                               n=cls._temp_region, c=name)
         call_module("g.remove", quiet=True, region=name)
         # TODO: we don't know if user calls this
         # so perhaps some decorator which would use with statemet
-        # but we have zero chance of infuencing another test
-        # since we use method-specific name for region
+        # but we have zero chance of infuencing another test class
+        # since we use class-specific name for temporary region
 
     def assertLooksLike(self, actual, reference, msg=None):
         """Test that ``actual`` text is the same as ``referece`` with ellipses.
@@ -137,9 +147,9 @@
     def assertCommandKeyValue(self, module, parameters, reference, sep,
                               precision=None, msg=None):
         if isinstance(reference, basestring):
-            reference = text_to_keyvalue(reference, sep=sep)
+            reference = text_to_keyvalue(reference, sep=sep, skip_empty=True)
         stdout = call_module(module, **parameters)
-        raster_univar = text_to_keyvalue(stdout, sep=sep)
+        raster_univar = text_to_keyvalue(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,
@@ -241,6 +251,7 @@
                                    precision=precision)
 
     # TODO: use precision?
+    # TODO: write a test for this method with r.in.ascii
     def assertRasterMinMax(self, map, refmin, refmax, msg=None):
         """Test that raster map minimum and maximum are within limits.
 

Modified: sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py	2014-07-08 14:49:12 UTC (rev 61194)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py	2014-07-08 14:59:49 UTC (rev 61195)
@@ -86,13 +86,15 @@
 class TestRasterMapAssertations(gunittest.TestCase):
     # pylint: disable=R0904
 
-    def setUp(self):
-        self.use_temp_region()
+    @classmethod
+    def setUpClass(cls):
+        cls.use_temp_region()
         # TODO: here we should actually not call self.runModule but call_module
-        self.runModule(Module('g.region', rast='elevation', run_=False))
+        cls.runModule(Module('g.region', rast='elevation', run_=False))
 
-    def tearDown(self):
-        self.del_temp_region()
+    @classmethod
+    def tearDownClass(cls):
+        cls.del_temp_region()
 
     def test_assertRasterFitsUnivar(self):
         self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET,



More information about the grass-commit mailing list