Created
January 4, 2011 03:49
-
-
Save brantfaircloth/764363 to your computer and use it in GitHub Desktop.
dynamic tests generation that works in unittest and nose
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
# modified from http://bit.ly/dEyw03 | |
import unittest | |
class TestMyGoods(unittest.TestCase): | |
pass | |
def create_dynamic_method(pair): | |
"""just don't include `test` in the function name here, nose will try to | |
run it""" | |
def dynamic_test_method(self): | |
"""this function name doesn't matter much, it can start with `test`, | |
but we're going to rename it dynamically below""" | |
self.assertEqual(pair, pair) # this is our actual assertion | |
return dynamic_test_method | |
for k, pair in enumerate(xrange(12)): | |
dynamic_method = create_dynamic_method(pair) | |
dynamic_method.__name__ = 'test_{0}'.format(k) | |
dynamic_method.__doc__ = 'my super great name {0}'.format(k) | |
setattr(TestMyGoods, dynamic_method.__name__, dynamic_method) | |
# remove the last test name from the current namespace, | |
# so nose doesn't run it | |
del dynamic_method | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
now with more cowbell (
__doc__
attribute and xrange() over range())