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

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Oct 6 06:54:45 EDT 2009


En Mon, 05 Oct 2009 23:12:02 -0300, Joel Smith <js-pythonlist at jk1.net>  
escribió:

> 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?

The problem isn't with partial - which works fine. By using  
unittest.main() your're building the default test suite using the default  
rules - your customized suite isn't used at all. Try with:

if __name__ == '__main__':
     unittest.TextTestRunner().run(suite())

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)

You may get rid of the 'testit' repetitive argument if you omit it from  
__init__ -- the generic TestLoader won't like it, but it doesn't like it  
right now because of the color argument, so you don't lose anything.

Another approach, if you want to stay with the built-in testing  
infrastructure:

class TestGenericWindow(unittest.TestCase):

   def _testit(self, color):
      # actual test implementation
      GenericWindow('foo', width=100, height=100, color=color)

   def test_brown(self):
      return self._testit('brown') # provide desired parameters

   def test_blue(self):
      return self._testit('blue')

unittest.main() should still work with this TestCase as written, without a  
custom Suite/Loader.

-- 
Gabriel Genellina




More information about the Python-list mailing list