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

2009-10-06 Thread Joel Smith

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/bangpypers/2008-December/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 
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
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-10-06 Thread Joel Smith

Gabriel Genellina wrote:
Note that you don't *have* to use partial in this case, as you're 
building the suite yourself. Just create the TestCase instances manually:


suite = unittest.TestSuite([
  TestGenericWindow('testit', 'brown'),
  TestGenericWindow('testit', 'blue'),
  TestGenericWindow('testit', 'green')
  ])
unittest.TextTestRunner().run(suite)


Perfect!  This is exactly what I needed.  For some reason, I didn't
understand that I could construct my TestCase objects directly... I
thought that I had to allow the unittest framework to construct them for
me.  This is straightforward, and does exactly what I need.
Many thanks,
Joel

--
http://mail.python.org/mailman/listinfo/python-list