0

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.

Ankit Dixit
  • 142
  • 11

2 Answers2

0

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)

Uri Shalit
  • 2,198
  • 2
  • 19
  • 29
  • Thanks for the response, able to fix this issue. The bug was at someplace else and after fixing that returning values with braces worked fine. – Ankit Dixit Dec 22 '16 at 06:36
0

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.

Ankit Dixit
  • 142
  • 11