Skip to content

Instantly share code, notes, and snippets.

@codeguru42
Created December 14, 2016 03:11
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 codeguru42/d74bd0397bd33b3d8b84b949f6e37349 to your computer and use it in GitHub Desktop.
Save codeguru42/d74bd0397bd33b3d8b84b949f6e37349 to your computer and use it in GitHub Desktop.
import unittest
class MyTests(unittest.TestCase):
def __init__(self, x, y):
super(MyTests, self).__init__("testMyMethod")
self.x = x
self. y = y
def testMyMethod(self):
self.assertEqual(self.y, my_method(self.x))
def load_tests(loader, tests, pattern):
print("load_tests()")
foobar = zip(range(5), range(5)) # This is a list of 2-tuples
test_suite = unittest.TestSuite()
for x, y in foobar:
test_suite.addTest(MyTests(x, y))
return test_suite
def my_method(x):
return x
if __name__ == "__main__":
unittest.main()
myrdraal% python mytests.py
Traceback (most recent call last):
File "mytests.py", line 24, in <module>
unittest.main()
File "/usr/lib/python3.5/unittest/main.py", line 93, in __init__
self.parseArgs(argv)
File "/usr/lib/python3.5/unittest/main.py", line 140, in parseArgs
self.createTests()
File "/usr/lib/python3.5/unittest/main.py", line 144, in createTests
self.test = self.testLoader.loadTestsFromModule(self.module)
File "/usr/lib/python3.5/unittest/loader.py", line 123, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "/usr/lib/python3.5/unittest/loader.py", line 92, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "/usr/lib/python3.5/unittest/suite.py", line 24, in __init__
self.addTests(tests)
File "/usr/lib/python3.5/unittest/suite.py", line 57, in addTests
for test in tests:
TypeError: __init__() missing 1 required positional argument: 'y'
myrdraal% python parameterized.py
.....
----------------------------------------------------------------------
Ran 5 tests in 0.001s
OK
import unittest
class ParameterizedTest(unittest.TestCase):
def __init__(self, value):
super(ParameterizedTest, self).__init__("test")
self.value = value
def test(self):
self.assertEqual(self.value, self.value)
def load_tests(loader, tests, pattern):
test_suite = unittest.TestSuite()
for i in range(5):
test_suite.addTest(ParameterizedTest(i))
return test_suite
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment