Skip to content Skip to sidebar Skip to footer

Patch Not Mocking A Module

I'm trying to mock subprocess.Popen. When I run the following code however, the mock is completely ignored and I'm not sure why Test Code: def test_bring_connection_up(self): #

Solution 1:

You're not patching in the right place. You patch where Popen is defined:

withpatch('subprocess.Popen', mock_popen):

You need to patch where Popen is imported, i.e. in the "Module Code" where you have written this line:

from subprocess importPopen, PIPE

I.e., it should look something like:

withpatch('myapp.mymodule.Popen', mock_popen):

For a quick guide, read the section in the docs: Where to patch.

Post a Comment for "Patch Not Mocking A Module"