Browse Source

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).


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@62189 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 10 years ago
parent
commit
5685f0172a

+ 1 - 1
lib/python/gunittest/case.py

@@ -17,7 +17,6 @@ import subprocess
 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 .gmodules import call_module, SimpleModule
 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):

+ 0 - 3
lib/python/gunittest/invoker.py

@@ -17,9 +17,6 @@ import shutil
 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

+ 1 - 2
lib/python/gunittest/main.py

@@ -15,8 +15,7 @@ import os
 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,

+ 3 - 4
lib/python/gunittest/runner.py

@@ -18,8 +18,7 @@ a template. It is not expected that something will left.
 import sys
 import time
 
-import unittest.result
-from unittest.signals import registerResult
+import unittest
 
 __unittest = True
 
@@ -40,7 +39,7 @@ class _WritelnDecorator(object):
         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 @@ class GrassTestRunner(object):
     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()

+ 14 - 0
lib/python/gunittest/utils.py

@@ -58,3 +58,17 @@ def do_doctest_gettext_workaround():
 
     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]...'