[Python-Dev] Proposed unittest changes

Michael Foord fuzzyman at voidspace.org.uk
Tue Jul 15 05:34:18 CEST 2008


Guido van Rossum wrote:
> On Mon, Jul 14, 2008 at 5:13 PM, Raymond Hettinger <python at rcn.com> wrote:
>   
>> It takes about one day of using py.test to realize have much
>> cleaner and more readable its syntax is.  Also, writing the
>> tests is *much* more pleasant.  It has the same clean, clear
>> joy as writing regular python code.  By comparison, the code
>> using unittest.py is javaesque.   I've written tons of test with
>> unittest.py and and find it to be joyless.
>>     
>
> I, too, have written tons of tests with unittest.py (and Google's
> extensions, which follow the same style), and reviewed even more. I
> agree that this is pretty joyless, but I'm not at all sure that the
> unittest API is the reason. It seems to me that a main problem with
> writing test code is and will always remain due to the need to use
> mocks, stubs and other similar techniques (e.g. dependency injection).
> Typical test code that I've written or reviewed spends more time
> setting up the input conditions for testing than it spends checking
> the results. Ten lines of mocking code to one self.assertEqual() call
> seems typical.
>
>   
Maybe Python needs a good mocking module in the standard library. There 
are plenty, but we use a particularly nice one at Resolver Systems [1]. :-)

It auto-creates attributes as mocks, allowing you to assert calls made 
to all of its children along with convenience methods like 
'assert_called_with' and has a companion decorator that patches class / 
module level attributes just for the duration of the test.

As we're changing more of our tests over to use these we're finding it 
reduces the volume and complexity of our test code.

Michael Foord

[1] Based on http://code.google.com/p/mock/ although there is some 
outstanding code to sync back to the project.

-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/
http://www.trypython.org/
http://www.ironpython.info/
http://www.theotherdelia.co.uk/
http://www.resolverhacks.net/



More information about the Python-Dev mailing list