[TIP] Recording changes and asserting an empty mock list in python unittest
halsmit at t-online.de
Thu Apr 26 11:27:01 PDT 2012
On Thu, Apr 26, 2012 at 02:09:33PM +0300, Barbu Paul Gheorghe wrote:
> 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):
> So the problem is that my baz mock object doesn't change accordingly
> after the foo() call and the last assertion fails.
Through patch() you create a global my_list variable that is different from the
one used in foo(). To make that test work (apart from globals being really
bad), you'd have to adapt the code to include this:
my_list = 
my_list[:] = 
You did hide the real problem by adding the create=True argument.
> 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?
Not at all. The my_list in foo() is local to that function, and created
when the function is executed.
More information about the testing-in-python