1
"""Implementation of the parametric test support for Python 2.x
3
#-----------------------------------------------------------------------------
5
#-----------------------------------------------------------------------------
9
from compiler.consts import CO_GENERATOR
11
#-----------------------------------------------------------------------------
12
# Classes and functions
13
#-----------------------------------------------------------------------------
15
def isgenerator(func):
17
return func.func_code.co_flags & CO_GENERATOR != 0
18
except AttributeError:
21
class ParametricTestCase(unittest.TestCase):
22
"""Write parametric tests in normal unittest testcase form.
24
Limitations: the last iteration misses printing out a newline when running
27
def run_parametric(self, result, testMethod):
28
# But if we have a test generator, we iterate it ourselves
29
testgen = testMethod()
33
result.startTest(self)
38
except KeyboardInterrupt:
41
result.addError(self, self._exc_info())
51
except self.failureException:
52
result.addFailure(self, self._exc_info())
53
except KeyboardInterrupt:
56
result.addError(self, self._exc_info())
60
except KeyboardInterrupt:
63
result.addError(self, self._exc_info())
65
if ok: result.addSuccess(self)
70
def run(self, result=None):
72
result = self.defaultTestResult()
73
testMethod = getattr(self, self._testMethodName)
74
# For normal tests, we just call the base class and return that
75
if isgenerator(testMethod):
76
return self.run_parametric(result, testMethod)
78
return super(ParametricTestCase, self).run(result)
82
"""Decorator to make a simple function into a normal test via unittest."""
84
class Tester(ParametricTestCase):
85
test = staticmethod(func)
87
Tester.__name__ = func.__name__