<div dir="ltr">Yep that one works well.<div>I'll use it for now :)</div><div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8">Actually I use patch.dict("...").start() in setUp and .stop() at tearDown, but it's the same concept...<br>
</div><div><br></div><div>Would like to debug nose but a bit on a deadline with this task so I'll have to postpone it.</div><div><br></div><div><br><div class="gmail_quote">On Wed, May 25, 2011 at 2:06 AM, Michael Foord <span dir="ltr"><<a href="mailto:michael@voidspace.org.uk">michael@voidspace.org.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div bgcolor="#ffffff" text="#000000"><div><div></div><div class="h5">
On 24/05/2011 08:31, Yoni Tsafir wrote:
<blockquote type="cite">
<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 style="font-family:'courier new',monospace">real_import = __import__</span></div>
<div><font face="'courier new',
monospace"><br>
</font></div>
<div><font face="'courier new',
monospace">def windows_safe_import(name, globals={},
locals={}, fromlist=(), *args, **kargs):</font></div>
<div><font 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 face="'courier new',
monospace"> return MagicMock()</font></div>
<div><font face="'courier new',
monospace"> </font></div>
<div><font face="'courier new',
monospace"> else:</font></div>
<div><font face="'courier new',
monospace"> return real_import(name, globals,
locals, fromlist, *args, **kargs)</font></div>
<div><font face="'courier new',
monospace"><br>
</font></div>
<div><font face="'courier new',
monospace">@patch("__builtin__.__import__", new =
windows_safe_import)</font></div>
<div><font face="'courier new',
monospace">class MyTestCase(unittest.TestCase):</font></div>
<div><font face="'courier new',
monospace"><span style="white-space:pre-wrap"> </span>...</font></div>
<div><font face="'courier new',
monospace"><span style="white-space:pre-wrap"> </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 face="'courier new',
monospace"><br>
</font></div>
<div><font face="arial, helvetica,
sans-serif">Michael suggested 2 alternatives:</font></div>
<div>
<div><font face="'courier new',
monospace">1.</font></div>
<div><font face="'courier new',
monospace"> import sys</font></div>
<div><font face="'courier new',
monospace"> sys.modules['wmi'] = MagicMock()</font></div>
<div><font face="'courier new',
monospace"><br>
</font></div>
<div><font face="'courier new',
monospace">2. </font></div>
<div><font face="'courier new',
monospace"> @patch.dict('sys.modules', {'wmi':
MagicMock()})</font></div>
<div><font face="'courier new',
monospace"><br>
</font></div>
<div><font face="'courier new',
monospace">Then "import wmi" will find your mock. </font><span style="font-family:'courier new',monospace">I'm very wary of overriding __import__
even in tests.</span></div>
</div>
<div><font face="'courier new',
monospace"><br>
</font></div>
<div><font face="'courier new',
monospace">
<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 face="'courier new',
monospace"><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 face="'courier new',
monospace"><span style="font-family:arial,helvetica,sans-serif">Tried to
debug it a bit but no luck...</span></font></div>
</div>
</blockquote>
<br></div></div>
Note that one fix *may* be (if you're using Python 2.5 or more
recent) to use patch.dict as a context manager so that it is called
inside the test instead of at test import time:<br>
<br>
def test_something():<br>
with patch.dict('sys.modules', {'wmi': MagicMock()}):<br>
...<br>
<br>
All the best,<br><font color="#888888">
<br>
Michael</font><div class="im"><br>
<br>
<blockquote type="cite">
<div dir="ltr">
<div>
<font face="'courier new', monospace"><span style="font-family:arial,helvetica,sans-serif">Any ideas?</span></font></div>
</div>
<pre><fieldset></fieldset>
_______________________________________________
testing-in-python mailing list
<a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
</blockquote>
<br>
<br>
</div><pre cols="72">--
<div class="im"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a>
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a>
</div></pre>
</div>
</blockquote></div><br></div></div>