views:

94

answers:

1

Hello I have test's using unittest. I have a test suite and I am trying to pass variables through into each of the tests. The below code shows the test suite used.

class suite():

    def suite(self): #Function stores all the modules to be tested

        modules_to_test = ('testmodule1', 'testmodule2')
        alltests = unittest.TestSuite()
        for module in map(__import__, modules_to_test):
            alltests.addTest(unittest.findTestCases(module))
        return alltests

It calls tests, I would like to know how to pass variables into the tests from this class. An example test script is below:

class TestThis(unittest.TestCase):
    def runTest(self):
        assertEqual('1', '1')

class TestThisTestSuite(unittest.TestSuite):

    # Tests to be tested by test suite
    def makeTestThisTestSuite():
        suite = unittest.TestSuite()
        suite.addTest("TestThis")
        return suite

    def suite():
        return unittest.makeSuite(TestThis)


if __name__ == '__main__':
    unittest.main()    

So from the class suite() I would like to enter in a value to change the value that is in assert value. Eg. assertEqual(self.value, '1'). I have tried sys.argv for unittest and it doesn't seem to work. Thanks for any help.

+1  A: 

Passing interactive values on the command line is not really in line with the intention of automated unit tests. How about using the ConfigParser library module and have your TestCase subclass's __init__ method load some variable test input data that way?

Of course, sticking with the code you have, what about either module global variables or class constant members to hold your test data? For example, after you import a test module by name, couldn't you just do

module.testVars = [1, 2, 3, "foo"]

And have the tests in the test modules reference that variable (which might be an empty list by default)?

Peter Lyons
@Peter Lyons - Thanks, that never even crossed my mind. Work's perfectly. +1
chrissygormley