[GRASS-SVN] r62189 - grass/trunk/lib/python/gunittest

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Oct 5 18:20:50 PDT 2014


Author: wenzeslaus
Date: 2014-10-05 18:20:50 -0700 (Sun, 05 Oct 2014)
New Revision: 62189

Modified:
   grass/trunk/lib/python/gunittest/case.py
   grass/trunk/lib/python/gunittest/invoker.py
   grass/trunk/lib/python/gunittest/main.py
   grass/trunk/lib/python/gunittest/runner.py
   grass/trunk/lib/python/gunittest/utils.py
Log:
gunittest: basic support for Python 2.6 and appropriate help for individual test file

Only import of modules is supposed to work with 2.6 to allow documentation build and perhaps running the machinery.

However, the individual tests may or may not work since a lot of assert methods and set up class step are missing and are impossible to provide without reimplementing unittest for 2.7. The full compatibility for 2.6 would be possible with unittest2 which even provides 3.2 features in 2.7 but this is a package which has to be installed separately.

The default help is fortunately the right one. The other help is not meant for individual files and is not possible to easily import for both 2.6 and 2.7 (and is not part of the API).


Modified: grass/trunk/lib/python/gunittest/case.py
===================================================================
--- grass/trunk/lib/python/gunittest/case.py	2014-10-05 19:10:48 UTC (rev 62188)
+++ grass/trunk/lib/python/gunittest/case.py	2014-10-06 01:20:50 UTC (rev 62189)
@@ -17,7 +17,6 @@
 import StringIO
 
 import unittest
-from unittest.util import safe_repr
 
 from grass.pygrass.modules import Module
 from grass.exceptions import CalledModuleError
@@ -26,6 +25,7 @@
 from .checkers import (check_text_ellipsis,
                        text_to_keyvalue, keyvalue_equals, diff_keyvalue,
                        file_md5, files_equal_md5)
+from .utils import safe_repr
 
 
 class TestCase(unittest.TestCase):

Modified: grass/trunk/lib/python/gunittest/invoker.py
===================================================================
--- grass/trunk/lib/python/gunittest/invoker.py	2014-10-05 19:10:48 UTC (rev 62188)
+++ grass/trunk/lib/python/gunittest/invoker.py	2014-10-06 01:20:50 UTC (rev 62189)
@@ -17,9 +17,6 @@
 import string
 import subprocess
 
-from unittest.main import TestProgram, USAGE_AS_MAIN
-TestProgram.USAGE = USAGE_AS_MAIN
-
 from .checkers import text_to_keyvalue
 
 from .loader import GrassTestLoader, discover_modules

Modified: grass/trunk/lib/python/gunittest/main.py
===================================================================
--- grass/trunk/lib/python/gunittest/main.py	2014-10-05 19:10:48 UTC (rev 62188)
+++ grass/trunk/lib/python/gunittest/main.py	2014-10-06 01:20:50 UTC (rev 62189)
@@ -15,8 +15,7 @@
 import sys
 import argparse
 
-from unittest.main import TestProgram, USAGE_AS_MAIN
-TestProgram.USAGE = USAGE_AS_MAIN
+from unittest.main import TestProgram
 
 from .loader import GrassTestLoader
 from .runner import (GrassTestRunner, MultiTestResult,

Modified: grass/trunk/lib/python/gunittest/runner.py
===================================================================
--- grass/trunk/lib/python/gunittest/runner.py	2014-10-05 19:10:48 UTC (rev 62188)
+++ grass/trunk/lib/python/gunittest/runner.py	2014-10-06 01:20:50 UTC (rev 62189)
@@ -18,8 +18,7 @@
 import sys
 import time
 
-import unittest.result
-from unittest.signals import registerResult
+import unittest
 
 __unittest = True
 
@@ -40,7 +39,7 @@
         self.write('\n') # text-mode streams translate to \r\n if needed
 
 
-class TestResult(unittest.result.TestResult):
+class TestResult(unittest.TestResult):
     # descriptions and verbosity unused
     # included for compatibility with unittest's TestResult
     # where are also unused, so perhaps we can remove them
@@ -473,7 +472,7 @@
     def run(self, test):
         "Run the given test case or test suite."
         result = self._result
-        registerResult(result)
+        unittest.registerResult(result)
         result.failfast = self.failfast
         result.buffer = self.buffer
         startTime = time.time()

Modified: grass/trunk/lib/python/gunittest/utils.py
===================================================================
--- grass/trunk/lib/python/gunittest/utils.py	2014-10-05 19:10:48 UTC (rev 62188)
+++ grass/trunk/lib/python/gunittest/utils.py	2014-10-06 01:20:50 UTC (rev 62189)
@@ -58,3 +58,17 @@
 
     import __builtin__
     __builtin__._ = new_translator
+
+
+_MAX_LENGTH = 80
+
+# taken from unittest.util (Python 2.7) since it is not part of API
+# but we need it for the same reason as it is used un unittest's TestCase
+def safe_repr(obj, short=False):
+    try:
+        result = repr(obj)
+    except Exception:
+        result = object.__repr__(obj)
+    if not short or len(result) < _MAX_LENGTH:
+        return result
+    return result[:_MAX_LENGTH] + ' [truncated]...'



More information about the grass-commit mailing list