[TIP] Mock objects and hierarchical visitor pattern

Grig Gheorghiu grig at gheorghiu.net
Sat Jun 9 08:43:07 PDT 2007


--- Stefano Masini <stefano.masini at pragma2000.com> wrote:
> So, for example, starting from the deepest level (leaves) I write
> some
> tests for processLeaf(leaf) that prepare some leaf object, pass it to
> the processLeaf() function, but before that I set up my mocks and
> write expectations for them.
> Ok. Everything is fine.
> 
> Now let's go up one level, and test processBranch(branch).
> I would like to follow the same approach, i.e. prepare a branch, set
> the mock expectations and call processBranch(). But this time, since
> a
> branch is made of leaves, the functions processBranch() will call
> several times the function processLeaf(). This is very unconvenient,
> because it forces me to define a whole bunch of expectations on the
> mock objects, even for mock leaves, that I don't care much about,
> since processLeaf() has already been tested.
> 

I would say you still need to check at least that processLeaf() has
been called (and maybe with the right number of parameters). So when
testing the Branch object, use a mock Leaf object and only verify that
processLeaf() has been called. That shouldn't introduce too much
overhead in your testing code.

Grig



More information about the testing-in-python mailing list