Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unittest.TestCase and functools.partial don't seem to mix

Reply
Thread Tools

unittest.TestCase and functools.partial don't seem to mix

 
 
Joel Smith
Guest
Posts: n/a
 
      10-06-2009
Hi List,
I want to make some test case classes that can have some data passed in
to modify the way they behave. I can't see a straightforward manner to
pass data to an __init__() method of a class derived from
unittest.TestCase, or to pass data to a test function within that
class. Being a C++ guy, I thought "If only Python had something
equivalent to boost::bind, I'd be fine," and then I discovered
functools.partial. I found a post showing how to create classes using
partial, and I thought I was good to go. The post I found is here:

http://mail.python.org/pipermail/ban...er/000974.html

So I adapted that code to see if it worked in the context of unittest.
When I run the code, it acts like the parameter I provided with partial
isn't even there. Any ideas?

#!/usr/bin/python

import functools
import unittest

class GenericWindow:
def __init__(self, name, width, height, color='white'):
print('Name: %s, width: %d, height: %d, color: %s' % (name, width,
height, color))

class TestGenericWindow(unittest.TestCase):
def __init__(self, methodName, color):
unittest.TestCase.__init__(self, methodName)
print('color: %s' % color)
self.color = color

def testit():
GenericWindow('foo', width=100, height=100, color=self.color)

def suite():
s = unittest.Suite()
BrownWindowTest = functools.partial(TestGenericWindow, color='brown')
BlueWindowTest = functools.partial(TestGenericWindow, color='blue')
GreenWindowTest = functools.partial(TestGenericWindow, color='green')
s.addTest(unittest.makeSuite(BrownWindowTest))
s.addTest(unittest.makeSuite(BlueWindowTest))
s.addTest(unittest.makeSuite(GreenWindowTest))
return s

if __name__ == '__main__': #unittest.main()
unittest.main()

That code gives the following:

Traceback (most recent call last):
File "./functools.partial.py", line 32, in <module>
unittest.main()
File "/usr/lib/python2.6/unittest.py", line 816, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.6/unittest.py", line 837, in parseArgs
self.test = self.testLoader.loadTestsFromModule(self.module)
File "/usr/lib/python2.6/unittest.py", line 559, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "/usr/lib/python2.6/unittest.py", line 550, in loadTestsFromTestCase
return self.suiteClass(map(testCaseClass, testCaseNames))
TypeError: __init__() takes exactly 3 arguments (2 given)

Thanks for having a look,
Joel
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-06-2009
Joel Smith wrote:

> Hi List,
> I want to make some test case classes that can have some data passed in
> to modify the way they behave. I can't see a straightforward manner to
> pass data to an __init__() method of a class derived from
> unittest.TestCase, or to pass data to a test function within that
> class. Being a C++ guy, I thought "If only Python had something
> equivalent to boost::bind, I'd be fine," and then I discovered
> functools.partial. I found a post showing how to create classes using
> partial, and I thought I was good to go. The post I found is here:
>
> http://mail.python.org/pipermail/ban...er/000974.html
>
> So I adapted that code to see if it worked in the context of unittest.
> When I run the code, it acts like the parameter I provided with partial
> isn't even there. Any ideas?
>
> #!/usr/bin/python
>
> import functools
> import unittest
>
> class GenericWindow:
> def __init__(self, name, width, height, color='white'):
> print('Name: %s, width: %d, height: %d, color: %s' % (name, width,
> height, color))
>
> class TestGenericWindow(unittest.TestCase):
> def __init__(self, methodName, color):
> unittest.TestCase.__init__(self, methodName)
> print('color: %s' % color)
> self.color = color
>
> def testit():
> GenericWindow('foo', width=100, height=100, color=self.color)
>
> def suite():
> s = unittest.Suite()
> BrownWindowTest = functools.partial(TestGenericWindow, color='brown')
> BlueWindowTest = functools.partial(TestGenericWindow, color='blue')
> GreenWindowTest = functools.partial(TestGenericWindow, color='green')
> s.addTest(unittest.makeSuite(BrownWindowTest))
> s.addTest(unittest.makeSuite(BlueWindowTest))
> s.addTest(unittest.makeSuite(GreenWindowTest))
> return s
>
> if __name__ == '__main__': #unittest.main()
> unittest.main()
>
> That code gives the following:
>
> Traceback (most recent call last):
> File "./functools.partial.py", line 32, in <module>
> unittest.main()
> File "/usr/lib/python2.6/unittest.py", line 816, in __init__
> self.parseArgs(argv)
> File "/usr/lib/python2.6/unittest.py", line 837, in parseArgs
> self.test = self.testLoader.loadTestsFromModule(self.module)
> File "/usr/lib/python2.6/unittest.py", line 559, in loadTestsFromModule
> tests.append(self.loadTestsFromTestCase(obj))
> File "/usr/lib/python2.6/unittest.py", line 550, in
> loadTestsFromTestCase
> return self.suiteClass(map(testCaseClass, testCaseNames))
> TypeError: __init__() takes exactly 3 arguments (2 given)
>
> Thanks for having a look,
> Joel


By default unittest.main() looks for subclasses of unittest.TestCase and
generates test cases for every method that starts with "test". From that
point of view suite() is just an ordinary function and will not even be
called. There are a lot more problems with your code that suggest that you
should carefully read the unittest documentation, and maybe even have a look
at its source code.

Here's what became of your code when I tried to make it run:

import unittest

class GenericWindow:
def __init__(self, name, width, height, color='white'):
print('Name: %s, width: %d, height: %d, color: %s' % (name, width,
height, color))

class TestGenericWindow(unittest.TestCase):
def __init__(self, methodName, color):
unittest.TestCase.__init__(self, methodName)
print('color: %s' % color)
self.color = color

def testit(self):
GenericWindow('foo', width=100, height=100, color=self.color)

def suite():
return unittest.TestSuite([
TestGenericWindow("testit", color="brown"),
TestGenericWindow("testit", color='blue'),
TestGenericWindow("testit", color='green'),
])

if __name__ == '__main__':
import sys
argv = sys.argv[:]
argv.insert(1, "suite")
unittest.main(argv=argv)





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using cpu and ram of a group of boxes to do work with perl, mix os and hardwarer gavino Perl Misc 0 11-01-2007 12:28 AM
Can you mix wired and unwired hardware in a simple home LAN =?Utf-8?B?cGhhc3Rp?= Wireless Networking 6 12-25-2005 04:16 AM
Can you mix it up between 2k and 2003? =?Utf-8?B?VHJlZ2dlZWU=?= MCSE 1 12-09-2005 06:29 PM
WARNING - Firefox 1.0.5 and Roboform don't mix Lil' Abner Firefox 2 07-16-2005 12:42 AM
Cisco vpn server enabled / VPN and no-VPN connections mix Elise Cisco 6 05-22-2004 07:55 AM



Advertisments