<div dir="ltr">Moving a discussion with Michael Foord to TIP so everyone can contribute:<div><br></div><div>I suggested a feature to the mock framework in which you can mock non existing imports (useful when you are unit-testing platform dependent code).</div>

<div><br></div><div>My solution for this was the following:</div><div><div><br></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">real_import = __import__</span></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>

</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">def windows_safe_import(name, globals={}, locals={}, fromlist=(), *args, **kargs):</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    if name in [&quot;wmi&quot;, &quot;win32api&quot;] or (name == &quot;ctypes&quot; and fromlist is not None and &quot;windll&quot; in fromlist):</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        return MagicMock()</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    </font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    else:</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        return real_import(name, globals, locals, fromlist, *args, **kargs)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>

</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">@patch(&quot;__builtin__.__import__&quot;, new = windows_safe_import)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">class MyTestCase(unittest.TestCase):</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>...</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-tab-span" style="white-space:pre">        </span># (tested module was imported only inside the test so the problematic imports it uses won&#39;t be imported before we mock the __import__)</font></div>

</div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Michael suggested 2 alternatives:</font></div><div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">1.</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    import sys</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    sys.modules[&#39;wmi&#39;] = MagicMock()</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">2.      </font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    @patch.dict(&#39;sys.modules&#39;, {&#39;wmi&#39;: MagicMock()})</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Then &quot;import wmi&quot; will find your mock. </font><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">I&#39;m very wary of overriding __import__ even in tests.</span></div>

<meta http-equiv="content-type" content="text/html; charset=utf-8"></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; ">Well, just wanted to update that #1 works well, but #2 causes something weird:</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; ">On PyDev I can run the tests fine, but when I try running them with nosetests, nose doesn&#39;t recognize any tests. If I comment out the @patch.dict line, everything works fine.</span></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; ">Tried to debug it a bit but no luck...</span></font></div><div>

<font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; ">Any ideas?</span></font></div></div>