pyUnit and dynamic test functions

Sakcee sakcee at gmail.com
Wed Dec 21 18:40:01 EST 2005


Thanks for reply,  here is the script with trucated datafile



The data file is following ,

--- data file--------
getUSStates~~dict~
{AK: Alaska,
 AL: Alabama,
 AR: Arkansas,
 AZ: Arizona,
 CA: California,
 CO: Colorado,
 ....
 ....
 WY: Wyoming }
----data file--------

I want to test function:getUSStates that takes input none and outputs a
dict
of us states.the first line shows function
name~input~output_format~expected_output

Now I have following class and loadTestInfoFromFile function for
loading the values for file and assinging the data to self.outputOfFunc
etc



class getUsStates( unittest.TestCase ):


    func_name = None
    inputToFunc = None
    outputOfFunc = None
    outputType = None

    setup = None
    testUsStates = None
    testLength = None

    def loadTestInfoFromFile(self):
        """ read function name, input to function, output format
        and output from file"""

        rest = file('data.txt').read()
        self.func_name , self.inputToFunc,
self.outputType,outputOfFuncRaw = rest.split("~")

        if self.outputType == 'dict':
            outputOfFuncRaw = outputOfFuncRaw.split(",")
            self.outputOfFunc = {}

            for i in range(0,len(outputOfFuncRaw)):
                li= ( (outputOfFuncRaw[i].strip()).replace('}','')
).split(':')
                self.outputOfFunc[li[0]]=li[1]



    def buildTestFunctions( self, returnedVals, func_name, inp ):


        self.testReturnVal =  lambda  : self.assertEqual( inp,
returnedVals )
        self.testLength    =  lambda  : self.assertEqual(
len(returnedVals) , len(inp) )


def runFunction():
    g = getUsStatesTest()
    g.loadTestInfoFromFile()
    g.buildFunctions(g.returnedVals,g.func_name,g.outputOfFunc)


if __name__ == "__main__":
    unittest.main(defaultTest="runFunction")


Now in the buildTestFunctions() I want to setup 2 unittest test
function that do assertEqual
so that I have 2 functions which are dynamically created from file ,
now I want to associate
these function with unittest and run them.

the above runs correctly reads the data and make 2 funcitons
testReturnVal and testLength
but then I want to associate those funcitons to unittest. and run unit
test but that part i dont
know how to do.

in this way I can define 100 functions with inputs and expected outputs
in a data file
and this type of script will generate similar assert functions for all
of them on fly
and run them

thanks for any input or any alternate approach to it


Kent Johnson wrote:
> Sakcee wrote:
> > Hi
> >
> > I am trying to use pyUnit to create a framework for testing functions
> >
> > I am reading function name, expected output,  from a text file.
>
> Can you show a sample of what the text file might look like, and what tests you want to
> generate from it?
>
> > and in python generating dynamic test functions
> > something like this
> >
> >
> > class getUsStatesTest( unittest.TestCase ):
> >
> >     self.setUp =  lambda  : function_call
> >     self.testReturnVal =  lambda  : self.assertEqual( fileInput,
> > function_returnedVals )
> >     self.testLength    =  lambda  : self.assertEqual( len(returnedVals)
> > , len(inp) )
>
> I don't understand the above at all. What do function_call, function_returnedVals,
> returnedVals and inp refer to? What is the point of the lambdas and the assignment to
> attributes of (undefined) self?
> 
> Kent




More information about the Python-list mailing list