<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Hi,<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">feels like the patch should be like this:<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">&lt;test_liba.py&gt;<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">@patch(&#39;liba.ClassB&#39;)<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">overwrite the ClassB that was imported in liba.py<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">*note liba.ClassB might not be the right complete path, you might have some folders you need to traverse or sth, but that&#39;s the general idea.<br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr">Best,<div>Zhi An</div></div></div></div>
<br><div class="gmail_quote">On Fri, Apr 17, 2015 at 3:23 PM, Vinicius Kwiecien Ruoso <span dir="ltr">&lt;<a href="mailto:vinicius.ruoso@gmail.com" target="_blank">vinicius.ruoso@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi!<br>
<br>
Thanks for your answer. Unfortunately, this does not work for me.<br>
libB is not importable during the test, and at some point the test try<br>
to import it.<br>
<br>
Let me show a real minimal example:<br>
├── src<br>
│   └── mylib<br>
│       ├── __init__.py<br>
│       ├── liba.py<br>
└── test<br>
    └── mylib<br>
        ├── conftest.py<br>
        └── test_liba.py<br>
<br>
8&lt;---------<br>
&lt;liba.py&gt;<br>
from mylibB.libb import ClassB<br>
<br>
class ClassA(ClassB):<br>
    pass<br>
<br>
&lt;conftest.py&gt;<br>
import os<br>
import sys<br>
<br>
_src_path = os.path.join(os.path.dirname(__file__), &#39;..&#39;, &#39;..&#39;, &#39;src&#39;)<br>
sys.path.insert(0, os.path.abspath(_src_path))<br>
<br>
&lt;test_liba.py&gt;<br>
from mock import patch<br>
<br>
@patch(&#39;mylibB.libb.ClassB&#39;)<br>
def test_classa_instance(mock_classB):<br>
    print mock_classB<br>
8&lt;---------<br>
<br>
This fails with: &quot;ImportError: No module named mylibB&quot; while executing<br>
the patch code.<br>
<br>
My real intent on the test_liba.py file was (and for that to work, I<br>
need to mock libb module import at the conftest.py file):<br>
<br>
import mylib.liba<br>
<br>
def test_classa_instance(mock_classB):<br>
    print mylib.liba.ClassA()<br>
<br>
<br>
Attached you may find this file structure if you want to run it.<br>
<br>
Just as a note, I&#39;m using Python 2.6 right now. I intend to support<br>
Python 2.7, but not 3.x at this time.<br>
<br>
Thanks again!<br>
Vinicius<br>
<br>
2015-04-17 14:47 GMT-03:00 Zhi An Ng &lt;<a href="mailto:ngzhian@gmail.com">ngzhian@gmail.com</a>&gt;:<br>
<div class="HOEnZb"><div class="h5">&gt; Hi,<br>
&gt; I believe you can mock out ClassY as such:<br>
&gt;<br>
&gt; @patch(&#39;liB.ClassY&#39;)<br>
&gt; def test_unit_test(self):<br>
&gt;     stuff<br>
&gt;<br>
&gt;<br>
&gt; More info:<br>
&gt; <a href="https://docs.python.org/3.3/library/unittest.mock.html#quick-guide" target="_blank">https://docs.python.org/3.3/library/unittest.mock.html#quick-guide</a><br>
&gt;<br>
&gt; Best,<br>
&gt; Zhi An<br>
&gt;<br>
&gt; On Fri, Apr 17, 2015 at 1:35 PM, Vinicius Kwiecien Ruoso<br>
&gt; &lt;<a href="mailto:vinicius.ruoso@gmail.com">vinicius.ruoso@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hi!<br>
&gt;&gt;<br>
&gt;&gt; I&#39;ve been using pytest and it is great. There is just one thing that<br>
&gt;&gt; I&#39;m not satisfied with the way I&#39;ve implemented, that is module level<br>
&gt;&gt; moking.<br>
&gt;&gt;<br>
&gt;&gt; My scenario is as follows: libA and libB where libA depends on libB<br>
&gt;&gt; (uses class definition as superclass). Let&#39;s assume this code:<br>
&gt;&gt;<br>
&gt;&gt; &lt;libA.py&gt;<br>
&gt;&gt; from libB import ClassY<br>
&gt;&gt;<br>
&gt;&gt; class ClassX(ClassY):<br>
&gt;&gt;    pass<br>
&gt;&gt;<br>
&gt;&gt; &lt;libB.py&gt;<br>
&gt;&gt; class ClassY(object):<br>
&gt;&gt;    pass<br>
&gt;&gt;<br>
&gt;&gt; Those modules live on different packages/repositories. LibB can be<br>
&gt;&gt; unit tested just fine, but we need to mock libB module to allow libA<br>
&gt;&gt; to be unit tested (libB won&#39;t be available in the moment of the test).<br>
&gt;&gt;<br>
&gt;&gt; In a conftest.py file in the tests directory of libA, I&#39;ve managed to<br>
&gt;&gt; mock the module import by mocking sys.modules dictionary, but I did<br>
&gt;&gt; this at the module (global) scope. I was not able to do it with any<br>
&gt;&gt; hook (like pytest_configure), because the tests files are imported<br>
&gt;&gt; before the hook is called. So a test_libA.py file with &quot;import libA&quot;<br>
&gt;&gt; would cause the test to fail.<br>
&gt;&gt;<br>
&gt;&gt; Am I doing something wrong or is this really the way to go?<br>
&gt;&gt;<br>
&gt;&gt; Greetings,<br>
&gt;&gt; Vinicius<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; testing-in-python mailing list<br>
&gt;&gt; <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
&gt;&gt; <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
&gt;<br>
&gt;<br>
</div></div></blockquote></div><br></div>