You should be using py.test
. I think the unittest module was blindly copied from JUnit. Anyway, you can hack your way like this:
import unittest
data = [
(2, True),
(3, False),
(4, True),
(5, False)]
# This should be imported from a separate module.
def isEven(number):
return True # Quite buggy implementation
def create_test_func(num, expected):
def _test_func(self):
self.assertEqual(expected, isEven(num))
return _test_func
class TestIsEven(unittest.TestCase):
pass
# pyunit isn't Pythonic enough. Use py.test instead
# till then we rely on such hackery
import new
for i, (num, expected) in enumerate(data):
setattr(TestIsEven, 'test_data_%d'%i, create_test_func(num, expected))
if __name__ == "__main__":
unittest.main()
And the output is:
.F.F
======================================================================
FAIL: test_data_1 (__main__.TestIsEven)
----------------------------------------------------------------------
Traceback (most recent call last):
File "untitled-1.py", line 15, in _test_func
self.assertEqual(expected, isEven(num))
AssertionError: False != True
======================================================================
FAIL: test_data_3 (__main__.TestIsEven)
----------------------------------------------------------------------
Traceback (most recent call last):
File "untitled-1.py", line 15, in _test_func
self.assertEqual(expected, isEven(num))
AssertionError: False != True
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures=2)
Using this approach, you can add more niceties like printing debugging information on failure, etc.