if I have mymodule.py:<div><br></div><div>class MyClass:</div><div>       def run():</div><div>             return 1</div><div><br></div><div><br></div><div><br></div><div>then I try to mock it in test:</div><div><br></div>

<div><br></div><div>with patch(&#39;mymodule.MyClass&#39;) as mock:</div><div>      instance = mock.return_value</div><div><br></div><div>      mock.run.return_value =2</div><div><br></div><div><br></div><div>this works fine. but if in my test code, I import the MyClass first:</div>

<div><br></div><div>from mymodule import MyClass</div><div><br></div><div>with patch(&#39;MyClass&#39;) as mock:</div><div> ......</div><div><br></div><div>it fails to find &#39;MyClass&#39;</div><div><br></div><div><br>
</div>
<div><br></div><div>even more serious is that if I refer to the mock class with full name mymodule.Myclass in the patch() line, but the production code refers to it as simply Myclass after importing it first, the mock does not take effect,  so I have to modify production code to mock it out. this completely does not work for real testing cases.</div>

<div><br></div><div><br></div><div><br></div>