Problem When Unit Testing with PMock
Terry Reedy
tjreedy at udel.edu
Sun Feb 27 16:36:57 EST 2005
<steven at lczmsoft.com> wrote in message
news:1109483520.813509.171880 at f14g2000cwb.googlegroups.com...
>> def mockit(): raise StopIteration
This was meant to be a generator function. Peter corrected my error in
leaving out a never reached yield to make it so. My apologies. Putting if
False: yield None as the first line in the function has same effect.
> but it behaviors differenctly when pass in a mockit() and pass in an
> iterator with empty. so i think the code emulates nothing.
I hope Peter's addition clarifies and improves the situation. My idea
was/is that an iterator comsumer should be tested with an empty iterator.
>> def intit(k):
>> for i in range(k): yield i
>
> Now you mean define my own iteration without the help of pmock.
That was also my intention above. 'mockit' was just a name, this could be
'mockintit'.
> there are still so many other methods in the iterator for pass in,
I don't understand this. An iterator only has a trivial .__iter__ method
and a usually nontrivial .next method. A generator function makes it easy
to create as many iterators as you need.
> have to mock them one by one totally manually, its boring and thats the
> reason why i want pmock.
I am not familiar with pmock, but my impression is that mock objects are
for objects that you may not have available, such as a connection to a
database or file or instance of an umimplemented class ;-). For such
objects, the mock-ness consists in returning canned rather than actual
fetched or calculated answers. Iterators, on the other hand, are almost as
easily available as ints, lists, etc. Anything with the interator
interface and behavior *is* an interator, though you could make one yield
mock objects. Do as you wish once you understand whatever choices pmock
gives you.
Terry J. Reedy
More information about the Python-list
mailing list