How to mock multiple return values in Easymock

766 views Asked by At

I am a python newbie and am trying to mock process.communicate method, but i do not know how to return multiple values from mock. The way i am approaching it is

with patch.object(subprocess, 'Popen', new_callable=MagicMock) as process:
  process.communicate.return_value = [b'', b'']
  output, error = process.communicate()

The error message i am getting is :

>       output, error = process.communicate()
E       ValueError: not enough values to unpack (expected 2, got 0)

Can someone please point out what wrong am i doing, i have tried returning with and without square and curly braces.

2

There are 2 answers

1
Uri Shalit On

process.communicate returns a tuple and not a list so all you need to change is to:

with patch.object(subprocess, 'Popen', new_callable=MagicMock) as process:
  process.communicate.return_value = (b'', b'')
  output, error = process.communicate()

In general in Python when you return a tuple it can be returned into a tuple pointer or unpacked to multiple values. For more info you can go here (First on Google)

0
Ankit Dixit On

Sorry for the mis-information, but i have found out that the error was in some different part of the code and the above mentioned syntax works fine with our without any kind of braces.