473,756 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.p y", 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.T estCase):
done = None

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

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

def tearDown(self):
pass

def testCommandFail ure(self):
status, output = commands.getsta tusoutput('pyth on
.../bin/uimClient.py' +
' -p ' + str(Test.port) + ' -h ' + Test.host)
self.assertEqua l(256, status)

def testCommandFail ure3(self):
status, output = commands.getsta tusoutput('pyth on
.../bin/uimClient.py' +
' -p ' + str(Test.port) + ' -h ' + Test.host)
self.assertEqua l(256, status)

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

if __name__ == '__main__':

base = 'def testCommandFail ure2(self):\n\t """ Testing test1 method
"""' +\
'\n\tstatus, output = commands.getsta tusoutput("pyth on " +' + \
'" ../bin/uimClient.py -p " + str(Test.port) + " -h " +
Test.host)'+\
'\n\tself.asser tEqual(status, 256)\n'
code = compile(base, 'uimClientFT.py ', 'exec')
testf = new.function(co de, Test.__dict__, 'testCommandFai lure2')
setattr(Test, 'testCommandFai lure2', testf)

print Test.__dict__
print type(Test.testC ommandFailure2)

unittest.main()

J-P
thankx

Jul 18 '05 #1
2 2918
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.p y", 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.p y", line 215, in __call__
testMethod()
TypeError: ?() takes no arguments (1 given)

<snip> base = 'def testCommandFail ure2(self):\n\t """ Testing test1 method
"""' +\
'\n\tstatus, output = commands.getsta tusoutput("pyth on " +' + \
'" ../bin/uimClient.py -p " + str(Test.port) + " -h " +
Test.host)'+\
'\n\tself.asser tEqual(status, 256)\n'
code = compile(base, 'uimClientFT.py ', 'exec')
testf = new.function(co de, Test.__dict__, 'testCommandFai lure2')
setattr(Test, 'testCommandFai lure2', testf)

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

def testf():
def testCommandFail ure2(self):
... whatever ...
Test.testComman dFailure2 = testf

so when Test.testComman dFailure2 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.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\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
1768
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 import unittest class KnowValues(unittest.TestCase):
3
1986
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 the corresponding value. A transition is defined as a number of input values, a next state, and a documentation string. I want to test each possible transition in a separate test method. For a particular transition, a test method could look as...
0
2054
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 TestCase.skipIf(expr, msg): skips if expr is true These can be called either in setUp() or in the test methods. I also added reporting of skipped tests to TestResult, _TextTestResult and
1
1877
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 I dynamically pass the variables from a list, for example to the unittest module so I can maintain the list of test cases more easily: ------------------------- import DB import unittest class ConnectString(unittest.TestCase):
41
10291
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 new project now and I'm thinking of trying py.test (http://codespeak.net/py/current/doc/test.html). It looks pretty cool from the docs. Is there anybody out there who has used both packages and can give a comparative review?
7
2073
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 old lines at the end: if __name__ == "__main__": unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a...
5
3576
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. thanks paul
3
3435
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. I've had some experience with unit test support in other languages.
2
1622
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 consideration: 1) TestCase.tearDown() is only run if TestCase.setUp() succeeded. It seems to me that tearDown() should always be run, regardless of any failures in setUp() or the test method itself. The case I'm considering is something like this,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.