Take the 2-minute tour ×

So, I am trying to create a unittest.TestCase that creates several different databases with different member types and verifies something about the data(not important for this context). I wanted to use a generator to pass the necessary information from a global dict to the function which will take care of the object creation and verification.

Currently, it will only setUp, run, and tearDown the first variant of the test. How can I generate several distinct (i.e. varying parameters from a data structure such as a dict) calls to my method, which has to determine how to populate the database based on the member type? Each call should be run as a separate test and have the setUp and tearDown performed every time. The test case is being run with PyUnit in the Pydev GUI.

import sys, os, socket, shutil, unittest

# Global counter for test generation
testRun = 1

# test dictionary for generating multiple tests
basicMembers = {"Int" : 1}

# test operations to generate federations
def addItemAndGetSizeOf(basicMember, expectedValue, testFileName):
    print "foo %s %s %s" % (basicMember, expectedValue, testFileName) 

class BasicMembers(unittest.TestCase):
    testFileName = "bar" + str(testRun)

    def setUp(self):
        global testRun
        testRun += 1
        print testRun      

    def tearDown(self):

    def testBasicMembers(self):
        for basicMember, expectedValue in basicMembers.items():
            yield addItemAndGetSizeOf, basicMember, expectedValue, self.testFileName

if __name__ == "__main__":
share|improve this question
testBasicMembers is going to return a generator, not a function. Read more about generators here: docs.python.org/tutorial/classes.html#generators Can you provide the least amount of code that reproduces the problem and what is the expected output? –  Mahmoud Abdelkader Mar 14 '11 at 8:14
Yes, I understand that it will return a generator that will hopefully run the "addItemAndGetSizeOf" function with the specified arguments. I believe my misunderstanding is in the unittest framework. I thought it would implicitly run through the generator, as it recognizes the "testBasicMember" as a test function. I know I am missing the point of this example: somethingaboutorange.com/mrl/projects/nose/1.0.0/… –  DbTesting101 Mar 14 '11 at 9:11
That's only a feature of the nose testing framework. Python's unittest does not support this. Are you asking about nose or docs.python.org/library/unittest.html? What version of Python are you using? –  Mahmoud Abdelkader Mar 14 '11 at 9:16
I am using Pydev's unittest in Eclipse, PyUnit. I was confused about the nose framework and assumed that it was part of PyUnit. Obviously, this assumption is turning out to be wrong. Is there a way to generate this function call for all items in a given dict data structure and let the unittest framework run it for me? –  DbTesting101 Mar 14 '11 at 9:26
Please edit your question if that's the question you would like to have answered :) However, do you want num-of-basicMembers-keys different tests or should one test suffice? –  Mahmoud Abdelkader Mar 14 '11 at 9:29
show 1 more comment


Your Answer



Browse other questions tagged or ask your own question.