Skip to content

Instantly share code, notes, and snippets.

@lprsd
Created January 2, 2012 13:54
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 lprsd/1550761 to your computer and use it in GitHub Desktop.
Save lprsd/1550761 to your computer and use it in GitHub Desktop.
Create Test Cases Dynamically, in Python.
import unittest2 as unittest
def get_verify_mtd(sum_tuple, sum_value):
def test1(self):
self.assertEqual(sum(sum_tuple),sum_value)
return test1
def create_test_case_class():
sumlist = range(20,30,2)
sum1_list = [20]*5
sum2_list = range(0,10,2)
sum3_list = zip(sum1_list,sum2_list)
input_list = zip(sum3_list,sumlist)
mtd_dict = {}
for element in input_list:
mtd_dict['test_%s'%element[1]] = get_verify_mtd(*element)
print mtd_dict
return type('TestSums',(unittest.TestCase,),mtd_dict)
def load_tests(loader, tests, pattern):
suite = unittest.TestSuite()
case1 = loader.loadTestsFromTestCase(create_test_case_class())
suite.addTests(case1)
return suite
@lprsd
Copy link
Author

lprsd commented Jan 2, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment