<div dir="ltr">If you know what the parent class is in advance, could you use mock.patch to stub out the parent method?<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 13 August 2014 19:00, Marcin Tustin <span dir="ltr">&lt;<a href="mailto:Marcin.Tustin@dealertrack.com" target="_blank">Marcin.Tustin@dealertrack.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div link="blue" vlink="purple" lang="EN-US">
<div>
<p class="MsoNormal">Hi All,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I’ve come across a method which I needed to add a unit test for, which calls super, e.g.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">def foofunc(self):<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">    x= do_some_stuff()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">    if x:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">        return x<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">    return super(WhatevClass, self).foofunc()<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Now, I don’t want to integration test the whole stack of super foofunc methods, just unit test that one, down to the super call.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I’ve accomplished this with, in my test (excuse the 8 leading spaces):<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">def setUp(self):<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">        self.method_archive = {}<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">        for klass in WhatevClass.__mro__[1:]:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">            # we use the class as its own sentinel<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">            meth_or_klass = klass.__dict__.get(&#39;foofunc&#39;, klass)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">            if meth_or_klass != klass:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">                self.method_archive[klass] = meth_or_klass<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">                del klass.foofunc<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">        # now patch an arbitrary member of the mro to supply a dummy foofunc<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">        self.sentinel_klass = next(iter(self.method_archive))<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">        self.mock_sentinel = mock.MagicMock()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:&quot;Arial monospaced for SAP&quot;">        self.sentinel_klass.foofunc = self.mock_sentinel<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">And corresponding unpatching in tearDown.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Is there any other approach which is standard, or objectively better?
<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
<p class="MsoNormal">Marcin<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<table style="width:237.75pt" border="0" cellpadding="0" cellspacing="0" width="396">
<tbody>
<tr>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:9.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666">Marcin Tustin</span></b><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666"><br>


Senior Software Engineer<u></u><u></u></span></p>
<div>
<div>
<div class="MsoNormal"><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666">
<hr style="width:1.5in" align="left" size="2" width="180">
</span></div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666"><br>
1111 Marcus Avenue<br>
Lake Success, NY 11042<br>
<a href="https://www.dealertrack.com/" target="_blank"><span style="color:#00549f">dealertrack.com</span></a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666">p
</span><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666">516.300.7072</span><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666"><u></u><u></u></span></p>


<p class="MsoNormal"><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666"><br>
</span><b><span style="font-size:10.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#5e6167">dealertrack </span></b><b><span style="font-size:10.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#999b98">technologies</span></b><span style="font-size:7.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#999b98">™</span><span style="font-size:8.5pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#666666"><br>


<br>
</span><span style="font-size:6.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#999b98">Confidentiality Notice: This e-mail and any attachments may contain confidential, proprietary information of Dealertrack Technologies. It is intended solely for the named recipient(s)
 listed above and should be maintained in strictest confidence. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY
 PROHIBITED. If you have received this e-mail in error, please immediately notify the sender and delete this information from your computer and destroy any related paper copies. Unless otherwise expressly stated in the text of the e-mail, the addition of a
 typed name or initials to this e-mail does not (i) evidence an intent to sign the e-mail or (ii) constitute either (a) a signature or (b) consent to use electronic records or signatures in place of a writing or a handwritten signature</span><span style="color:#1f497d"><u></u><u></u></span></p>


<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>

<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>
<br></blockquote></div><br><br clear="all"><br>-- <br>------------------------------<br>Harry J.W. Percival<br>------------------------------<br>Twitter: @hjwp<br>Mobile:  +44 (0) 78877 02511<br>Skype:         harry.percival
</div>