pyUnit and dynamic test functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sakcee

    #1

    pyUnit and dynamic test functions

    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.
    and in python generating dynamic test functions
    something like this


    class getUsStatesTest ( unittest.TestCa se ):

    self.setUp = lambda : function_call
    self.testReturn Val = lambda : self.assertEqua l( fileInput,
    function_return edVals )
    self.testLength = lambda : self.assertEqua l( len(returnedVal s)
    , len(inp) )



    is it possible to load these dynamic functions via pyUnit, instead of
    defining in text form ,
    can i use loadfromname etc to load these dynamic test functions.

    something like this

    unittest.TestSu ite(unittest.de faultTestLoader .loadTestsFromN ame(
    getUsStatesTest () ))

    in this way I can completely automate every function that returns some
    output by dynamically generating its
    test functions

    thanks

  • Sakcee

    #2
    Re: pyUnit and dynamic test functions


    Please anyone help, is this kind of dynamin thing possible? any
    suggestion for creating frameworks for automated testing ,

    all comments appreciated

    thanks


    Sakcee wrote:[color=blue]
    > 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.
    > and in python generating dynamic test functions
    > something like this
    >
    >
    > class getUsStatesTest ( unittest.TestCa se ):
    >
    > self.setUp = lambda : function_call
    > self.testReturn Val = lambda : self.assertEqua l( fileInput,
    > function_return edVals )
    > self.testLength = lambda : self.assertEqua l( len(returnedVal s)
    > , len(inp) )
    >
    >
    >
    > is it possible to load these dynamic functions via pyUnit, instead of
    > defining in text form ,
    > can i use loadfromname etc to load these dynamic test functions.
    >
    > something like this
    >
    > unittest.TestSu ite(unittest.de faultTestLoader .loadTestsFromN ame(
    > getUsStatesTest () ))
    >
    > in this way I can completely automate every function that returns some
    > output by dynamically generating its
    > test functions
    >
    > thanks[/color]

    Comment

    • Kent Johnson

      #3
      Re: pyUnit and dynamic test functions

      Sakcee wrote:[color=blue]
      > 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.[/color]

      Can you show a sample of what the text file might look like, and what tests you want to
      generate from it?
      [color=blue]
      > and in python generating dynamic test functions
      > something like this
      >
      >
      > class getUsStatesTest ( unittest.TestCa se ):
      >
      > self.setUp = lambda : function_call
      > self.testReturn Val = lambda : self.assertEqua l( fileInput,
      > function_return edVals )
      > self.testLength = lambda : self.assertEqua l( len(returnedVal s)
      > , len(inp) )[/color]

      I don't understand the above at all. What do function_call, function_return edVals,
      returnedVals and inp refer to? What is the point of the lambdas and the assignment to
      attributes of (undefined) self?

      Kent

      Comment

      • Sakcee

        #4
        Re: pyUnit and dynamic test functions

        Thanks for reply, here is the script with trucated datafile



        The data file is following ,

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

        I want to test function:getUSS tates that takes input none and outputs a
        dict
        of us states.the first line shows function
        name~input~outp ut_format~expec ted_output

        Now I have following class and loadTestInfoFro mFile function for
        loading the values for file and assinging the data to self.outputOfFu nc
        etc



        class getUsStates( unittest.TestCa se ):


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

        setup = None
        testUsStates = None
        testLength = None

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

        rest = file('data.txt' ).read()
        self.func_name , self.inputToFun c,
        self.outputType ,outputOfFuncRa w = rest.split("~")

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

        for i in range(0,len(out putOfFuncRaw)):
        li= ( (outputOfFuncRa w[i].strip()).repla ce('}','')
        ).split(':')
        self.outputOfFu nc[li[0]]=li[1]



        def buildTestFuncti ons( self, returnedVals, func_name, inp ):


        self.testReturn Val = lambda : self.assertEqua l( inp,
        returnedVals )
        self.testLength = lambda : self.assertEqua l(
        len(returnedVal s) , len(inp) )


        def runFunction():
        g = getUsStatesTest ()
        g.loadTestInfoF romFile()
        g.buildFunction s(g.returnedVal s,g.func_name,g .outputOfFunc)


        if __name__ == "__main__":
        unittest.main(d efaultTest="run Function")


        Now in the buildTestFuncti ons() 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:[color=blue]
        > Sakcee wrote:[color=green]
        > > 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.[/color]
        >
        > Can you show a sample of what the text file might look like, and what tests you want to
        > generate from it?
        >[color=green]
        > > and in python generating dynamic test functions
        > > something like this
        > >
        > >
        > > class getUsStatesTest ( unittest.TestCa se ):
        > >
        > > self.setUp = lambda : function_call
        > > self.testReturn Val = lambda : self.assertEqua l( fileInput,
        > > function_return edVals )
        > > self.testLength = lambda : self.assertEqua l( len(returnedVal s)
        > > , len(inp) )[/color]
        >
        > I don't understand the above at all. What do function_call, function_return edVals,
        > returnedVals and inp refer to? What is the point of the lambdas and the assignment to
        > attributes of (undefined) self?
        >
        > Kent[/color]

        Comment

        • Fabrizio Milo

          #5
          Re: pyUnit and dynamic test functions

          > thanks for any input or any alternate approach to it

          I think that your approach is not fair.

          You should create a TestCase for each of your data input, add it to a TestSuite
          and run the test suite.

          Here is a stub for loading just a 'dict' type, hoping it is helpful


          import unittest
          import glob
          from string import split,strip

          def getUSStates(*ar gs):
          #Fake
          return {'AK:': 'Alaska', 'CA:': 'California', 'AR:': 'Arkansas',
          'CO:': 'Colorado', 'WY:': 'Wyoming', 'AZ:': 'Arizona', 'AL:':
          'Alabama'}

          class TestStubExcepti on( Exception ):
          '''
          Test case creation failed.
          '''

          class TestStub( unittest.TestCa se ):

          def __init__( self, fname, farg, t_out, out ):
          unittest.TestCa se.__init__(sel f,'testRun')
          self.fname = eval(fname,glob als())
          self.input = farg
          self.fparse = getattr(self,'l oad_%s' % t_out)
          self.output = self.fparse( out )

          def load_dict(self, data):
          assert data[0] is '{', 'Wrong dict format %s' % data
          assert data[-1] is '}', 'Wrong dict format %s' % data
          items = data[1:-1].split(',')
          return dict( map( split, map( strip, items ) ) )

          def testRun(self):
          self.assertEqua ls( self.fname(self .input), self.output )


          def build_tests( filename ):
          try:
          fd = open( filename, 'r')
          tc = TestStub( *fd.read().spli t("~") )
          del fd
          return tc
          except:
          import traceback; traceback.print _exc()
          raise TestStubExcepti on( 'Failed creating test case from file
          : %s'%filename )

          if __name__== '__main__':

          tc_data = glob.glob('*.tx t') # all text files with data

          ts = unittest.TestSu ite()

          for tc_file in tc_data:
          ts.addTest( build_tests( tc_file ) )

          unittest.TextTe stRunner().run( ts)


          Fabrizio Milo aka Misto

          Comment

          • Sakcee

            #6
            Re: pyUnit and dynamic test functions

            Excellent , your example is elegant and runs beautifully

            so the idea is to create a testcase which runs a general function
            "testRun" (which calls the function defined in file )and loads data
            based on type ,

            add this testcase to suite and run suite.

            thanks , this is exactly what I was looking for.

            Fabrizio Milo wrote:[color=blue][color=green]
            > > thanks for any input or any alternate approach to it[/color]
            >
            > I think that your approach is not fair.
            >
            > You should create a TestCase for each of your data input, add it to a TestSuite
            > and run the test suite.
            >
            > Here is a stub for loading just a 'dict' type, hoping it is helpful
            >
            >
            > import unittest
            > import glob
            > from string import split,strip
            >
            > def getUSStates(*ar gs):
            > #Fake
            > return {'AK:': 'Alaska', 'CA:': 'California', 'AR:': 'Arkansas',
            > 'CO:': 'Colorado', 'WY:': 'Wyoming', 'AZ:': 'Arizona', 'AL:':
            > 'Alabama'}
            >
            > class TestStubExcepti on( Exception ):
            > '''
            > Test case creation failed.
            > '''
            >
            > class TestStub( unittest.TestCa se ):
            >
            > def __init__( self, fname, farg, t_out, out ):
            > unittest.TestCa se.__init__(sel f,'testRun')
            > self.fname = eval(fname,glob als())
            > self.input = farg
            > self.fparse = getattr(self,'l oad_%s' % t_out)
            > self.output = self.fparse( out )
            >
            > def load_dict(self, data):
            > assert data[0] is '{', 'Wrong dict format %s' % data
            > assert data[-1] is '}', 'Wrong dict format %s' % data
            > items = data[1:-1].split(',')
            > return dict( map( split, map( strip, items ) ) )
            >
            > def testRun(self):
            > self.assertEqua ls( self.fname(self .input), self.output )
            >
            >
            > def build_tests( filename ):
            > try:
            > fd = open( filename, 'r')
            > tc = TestStub( *fd.read().spli t("~") )
            > del fd
            > return tc
            > except:
            > import traceback; traceback.print _exc()
            > raise TestStubExcepti on( 'Failed creating test case from file
            > : %s'%filename )
            >
            > if __name__== '__main__':
            >
            > tc_data = glob.glob('*.tx t') # all text files with data
            >
            > ts = unittest.TestSu ite()
            >
            > for tc_file in tc_data:
            > ts.addTest( build_tests( tc_file ) )
            >
            > unittest.TextTe stRunner().run( ts)
            >
            >
            > Fabrizio Milo aka Misto[/color]

            Comment

            Working...