Patch not mocking a module

前端 未结 1 1971
青春惊慌失措
青春惊慌失措 2020-12-20 04:01

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:

<         


        
相关标签:
1条回答
  • 2020-12-20 04:27

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

    with patch('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 import Popen, PIPE
    

    I.e., it should look something like:

    with patch('myapp.mymodule.Popen', mock_popen):
    

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

    0 讨论(0)
提交回复
热议问题