<div dir="ltr">Yep that one works well.<div>I&#39;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(&quot;...&quot;).start() in setUp and .stop() at tearDown, but it&#39;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&#39;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">&lt;<a href="mailto:michael@voidspace.org.uk">michael@voidspace.org.uk</a>&gt;</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:&#39;courier new&#39;,monospace">real_import = __import__</span></div>
          <div><font face="&#39;courier new&#39;,
              monospace"><br>
            </font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">def windows_safe_import(name, globals={},
              locals={}, fromlist=(), *args, **kargs):</font></div>
          <div><font 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 face="&#39;courier new&#39;,
              monospace">        return MagicMock()</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">    </font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">    else:</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">        return real_import(name, globals,
              locals, fromlist, *args, **kargs)</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace"><br>
            </font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">@patch(&quot;__builtin__.__import__&quot;, new =
              windows_safe_import)</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">class MyTestCase(unittest.TestCase):</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace"><span style="white-space:pre-wrap"> </span>...</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace"><span style="white-space:pre-wrap"> </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 face="&#39;courier new&#39;,
            monospace"><br>
          </font></div>
        <div><font face="arial, helvetica,
            sans-serif">Michael suggested 2 alternatives:</font></div>
        <div>
          <div><font face="&#39;courier new&#39;,
              monospace">1.</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">    import sys</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">    sys.modules[&#39;wmi&#39;] = MagicMock()</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace"><br>
            </font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">2.      </font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">    @patch.dict(&#39;sys.modules&#39;, {&#39;wmi&#39;:
              MagicMock()})</font></div>
          <div><font face="&#39;courier new&#39;,
              monospace"><br>
            </font></div>
          <div><font face="&#39;courier new&#39;,
              monospace">Then &quot;import wmi&quot; will find your mock. </font><span style="font-family:&#39;courier new&#39;,monospace">I&#39;m very wary of overriding __import__
              even in tests.</span></div>
          
        </div>
        <div><font face="&#39;courier new&#39;,
            monospace"><br>
          </font></div>
        <div><font face="&#39;courier new&#39;,
            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="&#39;courier new&#39;,
            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&#39;t recognize any tests. If I comment
              out the @patch.dict line, everything works fine.</span></font></div>
        <div><font face="&#39;courier new&#39;,
            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&#39;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(&#39;sys.modules&#39;, {&#39;wmi&#39;: 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="&#39;courier new&#39;, 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>