[TIP] mock objects
John Arbash Meinel
john at arbash-meinel.com
Fri Apr 13 14:09:10 PDT 2007
Mike Beachy wrote:
> Hi all -
> Does anyone have any insight to offer on the various mock object libs?
> I just slapped together a test using pymock and found that it took
> about twice as many lines of code as compared to a test that used a
> simple fake object.
> The only "advantage" I see is that the pymock test guarantees that no
> additional calls are made to the mocked object. But, this seems to be
> a rather tight binding to the underlying protocol.
> Do I just not get it? Is this a case of a Java pattern not
> successfully translating to python? Does the infrastructure pay off
> after you get larger numbers of tests in place?
> The only mock lib I've seen that seems worthwhile so far is Ian
> Bicking's minimock.
I thought this was a pretty good article about Mock testing.
And its ability to test behaviors versus state.
A benefit he mentions are that your tests can be really really fast,
since you don't have to build up a given state to test the transition to
the next state. (Instead you build up a Mock defining the transition,
but that is usually much faster without doing any disk I/O,
On the flip side, Mock testing ties you more directly into
implementation (as you mention). And it has problems with api skew. You
may test that your class Foo calls Bar.baz() correctly with mocks. But
it won't show you that the real function is actually Bar.buz().
So for things that are really heavy to setup (databases perhaps), Mock
can be useful, as long as you can prove that the Mock object matches the
I also found Robert Collins (incomplete?) essay useful:
More information about the testing-in-python