[TIP] Return different values based on an argument passed to a method

Marcin Zajączkowski mszpak at wp.pl
Mon Mar 5 04:33:17 PST 2012


Hi,

Is it possible to simple stub a method's return value when called with an argument (any argument or specific one)?

I tried with:
mock = Mock()
mock.foo(ANY).return_value(False)

but in a debugger I see that in my code mock.foo(<<some_object>>) returns a mock instead of False value. If not the returning value I could use assert_called_with with custom mather, but how to return different value based on an argument?

Regards
Marcin






More information about the testing-in-python mailing list