[TIP] [python] Re: Mock Testing Patterns
fuzzyman at voidspace.org.uk
Mon Nov 19 10:08:00 PST 2007
Gustavo Niemeyer wrote:
> Hello Michael,
>> Well, the pattern is usually more like:
>> setup mocks
>> So yes, there is some setup to put the mocks in place.
>> This pattern works *fine*, but we aren't yet using a mocking library. I
>> wonder if there are any that will support us in this pattern rather than
>> switching to recording expectations.
> I find it unlikely. As Martin explained, the setup work is needed in any
> case, as you have to specify return values, ordering between expressions,
> actions executed, and so on. If you're going to setup these things ahead
> of time, you're actually already defining expectations.
Hmm... not directly you're not. You may patch a method with a listener
so that you can make an assert as to what arguments it was called with -
but you don't specify those arguments ahead of time.
You do the setup, then perform the action, then make assertions about
> Another detail I find important in that regard is that I want the mocking
> library to, as much as possible, explode right at the spot where the
> expectation was broken. This usually makes fixing the situation much more
Well, failing at a specific assertion is usually exact enough for us. I
still find the 'record -> replay' approach backwards and less intuitive.
I guess it is a question of taste, but I don't think you're going to
persuade me. :-)
I've rolled my own Mock class that lets me do what I want, and as is the
way with Python it turns out to be less than 50 LOC. I'll post it
shortly. It currently doesn't do the patching for you - but at Resolver
we do this with a decorator pattern that I think is quite nice - so I'll
add this shortly.
More information about the testing-in-python