1
"""Implementation of the parametric test support for Python 3.x.
3
Thanks for the py3 version to Robert Collins, from the Testing in Python
7
#-----------------------------------------------------------------------------
8
# Copyright (C) 2009 The IPython Development Team
10
# Distributed under the terms of the BSD License. The full license is in
11
# the file COPYING, distributed as part of this software.
12
#-----------------------------------------------------------------------------
14
#-----------------------------------------------------------------------------
16
#-----------------------------------------------------------------------------
19
from unittest import TestSuite
21
#-----------------------------------------------------------------------------
22
# Classes and functions
23
#-----------------------------------------------------------------------------
26
def isgenerator(func):
27
return hasattr(func,'_generator')
30
class IterCallableSuite(TestSuite):
31
def __init__(self, iterator, adapter):
33
self._adapter = adapter
35
yield self._adapter(self._iter.__next__)
37
class ParametricTestCase(unittest.TestCase):
38
"""Write parametric tests in normal unittest testcase form.
40
Limitations: the last iteration misses printing out a newline when
41
running in verbose mode.
44
def run(self, result=None):
45
testMethod = getattr(self, self._testMethodName)
46
# For normal tests, we just call the base class and return that
47
if isgenerator(testMethod):
48
def adapter(next_test):
49
return unittest.FunctionTestCase(next_test,
53
return IterCallableSuite(testMethod(),adapter).run(result)
55
return super(ParametricTestCase, self).run(result)
59
"""Decorator to make a simple function into a normal test via
61
# Hack, until I figure out how to write isgenerator() for python3!!
62
func._generator = True
64
class Tester(ParametricTestCase):
65
test = staticmethod(func)
67
Tester.__name__ = func.__name__