How to mock an import
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Module A includes import B at its top. However under test conditions I'd like to mock B in A (mock A.B ) and completely refrain from importing B . In fact, B isn't installed in the test environment on purpose. A is the unit under test. I have to import A with all its functionality. B is the module I need to mock. But how can I mock B within A and stop A from importing the real B, if the first thing A does is import B? (The reason B isn't installed is that I use pypy for quick testing and unfortunately B isn't compatible with pypy yet.) How