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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jul 7 14:22:27 PDT 2014


Author: wenzeslaus
Date: 2014-07-07 14:22:27 -0700 (Mon, 07 Jul 2014)
New Revision: 61174

Modified:
   sandbox/wenzeslaus/gunittest/__init__.py
   sandbox/wenzeslaus/gunittest/case.py
   sandbox/wenzeslaus/gunittest/invoker.py
   sandbox/wenzeslaus/gunittest/loader.py
   sandbox/wenzeslaus/gunittest/main.py
   sandbox/wenzeslaus/gunittest/reporters.py
   sandbox/wenzeslaus/gunittest/suite.py
   sandbox/wenzeslaus/gunittest/testing.rst
   sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
   sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py
   sandbox/wenzeslaus/gunittest/testsuite/test_doctests.py
   sandbox/wenzeslaus/gunittest/testsuite/test_gmodules.py
   sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py
Log:
gunittest: use correct relative imports, leave out Grass from class names (TestCase and TestSuite)

Modified: sandbox/wenzeslaus/gunittest/__init__.py
===================================================================
--- sandbox/wenzeslaus/gunittest/__init__.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/__init__.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -1 +1,2 @@
-import case, checkers, gmodules, utils
+from .case import TestCase
+from .main import test

Modified: sandbox/wenzeslaus/gunittest/case.py
===================================================================
--- sandbox/wenzeslaus/gunittest/case.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/case.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -25,7 +25,7 @@
                        file_md5, files_equal_md5)
 
 
-class GrassTestCase(unittest.TestCase):
+class TestCase(unittest.TestCase):
     # we dissable R0904 for all TestCase classes because their purpose is to
     # provide a lot of assert methods
     # pylint: disable=R0904
@@ -35,7 +35,7 @@
     the firt two, it is recommened to use keyword arguments but not required.
     """
     def __init__(self, methodName):
-        super(GrassTestCase, self).__init__(methodName)
+        super(TestCase, self).__init__(methodName)
         self._temp_region = None
 
     def use_temp_region(self):
@@ -64,10 +64,10 @@
         if name != self._temp_region:
             # be strict about usage of region
             raise RuntimeError("Inconsistent use of"
-                               " GrassTestCase.use_temp_region, WIND_OVERRIDE"
+                               " TestCase.use_temp_region, WIND_OVERRIDE"
                                " or temporary region in general\n"
                                "Region to which should be now deleted ({n})"
-                               " by GrassTestCase class"
+                               " by TestCase class"
                                "does not corresond to currently set"
                                " WIND_OVERRIDE ({c})",
                                n=self._temp_region, c=name)
@@ -342,10 +342,6 @@
 
         :raises CalledModuleError: if the module failed
         """
-        # TODO: using call_module for compatibility with run_command rules
-        call_module(module, **kwargs)
-        return
-        
         if kwargs:
             if not isinstance(module, basestring):
                 raise ValueError('module can be only string or PyGRASS Module')

Modified: sandbox/wenzeslaus/gunittest/invoker.py
===================================================================
--- sandbox/wenzeslaus/gunittest/invoker.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/invoker.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -9,9 +9,9 @@
 from unittest.main import TestProgram, USAGE_AS_MAIN
 TestProgram.USAGE = USAGE_AS_MAIN
 
-from loader import GrassTestLoader, discover_modules
-from reporters import GrassTestFilesReporter
-import utils
+from .loader import GrassTestLoader, discover_modules
+from .reporters import GrassTestFilesReporter
+from .utils import silent_rmtree, ensure_dir
 
 import grass.script.setup as gsetup
 
@@ -56,7 +56,7 @@
         # TODO: use grass module to do this? but we are not in the right gisdbase
         mapset_dir = os.path.join(gisdbase, location, mapset)
         if self.clean_before:
-            utils.silent_rmtree(mapset_dir)
+            silent_rmtree(mapset_dir)
         os.mkdir(mapset_dir)
         # TODO: default region in mapset will be what?
         # copy WIND file from PERMANENT
@@ -68,7 +68,7 @@
 
     def _run_test_module(self, module, results_dir, gisdbase, location):
         cwd = os.path.join(results_dir, module.tested_dir, module.name)
-        utils.ensure_dir(os.path.abspath(cwd))
+        ensure_dir(os.path.abspath(cwd))
         # TODO: put this to constructor and copy here again
         env = os.environ.copy()
         mapset, mapset_dir = self._create_mapset(gisdbase, location, module)

Modified: sandbox/wenzeslaus/gunittest/loader.py
===================================================================
--- sandbox/wenzeslaus/gunittest/loader.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/loader.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -9,7 +9,7 @@
 import collections
 import importlib
 
-import suite
+from .suite import TestSuite
 
 
 GrassTestPythonModule = collections.namedtuple('GrassTestPythonModule',
@@ -73,7 +73,7 @@
     skip_dirs = ['.svn', 'dist.*', 'bin.*', 'OBJ.*']
     testsuite_dir = 'testsuite'
     files_in_testsuite = '*.py'
-    suiteClass = suite.GrassTestSuite
+    suiteClass = TestSuite
     all_tests_value = 'all'
     universal_tests_value = 'universal'
 

Modified: sandbox/wenzeslaus/gunittest/main.py
===================================================================
--- sandbox/wenzeslaus/gunittest/main.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/main.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -9,7 +9,7 @@
 from loader import GrassTestLoader
 from runner import GrassTestRunner
 from invoker import GrassTestFilesInvoker
-import utils
+from utils import silent_rmtree
 
 import grass.script.core as gcore
 
@@ -59,6 +59,7 @@
     cov.stop()
     cov.html_report(directory='testcodecoverage')
 
+    # TODO: is sys.exit the right thing here
     sys.exit(not program.result.wasSuccessful())
 
 
@@ -105,7 +106,7 @@
         sys.stderr.write("GISDBASE <%s> does not exist\n" % gisdbase)
         sys.exit(1)
     results_dir = 'testreport'
-    utils.silent_rmtree(results_dir)  # TODO: too brute force?
+    silent_rmtree(results_dir)  # TODO: too brute force?
 
     invoker = GrassTestFilesInvoker(start_dir='.')
     invoker.run_in_location(gisdbase=gisdbase,

Modified: sandbox/wenzeslaus/gunittest/reporters.py
===================================================================
--- sandbox/wenzeslaus/gunittest/reporters.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/reporters.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -5,7 +5,7 @@
 import datetime
 import xml.sax.saxutils as saxutils
 
-import utils
+from utils import ensure_dir
 
 
 def html_escape(text):
@@ -40,7 +40,7 @@
 
     def __init__(self, results_dir):
         # TODO: no directory cleaning (self.clean_before)? now cleaned by caller
-        utils.ensure_dir(os.path.abspath(results_dir))
+        ensure_dir(os.path.abspath(results_dir))
 
         # having all variables public although not really part of API
         self.main_index = open(os.path.join(results_dir, 'index.html'), 'w')
@@ -67,6 +67,7 @@
     def start_file_test(self, module):
         self.file_start_time = datetime.datetime.now()
         self._start_file_test_called = True
+        self.main_index.flush()  # to get previous ones to the report
 
     def wrap_stdstream_to_html(self, infile, outfile, module, stream):
         before = '<html><body><h1>%s</h1><pre>' % (module.name + ' ' + stream)

Modified: sandbox/wenzeslaus/gunittest/suite.py
===================================================================
--- sandbox/wenzeslaus/gunittest/suite.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/suite.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -3,7 +3,8 @@
 import unittest
 
 
-class GrassTestSuite(unittest.TestSuite):
+# TODO: this is not necessary if we handle separate files as separate processes
+class TestSuite(unittest.TestSuite):
 
     def run(self, result, debug=False):
         """

Modified: sandbox/wenzeslaus/gunittest/testing.rst
===================================================================
--- sandbox/wenzeslaus/gunittest/testing.rst	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/testing.rst	2014-07-07 21:22:27 UTC (rev 61174)
@@ -27,7 +27,7 @@
     ::
     import guinttest
 
-    class TestPython(gunittest.GrassTestCase):
+    class TestPython(gunittest.TestCase):
 
         def test_counting(self):
             """Test that Python can count to two"""
@@ -41,7 +41,8 @@
 
 To run (invoke) all tests in the source tree run::
 
-    python gunittest/main.py [gisdbase] location test_data_category
+    export PYTHONPATH=.../sandbox/wenzeslaus:$PYTHONPATH
+    python python -m gunittest.main [gisdbase] location test_data_category
 
 All test files in all ``testsuite`` directories will be executed and
 a report will be created in a newly created ``testreport`` directory.
@@ -66,7 +67,7 @@
 
 Read the documentation of Python ``unittest`` package for a list of assert
 methods which you can use to test your results. For test of more complex
-GRASS-specific results refer to `GrassTestCase` class documentation.
+GRASS-specific results refer to `TestCase` class documentation.
 
 
 Tests of GRASS modules
@@ -74,7 +75,7 @@
 
 ::
 
-    class TestRInfo(gunittest.GrassTestCase):
+    class TestRInfo(gunittest.TestCase):
 
         def test_elevation(self):
             rinfo = Module('r.info', map='elevation', flags='g',
@@ -90,7 +91,7 @@
 
 ::
 
-    class TestRInfoInputHandling(gunittest.GrassTestCase):
+    class TestRInfoInputHandling(gunittest.TestCase):
 
         def test_rinfo_wrong_map(self):
             map_name = 'does_not_exist'

Modified: sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -5,18 +5,15 @@
 """
 
 
-import sys
 import os
 
 import grass.script.core as gcore
 from grass.pygrass.modules import Module
 
-# import gunittest as a package so that relative imports there works
-sys.path.insert(0, os.path.split(os.path.split((os.path.dirname(os.path.abspath(__file__))))[0])[0])
-from gunittest.case import GrassTestCase
+import gunittest
 
 
-class TestTextAssertions(GrassTestCase):
+class TestTextAssertions(gunittest.TestCase):
     # pylint: disable=R0904
     def test_assertLooksLike(self):
         self.assertLooksLike("Generated map is <elevation>",
@@ -86,7 +83,7 @@
 """
 
 
-class TestRasterMapAssertations(GrassTestCase):
+class TestRasterMapAssertations(gunittest.TestCase):
     # pylint: disable=R0904
 
     def setUp(self):
@@ -127,7 +124,7 @@
         self.assertRasterFitsInfo('elevation', ELEVATION_MAPSET_DICT)
 
 
-class TestVectorMapAssertations(GrassTestCase):
+class TestVectorMapAssertations(gunittest.TestCase):
     # pylint: disable=R0904
     def test_assertVectorFitsUnivar(self):
         self.assertVectorFitsUnivar(map='bridges', column='WIDTH',
@@ -144,7 +141,7 @@
                           reference=RANDOM_KEYVALUES)
 
 
-class TestFileAssertations(GrassTestCase):
+class TestFileAssertations(gunittest.TestCase):
     # pylint: disable=R0904
 
     @classmethod
@@ -208,5 +205,4 @@
 
 
 if __name__ == '__main__':
-    from gunittest.main import test
-    test()
+    gunittest.test()

Modified: sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -14,20 +14,15 @@
 """
 
 
-import sys
-import os
-
 from grass.script.core import parse_key_val
 
-# import gunittest as a package so that relative imports there works
-sys.path.insert(0, os.path.split(os.path.split((os.path.dirname(os.path.abspath(__file__))))[0])[0])
-from gunittest.case import GrassTestCase
+import gunittest
 from gunittest.checkers import (values_equal, text_to_keyvalue,
                                 compare_keyvalue,
                                 proj_info_equals, proj_units_equals)
 
 
-class TestValuesEqual(GrassTestCase):
+class TestValuesEqual(gunittest.TestCase):
 
     def test_floats(self):
         self.assertTrue(values_equal(5.0, 5.0))
@@ -120,7 +115,7 @@
 # what about keys and lower/upper case letters
 
 
-class TestTextToKeyValue(GrassTestCase):
+class TestTextToKeyValue(gunittest.TestCase):
     def test_conversion(self):
         keyvals = text_to_keyvalue(KEYVAL_TEXT, sep=':', val_sep=',')
         expected = {'s': 'Hello',
@@ -222,14 +217,14 @@
                             'null_cells': 57995100, 'cells': 60020100}
 
 
-class TestComapreProjections(GrassTestCase):
+class TestComapreProjections(gunittest.TestCase):
 
     def test_compare_proj_info(self):
         self.assertTrue(proj_info_equals(PROJ_INFO_TEXT_1, PROJ_INFO_TEXT_2))
         self.assertTrue(proj_units_equals(PROJ_UNITS_TEXT_1, PROJ_UNITS_TEXT_2))
 
 
-class TestParseKeyvalue(GrassTestCase):
+class TestParseKeyvalue(gunittest.TestCase):
 
     def test_shell_script_style(self):
 
@@ -281,7 +276,7 @@
 """
 
 
-class TestRasterMapComparisons(GrassTestCase):
+class TestRasterMapComparisons(gunittest.TestCase):
 
     def test_compare_univars(self):
         self.assertTrue(compare_keyvalue(text_to_keyvalue(R_UNIVAR_ELEVATION,
@@ -314,5 +309,4 @@
 
 
 if __name__ == '__main__':
-    from gunittest.main import test
-    test()
+    gunittest.test()

Modified: sandbox/wenzeslaus/gunittest/testsuite/test_doctests.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_doctests.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_doctests.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -3,12 +3,8 @@
 Tests checkers
 """
 
-import os
-import sys
 import doctest
 
-# import gunittest as a package so that relative imports there works
-sys.path.insert(0, os.path.split(os.path.split((os.path.dirname(os.path.abspath(__file__))))[0])[0])
 import gunittest
 
 
@@ -18,10 +14,10 @@
 # the alternative is to copy 500 from doctest and change what is needed
 # (this might be necessary anyway beacuse of the reports and stdout and stderr)
 doctest.DocFileCase = type('DocFileCase',
-                           (gunittest.case.GrassTestCase,),
+                           (gunittest.TestCase,),
                            dict(doctest.DocFileCase.__dict__))
 doctest.SkipDocTestCase = type('SkipDocTestCase',
-                               (gunittest.case.GrassTestCase,),
+                               (gunittest.TestCase,),
                                dict(doctest.SkipDocTestCase.__dict__))
 
 
@@ -33,5 +29,4 @@
 
 
 if __name__ == '__main__':
-    from gunittest.main import test
-    test()
+    gunittest.test()

Modified: sandbox/wenzeslaus/gunittest/testsuite/test_gmodules.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_gmodules.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_gmodules.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -1,13 +1,8 @@
 # -*- coding: utf-8 -*-
 
-import os
-import sys
 import subprocess
 
-# this should be solved on the level of the testing framework
-sys.path.insert(0, os.path.split(os.path.split((os.path.dirname(os.path.abspath(__file__))))[0])[0])
-
-from gunittest.case import GrassTestCase
+import gunittest
 from gunittest.gmodules import (call_module, CalledModuleError,
                                 run_module, read_module, write_module)
 
@@ -23,7 +18,7 @@
 """
 
 
-class TestCallModuleFunction(GrassTestCase):
+class TestCallModuleFunction(gunittest.TestCase):
 
     def test_output(self):
         output = call_module('g.region', flags='pg')
@@ -94,7 +89,7 @@
                           stderr='any_value_or_type_here')
 
 
-class ModuleFunctionsTest(GrassTestCase):
+class ModuleFunctionsTest(gunittest.TestCase):
 
     def test_run_module_raise(self):
         self.assertRaises(CalledModuleError,
@@ -125,5 +120,4 @@
 
 
 if __name__ == '__main__':
-    from gunittest.main import test
-    test()
+    gunittest.test()

Modified: sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py	2014-07-07 20:25:39 UTC (rev 61173)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py	2014-07-07 21:22:27 UTC (rev 61174)
@@ -1,19 +1,15 @@
 # -*- coding: utf-8 -*-
 
-import sys
-import os
 import copy
 import subprocess
 
 from grass.pygrass.modules import Module
 
-# import gunittest as a package so that relative imports there works
-sys.path.insert(0, os.path.split(os.path.split((os.path.dirname(os.path.abspath(__file__))))[0])[0])
-from gunittest.case import GrassTestCase
+import gunittest
 from gunittest.gmodules import CalledModuleError
 
 
-class TestModuleAssertions(GrassTestCase):
+class TestModuleAssertions(gunittest.TestCase):
     # pylint: disable=R0904
 
     def setUp(self):
@@ -42,5 +38,4 @@
 
 
 if __name__ == '__main__':
-    from gunittest.main import test
-    test()
+    gunittest.test()



More information about the grass-commit mailing list