473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unittest and dynamically created methods

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

Jul 18 '05 #1
2 2891
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
Jul 18 '05 #2
JAWS <ja**@ericsson.ca> 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 du****@rcp.co.uk
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?
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Zunbeltz Izaola | last post by:
Hi, I am using unittest for the first time. I have read chapter 7 of dive into pyhton (Unit Test). I have the following code from spacegroup import * import pygroups.misc.matrix as matrix...
3
by: Jan Decaluwe | last post by:
I'm working on a unit test for a finite state machine (FSM). The FSM behavior is specified in a dictionary called transitionTable. It has a key per state with a tuple of possible transitions as...
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
1
by: Tom Haddon | last post by:
Hi Folks, Newbie question here. I'm trying to set up some unit testing for a database abstraction class, and the first thing I want to test is the connection parameters. So, my question is, how do...
41
by: Roy Smith | last post by:
I've used the standard unittest (pyunit) module on a few projects in the past and have always thought it basicly worked fine but was just a little too complicated for what it did. I'm starting a...
7
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good...
5
by: paul kölle | last post by:
hi all, I noticed that setUp() and tearDown() is run before and after *earch* test* method in my TestCase subclasses. I'd like to run them *once* for each TestCase subclass. How do I do that. ...
3
by: David Vincent | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hello I'm hoping to get some insight into a situation that seems odd to me. My Python experience is limited; I've just started using the unittest module....
2
by: Collin Winter | last post by:
While working on a test suite for unittest these past few weeks, I've run across some behaviours that, while not obviously wrong, don't strike me as quite right, either. Submitted for your...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.