Testing for empty iterators?
Peter Otten
__peter__ at web.de
Sun Jul 4 06:59:21 EDT 2004
Roy Smith wrote:
> Now I need to test to see if the iterator is empty. Actually, it's a
> unit test where I want to assert that it is empty. In the old days, I
> would have done:
>
> assertEquals (getListOfThings (), [])
>
> but I don't see any clean way to do this with an iterator. The best I
[and in a follow-up]
> Oh, never mind, I got it...
>
> assertEquals (list (getThingIterator ()), [])
An alternative would be
self.assertRaises(StopIteration, getThingIterator().next)
which is a bit stricter as it will choke if getThingIterator() returns a
list instead of an iterator.
Peter
More information about the Python-list
mailing list