<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">I believe you can mock out ClassY as such:<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">@patch(&#39;liB.ClassY&#39;)<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">def test_unit_test(self):<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">    stuff<br><br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">More info: <a href="https://docs.python.org/3.3/library/unittest.mock.html#quick-guide">https://docs.python.org/3.3/library/unittest.mock.html#quick-guide</a><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 1:35 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>
I&#39;ve been using pytest and it is great. There is just one thing that<br>
I&#39;m not satisfied with the way I&#39;ve implemented, that is module level<br>
moking.<br>
<br>
My scenario is as follows: libA and libB where libA depends on libB<br>
(uses class definition as superclass). Let&#39;s assume this code:<br>
<br>
&lt;libA.py&gt;<br>
from libB import ClassY<br>
<br>
class ClassX(ClassY):<br>
   pass<br>
<br>
&lt;libB.py&gt;<br>
class ClassY(object):<br>
   pass<br>
<br>
Those modules live on different packages/repositories. LibB can be<br>
unit tested just fine, but we need to mock libB module to allow libA<br>
to be unit tested (libB won&#39;t be available in the moment of the test).<br>
<br>
In a conftest.py file in the tests directory of libA, I&#39;ve managed to<br>
mock the module import by mocking sys.modules dictionary, but I did<br>
this at the module (global) scope. I was not able to do it with any<br>
hook (like pytest_configure), because the tests files are imported<br>
before the hook is called. So a test_libA.py file with &quot;import libA&quot;<br>
would cause the test to fail.<br>
<br>
Am I doing something wrong or is this really the way to go?<br>
<br>
Greetings,<br>
Vinicius<br>
<br>
_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
</blockquote></div><br></div>