<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_1_1479937189314_15409"><br></div><div id="yui_3_16_0_1_1479937189314_15409"><br></div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">I am testing some code that is distributed across multiple modules.&nbsp;</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">Each module imports a single method that I want to mock,&nbsp;</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">Say check_call from subprocess or open from builtins.</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr"><br></div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">I want to mock out these methods from the multiple modules with one mock object.&nbsp;</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">Whats the best way to do it.</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr"><br></div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">mopen=mock.MagicMock()</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr"><br></div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr"># I don't want to see the following, where multiple mock open objects get passed</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">@patch('module1.open',new=mopen)</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">@patch('module2.open',new=mopen)</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">def test_case1(mopen1, mopen2):</div><div id="yui_3_16_0_1_1479937189314_15409" dir="ltr">&nbsp; &nbsp; m1_open.side_effect &nbsp;= [OSError]<br></div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15676">&nbsp; &nbsp; m2_open.side_effect = [OSError]</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15677"><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_1_1479937189314_15691">&nbsp; &nbsp; # Test code</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15692"><br id="yui_3_16_0_1_1479937189314_15693"></div></div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15600">I want to see something like the following</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15600">@patch.special(['module1.open', 'module2.open', 'module3.open'], new=mopen)</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15625">@patch('module2.open',new=mopen)</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15626">def test_case1(mopen):</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15700">&nbsp; &nbsp; mopen.side_effect = [OSError, OSError]</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15703"></div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15627">&nbsp; &nbsp; '# Test code</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15628"><br id="yui_3_16_0_1_1479937189314_15629"></div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15628"><br></div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15628">Is there a way to do this?</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15628"><br></div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15628">Thanks,</div><div dir="ltr" id="yui_3_16_0_1_1479937189314_15628">Sarvi</div></div></body></html>