pythonmockingnose

Mock python function with multiple return values


I have a python function that returns multiples values. My function:

def myExampleFunction(a,b)
    # here is my code
    return name, number1, number2


def FunctionIWantToTest(self):
    # here is my code
    myName, myNumber1, myNumber2 = self.myExampleFunction(a,b)

I want to give my own values to the returned values from FunctionIWantToTest. So, I'm trying to test the 2nd function with nosetest, but I don't know how to mock the return of myExampleFunction.

I tried this:

def test_mytest(self):
    [...]
    c.myExampleFunction = Mock()
    c.myExampleFunction.side_effect = ["myName", 100, 200]
    [...]

But it doesn't work. When I launch nosetests I read this message:

ValueError: too many values to unpack

Any idea? I use python 2.7.3


Solution

  • You need to set the return_value of the mock, not side_effect.

    You can do this when you instantiate it:

    c.myExampleFunction = Mock(return_value=["myName", 100, 200])