1

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):
        pass

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

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
  • 1
    testBasicMembers is going to return a generator, not a function. Read more about generators here: http://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 08: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: http://somethingaboutorange.com/mrl/projects/nose/1.0.0/writing_tests.html – DbTesting101 Mar 14 '11 at 09:11
  • That's only a feature of the __nose__ testing framework. Python's unittest does not support this. Are you asking about nose or http://docs.python.org/library/unittest.html? What version of Python are you using? – Mahmoud Abdelkader Mar 14 '11 at 09: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 09: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 09:29
  • http://stackoverflow.com/questions/1193909/pythons-unittest-and-dynamic-creation-of-test-cases and http://stackoverflow.com/questions/2798956/python-unittest-generate-multiple-tests-programmatically/4455312#4455312 should answer your question – Mahmoud Abdelkader Mar 14 '11 at 10:26

0 Answers0