Adding decorator requires(ModuleName) for tests
This commit is contained in:
parent
9e6100816e
commit
859cec5632
5 changed files with 88 additions and 15 deletions
43
tests/util/helper/decorators.py
Normal file
43
tests/util/helper/decorators.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
|
||||
'''Decorators for skipping test methods and test cases'''
|
||||
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
|
||||
class requires(object):
|
||||
'''Skip if the given module is not found
|
||||
|
||||
Usage:
|
||||
|
||||
@requires('RequiredModule')
|
||||
class MyTestCase(unittest.TestCase):
|
||||
...
|
||||
'''
|
||||
|
||||
def __init__(self, *args):
|
||||
'''Setup this decorator. Args should be a list of
|
||||
module names'''
|
||||
self.skip = False
|
||||
|
||||
for module in args:
|
||||
try:
|
||||
__import__(module)
|
||||
except ImportError:
|
||||
self.skip = True
|
||||
self.skipped = module
|
||||
break
|
||||
|
||||
def __call__(self, klass):
|
||||
'''Replace a skipped class with a dummy testCase'''
|
||||
if not self.skip:
|
||||
return klass
|
||||
|
||||
logging.warning('Module %s not found. Skipping %s' % (self.skipped,
|
||||
klass.__name__))
|
||||
|
||||
class SkipWrapper(unittest.TestCase):
|
||||
'''Dummy wrapper'''
|
||||
pass
|
||||
|
||||
return SkipWrapper
|
||||
Loading…
Add table
Add a link
Reference in a new issue