Unittest and dynamically created methods

Discussion in 'Python' started by JAWS, Oct 14, 2003.

  1. JAWS

    JAWS Guest

    I get this error message when trying to run a unittest test with a
    dynamically created test method:

    Traceback (most recent call last):
    File "unittest.py", line 215, in __call__
    testMethod()
    TypeError: ?() takes no arguments (1 given)


    I have no clue as to where the 1 given argument comes from...
    I am using python 2.2 and here is a copy of the code generating this:

    #! /bin/env python

    import unittest, commands, new

    class Test(unittest.TestCase):
    done = None

    def initialization(self):
    Test.port = 11000
    Test.host = 'meadow'

    def setUp(self):
    if not Test.done:
    Test.done = 1
    Test.initialization(self)

    def tearDown(self):
    pass

    def testCommandFailure(self):
    status, output = commands.getstatusoutput('python
    .../bin/uimClient.py' +
    ' -p ' + str(Test.port) + ' -h ' + Test.host)
    self.assertEqual(256, status)

    def testCommandFailure3(self):
    status, output = commands.getstatusoutput('python
    .../bin/uimClient.py' +
    ' -p ' + str(Test.port) + ' -h ' + Test.host)
    self.assertEqual(256, status)

    #====================================BASE
    TEST==================================

    if __name__ == '__main__':

    base = 'def testCommandFailure2(self):\n\t""" Testing test1 method
    """' +\
    '\n\tstatus, output = commands.getstatusoutput("python " +' + \
    '" ../bin/uimClient.py -p " + str(Test.port) + " -h " +
    Test.host)'+\
    '\n\tself.assertEqual(status, 256)\n'
    code = compile(base, 'uimClientFT.py', 'exec')
    testf = new.function(code, Test.__dict__, 'testCommandFailure2')
    setattr(Test, 'testCommandFailure2', testf)

    print Test.__dict__
    print type(Test.testCommandFailure2)

    unittest.main()



    J-P
    thankx
    JAWS, Oct 14, 2003
    #1
    1. Advertising

  2. JAWS

    Peter Hansen Guest

    JAWS wrote:
    >
    > I get this error message when trying to run a unittest test with a dynamically created test method:
    >
    > Traceback (most recent call last):
    > File "unittest.py", line 215, in __call__
    > testMethod()
    > TypeError: ?() takes no arguments (1 given)


    I can't say offhand, but the above error might give you a hint:
    it appears it's trying to report the name of the method, but
    doesn't have it (thus the ?() part). Maybe you should examine
    the dynamic creation of the method more closely. Have you
    passed the name parameter in the right place?

    -Peter
    Peter Hansen, Oct 14, 2003
    #2
    1. Advertising

  3. JAWS

    Duncan Booth Guest

    JAWS <> wrote five times with HTML attachments:

    > I get this error message when trying to run a unittest test with a
    > dynamically created test method:
    >
    > Traceback (most recent call last):
    > File "unittest.py", line 215, in __call__
    > testMethod()
    > TypeError: ?() takes no arguments (1 given)
    >
    >

    <snip>
    > base = 'def testCommandFailure2(self):\n\t""" Testing test1 method
    > """' +\
    > '\n\tstatus, output = commands.getstatusoutput("python " +' + \
    > '" ../bin/uimClient.py -p " + str(Test.port) + " -h " +
    > Test.host)'+\
    > '\n\tself.assertEqual(status, 256)\n'
    > code = compile(base, 'uimClientFT.py', 'exec')
    > testf = new.function(code, Test.__dict__, 'testCommandFailure2')
    > setattr(Test, 'testCommandFailure2', testf)
    >

    Try adding:
    import dis
    dis.dis(code)
    and it should become obvious. Effectively your code is the same as:

    def testf():
    def testCommandFailure2(self):
    ... whatever ...
    Test.testCommandFailure2 = testf

    so when Test.testCommandFailure2 is called it gets passed a self parameter
    which it wasn't expecting.

    In short, compiling code which contains a def statement doesn't execute the
    def statement until you execute the code.

    P.S. I don't know what you are trying to do, but I expect you can achieve
    whatever it is you are trying to do more cleanly by not compiling any code
    on the fly.

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
    Duncan Booth, Oct 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JAWS
    Replies:
    0
    Views:
    241
  2. JAWS
    Replies:
    0
    Views:
    265
  3. JAWS
    Replies:
    0
    Views:
    314
  4. JAWS
    Replies:
    0
    Views:
    232
  5. msimmons
    Replies:
    0
    Views:
    472
    msimmons
    Jul 16, 2009
Loading...

Share This Page