Created
June 27, 2013 14:50
-
-
Save fcasco/5877055 to your computer and use it in GitHub Desktop.
Dynamic test generation (just because I keep forgeting how to do it).
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 unittest | |
class TestAutogenerated(unittest.TestCase): | |
values = [('a', 1), ('be', 20), ('cal', 3), ('dado', 2)] | |
@staticmethod | |
def make_test(s, n): | |
def new_test(self): | |
self.assertEqual(len(s), n) | |
return new_test | |
@classmethod | |
def add_tests(cls): | |
for v in cls.values: | |
setattr(cls, 'test_{}'.format(v[0]), cls.make_test(*v)) | |
TestAutogenerated.add_tests() | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment