with mock.patch.object(subprocess, 'Popen') as mocked_popen:
    mocked_popen.return_value.communicate.return_value = some_fake_result
function_which_uses_popen_communicate()
How to unit test a function that uses Popen?