Art of Unit Testing: Part 2

William Tanksley wtanksle at
Fri Aug 24 22:02:17 CEST 2001

On Fri, 24 Aug 2001 11:51:07 -0700, Jesse F. W wrote:
>Dear Python-list-iners,
>	I have another Testing related question.  How are unit tests done 
>when the units to be tested get lots of information from nested 
>objects, e.g. (in a method of a class to be tested):
>how would this be tested?

It wouldn't -- it would be deleted and abhorred.  That's a violation of
encapsulation and of the law of Demeter (google that if you don't know
what it means).

The problem is that each object should have an interface to do everything
it needs to do.  You shouldn't have to reach inside any object to inspect
how it's doing its job.  The above code should look like 'if' (I'm assuming that's an
appropriate name).

However, your question still remains: how do you unit test objects which
are supposed to delegate operations to other objects?  The answer is that
you write the unit test without assuming any delegation.  That method you
mentioned should work whether or not delegation is happening; and your
test won't care, because it only tests the method, not the implementation.

>Jesse W

-William "Billy" Tanksley

More information about the Python-list mailing list