I'm trying to write a python unittest that does a subprocess call, so I'd like to mock that call.
I've already gone through these SO questions (to no avail):
- Mocking a subprocess call in Python
- mocking subprocess.Popen
- mocking subprocess.Popen dependant on import style
- Mocking two functions with patch for a unit test
benchmark.py
from subprocess import Popen, PIPE, STDOUT
def some_func():
with Popen(some_list, stdout=PIPE, stderr=STDOUT) as process:
stdout, stderr = process.communicate(timeout=timeout)
test.py
import mock
@mock.patch('benchmark.Popen.communicate')
@mock.patch('benchmark.Popen')
def test_some_func(self, mock_popen, mock_comm):
mock_popen.return_value = 0
mock_comm.return_value = ('output', 'error')
foo = benchmark.some_func()
When running the unittest I get:
stdout, stderr = process.communicate(timeout=timeout)
ValueError: not enough values to unpack (expected 2, got 0)
It looks like I'm not mocking the return value of communicate
correctly; what am I doing wrong?
solution
I took the comments and suggested answers to solve things like this:
test.py
import mock
@mock.patch('benchmark.Popen')
def test_some_func(self, mock_popen):
process = mock_popen.return_value.__enter__.return_value
process.returncode = 0
process.communicate.return_value = (b'some output', b'some error')
foo = benchmark.some_func()