[TIP] Recording changes and asserting an empty mock list in python unittest

Barbu Paul Gheorghe paullik.paul at gmail.com
Thu Apr 26 04:09:33 PDT 2012


I'm using unittest and mock (Michael Foord's module) to test some python code.

I have something like this (this is a proof of concept that could be rewritten 
more cleanly, but my real code needs to behave like that foo function):

http://pastebin.com/DEFUtqr0

So the problem is that my baz mock object doesn't change accordingly after the 
foo() call and the last assertion fails.

If I use my_list.remove(x) in foo() then I can see the changes in my test case, 
but I just want to empty that list, I don't want to pass through every element 
of the list then remove it, no, I want a fast empty operation.

How can I check if my mock object is emptied without using .remove(x), but using 
the current implementation of function foo?

-- 
Barbu Paul - Gheorghe
Common sense is not so common - Voltaire
Visit My GitHub profile to see my open-source projects - https://github.com/paullik



More information about the testing-in-python mailing list