-
-
Save ahlusar1989/8e2aefa00b5661efd6ff7855d7a0b117 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 unittest | |
def foo(x, y, z): | |
return (x != y and x != z or x and z) | |
def get_test_args(): | |
x = y = z = [True, False] | |
from itertools import product, repeat | |
# for each input arg ---> expected arg | |
input_args = list(product(x, y, z)) | |
expected_args = [ | |
True, | |
False, | |
True, | |
True, | |
True, | |
False, | |
True, | |
False] | |
test_names = list(range(1, len(expected_args) + 1)) | |
for test_name, i, e in zip(test_names, input_args, expected_args): | |
yield (str(test_name), i, e) | |
class TestFunctionFooMeta(type): | |
def __new__(cls, name, bases, attrs): | |
# factory method to create each test method | |
def create_func(test_name, input_args, expected_args): | |
def func(self): | |
result = foo(*input_args) # call the Function under test | |
self.check_common_invariants(result, expected_args) | |
func.__name__ = "test_" + str(test_name) | |
return func | |
# get_test_args yields (test_name, input_args, expected_value) | |
for test_name, input_args, expected_args in get_test_args(): | |
func = create_func(test_name, input_args, expected_args) | |
attrs[func.__name__] = func | |
return type.__new__(cls, name, bases, attrs) | |
class TestUnit(unittest.TestCase, metaclass=TestFunctionFooMeta): | |
def check_common_invariants(self, actual_value, expected_value): | |
self.assertEqual(actual_value, expected_value) | |
if __name__ == '__main__': | |
unittest.main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment