summaryrefslogtreecommitdiffstats
path: root/testsuite/common.py
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-20 17:26:22 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-21 13:33:20 -0400
commitd0efac8fde1f2bfc77b45899073c225201c64a2c (patch)
tree43e6cc017172b4da3b0145a7a7a3b2270a17c012 /testsuite/common.py
parent0fd2d233f460fa868a62726c37a30ae06f1b0797 (diff)
downloadbcfg2-d0efac8fde1f2bfc77b45899073c225201c64a2c.tar.gz
bcfg2-d0efac8fde1f2bfc77b45899073c225201c64a2c.tar.bz2
bcfg2-d0efac8fde1f2bfc77b45899073c225201c64a2c.zip
attempt to get tests working under python 2.6
Diffstat (limited to 'testsuite/common.py')
-rw-r--r--testsuite/common.py83
1 files changed, 79 insertions, 4 deletions
diff --git a/testsuite/common.py b/testsuite/common.py
index 006b45970..e0a27b2c7 100644
--- a/testsuite/common.py
+++ b/testsuite/common.py
@@ -1,8 +1,6 @@
import os
import unittest
-
-__all__ = ['call', 'datastore', 'Bcfg2TestCase', 'DBModelTestCase', 'syncdb',
- 'XI', 'XI_NAMESPACE']
+from functools import wraps
datastore = "/"
@@ -32,7 +30,84 @@ except ImportError:
return (args, kwargs)
-class Bcfg2TestCase(unittest.TestCase):
+if hasattr(unittest.TestCase, "assertItemsEqual"):
+ TestCase = unittest.TestCase
+else:
+ def assertion(predicate, default_msg=None):
+ @wraps(predicate)
+ def inner(*args, **kwargs):
+ if 'msg' in kwargs:
+ msg = kwargs['msg']
+ del kwargs['msg']
+ else:
+ msg = default_msg % args
+ assert predicate(*args, **kwargs), msg
+ return inner
+
+ class TestCase(unittest.TestCase):
+ # versions of TestCase before python 2.7 lacked a lot of the
+ # really handy convenience methods, so we provide them -- at
+ # least the easy ones and the ones we use.
+ assertIs = assertion(lambda a, b: a is b, "%s is not %s")
+ assertIsNot = assertion(lambda a, b: a is not b, "%s is %s")
+ assertIsNone = assertion(lambda x: x is None, "%s is not None")
+ assertIsNotNone = assertion(lambda x: x is not None, "%s is None")
+ assertIn = assertion(lambda a, b: a in b, "%s is not in %s")
+ assertNotIn = assertion(lambda a, b: a not in b, "%s is in %s")
+ assertIsInstance = assertion(isinstance, "%s is not %s")
+ assertNotIsInstance = assertion(lambda a, b: not isinstance(a, b),
+ "%s is %s")
+ assertGreater = assertion(lambda a, b: a > b,
+ "%s is not greater than %s")
+ assertGreaterEqual = assertion(lambda a, b: a >= b,
+ "%s is not greater than or equal to %s")
+ assertLess = assertion(lambda a, b: a < b, "%s is not less than %s")
+ assertLessEqual = assertion(lambda a, b: a <= b,
+ "%s is not less than or equal to %s")
+ assertItemsEqual = assertion(lambda a, b: sorted(a) == sorted(b),
+ "Items do not match:\n%s\n%s")
+
+if hasattr(unittest, "skip"):
+ can_skip = True
+ skip = unittest.skip
+ skipIf = unittest.skipIf
+ skipUnless = unittest.skipUnless
+else:
+ # we can't actually skip tests, we just make them pass
+ can_skip = False
+
+ def skip(msg):
+ def decorator(func):
+ @wraps(func)
+ def inner(*args, **kwargs):
+ pass
+ return inner
+ return decorator
+
+ def skipIf(condition, msg):
+ def decorator(func):
+ if condition:
+ return func
+
+ @wraps(func)
+ def inner(*args, **kwargs):
+ pass
+ return inner
+ return decorator
+
+ def skipUnless(condition, msg):
+ def decorator(func):
+ if not condition:
+ return func
+
+ @wraps(func)
+ def inner(*args, **kwargs):
+ pass
+ return inner
+ return decorator
+
+
+class Bcfg2TestCase(TestCase):
def assertXMLEqual(self, el1, el2, msg=None):
self.assertEqual(el1.tag, el2.tag, msg=msg)
self.assertEqual(el1.text, el2.text, msg=msg)