Which mock library do you prefer?

Terry Reedy tjreedy at udel.edu
Wed Mar 3 21:47:11 CET 2010

On 3/3/2010 7:43 AM, Albert van der Horst wrote:

>> Right, isolation is essential. But I can't decide to which extent I
>> should propagate isolation.
>> For example, in "Python Testing: Beginner's Guide" by Daniel Arbuckle,
>> author suggests that if you do unittesting you should isolate the
>> smallest units of code from each other. For example, if you have a
>> class:
>> Class SomeClass(object):
>>     def method1(self):
>>         return 5
>>     def method2(self):
>>         return self.method1 + 10
>> According to the book, if you want to test method2, you should isolate
>> it from method1 and class instance('self').
>> Other books are not so strict...
>> And what should I follow as newbie?

Pretty much any test assumes that basic things other than the tested 
object work correctly. For instance, any test of method2 will assume 
that '+' works correctly. The dependency graph between methods in a 
class will nearly always be acyclic. So I would start with the 'leaf' 
methods and work up. In the above case, test method1 first and then 
method2. The dependence of the test of method2 on the correctness of 
method1 is hardly worse, to me, then its dependence on the correctness 
of int.__add__. It is just the the responsibility for the latter falls 
on the developers, and *their* suite of tests.

Whenever any code test fails, there are two possibilities. The code 
itself is buggy, or something it depends on is buggy. I see two reasons 
for isolation and mock units: test resource saving (especially time) and 
independent development. If you are developing ClassA and someone else 
is developing ClassB, you might want to test ClassA even though it 
depends on ClassB and classB is not ready yet. This consideration is 
much less likely to apply to method2 versus method1 of a coherent class.

My current opinions.

Terry Jan Reedy

More information about the Python-list mailing list