<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: 'courier new', monospace; ">real_import = __import__</span></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">def windows_safe_import(name, globals={}, locals={}, fromlist=(), *args, **kargs):</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> if name in ["wmi", "win32api"] or (name == "ctypes" and fromlist is not None and "windll" in fromlist):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> return MagicMock()</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> </font></div><div><font class="Apple-style-span" face="'courier new', monospace"> else:</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> return real_import(name, globals, locals, fromlist, *args, **kargs)</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">@patch("__builtin__.__import__", new = windows_safe_import)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">class MyTestCase(unittest.TestCase):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>...</font></div><div><font class="Apple-style-span" face="'courier new', 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't be imported before we mock the __import__)</font></div>
</div><div><font class="Apple-style-span" face="'courier new', 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="'courier new', monospace">1.</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> import sys</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> sys.modules['wmi'] = MagicMock()</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">2. </font></div><div><font class="Apple-style-span" face="'courier new', monospace"> @patch.dict('sys.modules', {'wmi': MagicMock()})</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">Then "import wmi" will find your mock. </font><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">I'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="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', 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="'courier new', 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'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="'courier new', 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="'courier new', monospace"><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; ">Any ideas?</span></font></div></div>