summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabrice Douchant <Fabrice.Douchant@logilab.fr>2008-10-13 12:38:30 +0200
committerFabrice Douchant <Fabrice.Douchant@logilab.fr>2008-10-13 12:38:30 +0200
commitab9434f0b350b8ca9da6c40e17f12f0a7eba2e9e (patch)
tree0fb2906d0b71ac7cf80ff7514af8536b9c1c55da
parentdbc6734cbc23730c7fd3f46f7994d5950d14c1fb (diff)
downloadlogilab-common-ab9434f0b350b8ca9da6c40e17f12f0a7eba2e9e.tar.gz
add @require_module('module') that skip a test if the module can not be imported
-rw-r--r--decorators.py15
-rw-r--r--test/unittest_decorators.py45
2 files changed, 52 insertions, 8 deletions
diff --git a/decorators.py b/decorators.py
index 7ed4554..71cd325 100644
--- a/decorators.py
+++ b/decorators.py
@@ -167,3 +167,18 @@ def require_version(version):
return f
return check_require_version
+def require_module(module):
+ """ Check if the given module is loaded. Skip the test if not.
+ """
+ def check_require_module(f):
+ try:
+ __import__(module)
+ #print module, 'imported'
+ return f
+ except ImportError:
+ #print module, 'can not be imported'
+ def new_f(self, *args, **kwargs):
+ self.skip('%s can not be imported.' % module)
+ new_f.__name__ = f.__name__
+ return new_f
+ return check_require_module
diff --git a/test/unittest_decorators.py b/test/unittest_decorators.py
index 4a8b588..855f242 100644
--- a/test/unittest_decorators.py
+++ b/test/unittest_decorators.py
@@ -1,6 +1,6 @@
import sys
-from logilab.common.testlib import TestCase, unittest_main
-from logilab.common.decorators import require_version
+from logilab.common.testlib import TestCase, unittest_main, TestSuite
+from logilab.common.decorators import require_version, require_module
class DecoratorsTC(TestCase):
def test_require_version_good(self):
@@ -13,9 +13,9 @@ class DecoratorsTC(TestCase):
compare = ('2.4', '2.5', '2.5.4', '2.5.5')
for version in compare:
decorator = require_version(version)
- self.assertEquals(func, decorator(func), '%s =< %s : function return by \
- the decorator should be the same.' % (version,
- '.'.join([str(element) for element in current])))
+ self.assertEquals(func, decorator(func), '%s =< %s : function \
+ return by the decorator should be the same.' % (version,
+ '.'.join([str(element) for element in current])))
def test_require_version_bad(self):
""" should return a different function : skipping test
@@ -27,9 +27,9 @@ class DecoratorsTC(TestCase):
compare = ('2.5.6', '2.6', '2.6.5')
for version in compare:
decorator = require_version(version)
- self.assertNotEquals(func, decorator(func), '%s >= %s : function return by \
- the decorator should NOT be the same.' % ('.'.join([str(element) for
- element in current]), version))
+ self.assertNotEquals(func, decorator(func), '%s >= %s : function \
+ return by the decorator should NOT be the same.'
+ % ('.'.join([str(element) for element in current]), version))
def test_require_version_exception(self):
""" should throw a ValueError exception
@@ -41,5 +41,34 @@ class DecoratorsTC(TestCase):
decorator = require_version(version)
self.assertRaises(ValueError, decorator, func)
+ def test_require_module_good(self):
+ """ should return the same function
+ """
+ def func() :
+ pass
+ module = 'sys'
+ decorator = require_module(module)
+ self.assertEquals(func, decorator(func), 'module %s exists : function \
+ return by the decorator should be the same.' % module)
+
+ def test_require_module_bad(self):
+ """ should return a different function : skipping test
+ """
+ def func() :
+ pass
+ modules = ('bla', 'blo', 'bli')
+ for module in modules:
+ try:
+ __import__(module)
+ pass
+ except ImportError:
+ decorator = require_module(module)
+ self.assertNotEquals(func, decorator(func), 'module %s does \
+ not exist : function return by the decorator should \
+ NOT be the same.' % module)
+ return
+ print 'all modules in %s exist. Could not test %s' % (', '.join(modules),
+ sys._getframe().f_code.co_name)
+
if __name__ == '__main__':
unittest_main()