Last active
December 26, 2015 14:29
-
-
Save renskiy/7165930 to your computer and use it in GitHub Desktop.
Decorator for unit test methods allowing parametrized execution of tests. Inspiring by PHPUnit's @dataProvider.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
class data_set: | |
""" | |
Class: data_set | |
Allows to combine args and kwargs in set of parameters. | |
""" | |
def __init__(self, *args, **kwargs): | |
self.args = args | |
self.kwargs = kwargs | |
def data_provider(*data_sets, **kw_data_sets): | |
""" | |
Decorator: data_provider | |
Allows to write parametrized tests. | |
Usage: | |
@data_provider( | |
(1, 2, 3), | |
{'a': 4, 'b': 5, 'c': 6}, | |
ds789=data_set(7, 8, c=9) | |
# more parameter sets... | |
) | |
def some_test(self, a, b, c): | |
# testing code... | |
""" | |
def _decorator(test_method): | |
def _executor(test_case): | |
for ds in itertools.chain(data_sets, kw_data_sets.itervalues()): | |
test_case.setUp() | |
try: | |
if isinstance(ds, dict): | |
test_method(test_case, **ds) | |
elif isinstance(ds, data_set): | |
test_method(test_case, *ds.args, **ds.kwargs) | |
else: | |
test_method(test_case, *ds) | |
finally: | |
test_case.tearDown() | |
return _executor | |
return _decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment