Skip to content

Instantly share code, notes, and snippets.

@fcasco
Created June 27, 2013 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcasco/5877055 to your computer and use it in GitHub Desktop.
Save fcasco/5877055 to your computer and use it in GitHub Desktop.
Dynamic test generation (just because I keep forgeting how to do it).
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