You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jd...@apache.org on 2020/05/26 15:15:24 UTC

[qpid-proton] 01/02: PROTON-2181 in tests/py/test_unittest.py, try unittest, then unittest2, otherwise monkeypatch

This is an automated email from the ASF dual-hosted git repository.

jdanek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git

commit c0bdab62d245128869bb7d7084a986b3fa5102d3
Author: Jiri Danek <jd...@redhat.com>
AuthorDate: Fri Feb 21 11:17:45 2020 +0100

    PROTON-2181 in tests/py/test_unittest.py, try unittest, then unittest2, otherwise monkeypatch
---
 tests/py/test_unittest.py | 95 +++++++++++++++++++++++++++++------------------
 1 file changed, 59 insertions(+), 36 deletions(-)

diff --git a/tests/py/test_unittest.py b/tests/py/test_unittest.py
index 8073ce2..623d63f 100644
--- a/tests/py/test_unittest.py
+++ b/tests/py/test_unittest.py
@@ -21,40 +21,63 @@ from __future__ import absolute_import
 from __future__ import division
 from __future__ import print_function
 
-import unittest
+import sys
 
-# Monkey-patch a few unittest 2.7 features for Python 2.6.
-#
-# These are not the pretty versions provided by 2.7 but they do the
-# same job as far as correctness is concerned.
-
-if not hasattr(unittest.TestCase, "assertMultiLineEqual"):
-    def assertMultiLineEqual(self, a, b, msg=None): self.assertEqual(a, b, msg)
-    unittest.TestCase.assertMultiLineEqual = assertMultiLineEqual
-
-if not hasattr(unittest.TestCase, "assertIn"):
-    def assertIn(self, a, b, msg=None): self.assertTrue(a in b, msg)
-    unittest.TestCase.assertIn = assertIn
-
-if not hasattr(unittest.TestCase, "assertIsNone"):
-    def assertIsNone(self, obj, msg=None): self.assertEqual(obj, None, msg)
-    unittest.TestCase.assertIsNone = assertIsNone
-
-if not hasattr(unittest, "skip"):
-    def skip(reason="Test skipped"):
-        return lambda f: print(reason)
-    unittest.skip = skip
-
-if not hasattr(unittest, "skipIf"):
-    def skipIf(condition, reason):
-        if condition:
-            return skip(reason)
-        return lambda f: f
-    unittest.skipIf = skipIf
-
-if not hasattr(unittest, "skipUnless"):
-    def skipUnless(condition, reason):
-        if not condition:
-            return skip(reason)
-        return lambda f: f
-    unittest.skipUnless = skipUnless
+__all__ = ['unittest']
+
+
+def _monkey_patch():
+    """Monkey-patch a few unittest 2.7 features for Python 2.6.
+
+    These are not the pretty versions provided by 2.7, but they do the
+    same job as far as correctness is concerned.
+
+    Only used as a measure of last resort if unittest2 is not available.
+    """
+    if not hasattr(unittest.TestCase, "assertMultiLineEqual"):
+        def assertMultiLineEqual(self, a, b, msg=None): self.assertEqual(a, b, msg)
+
+        unittest.TestCase.assertMultiLineEqual = assertMultiLineEqual
+
+    if not hasattr(unittest.TestCase, "assertIn"):
+        def assertIn(self, a, b, msg=None): self.assertTrue(a in b, msg)
+
+        unittest.TestCase.assertIn = assertIn
+
+    if not hasattr(unittest.TestCase, "assertIsNone"):
+        def assertIsNone(self, obj, msg=None): self.assertEqual(obj, None, msg)
+
+        unittest.TestCase.assertIsNone = assertIsNone
+
+    if not hasattr(unittest, "skip"):
+        def skip(reason="Test skipped"):
+            return lambda f: print(reason)
+
+        unittest.skip = skip
+
+    if not hasattr(unittest, "skipIf"):
+        def skipIf(condition, reason):
+            if condition:
+                return skip(reason)
+            return lambda f: f
+
+        unittest.skipIf = skipIf
+
+    if not hasattr(unittest, "skipUnless"):
+        def skipUnless(condition, reason):
+            if not condition:
+                return skip(reason)
+            return lambda f: f
+
+        unittest.skipUnless = skipUnless
+
+
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    try:
+        import unittest2 as unittest
+    except ImportError:
+        import unittest
+
+        _monkey_patch()


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org