[TIP] Unit testing functions which call super

Marcin Tustin Marcin.Tustin at dealertrack.com
Wed Aug 13 11:00:43 PDT 2014


Hi All,

I've come across a method which I needed to add a unit test for, which calls super, e.g.

def foofunc(self):
    x= do_some_stuff()
    if x:
        return x
    return super(WhatevClass, self).foofunc()

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.

I've accomplished this with, in my test (excuse the 8 leading spaces):

def setUp(self):
        self.method_archive = {}
        for klass in WhatevClass.__mro__[1:]:
            # we use the class as its own sentinel
            meth_or_klass = klass.__dict__.get('foofunc', klass)
            if meth_or_klass != klass:
                self.method_archive[klass] = meth_or_klass
                del klass.foofunc
        # now patch an arbitrary member of the mro to supply a dummy foofunc
        self.sentinel_klass = next(iter(self.method_archive))
        self.mock_sentinel = mock.MagicMock()
        self.sentinel_klass.foofunc = self.mock_sentinel


And corresponding unpatching in tearDown.

Is there any other approach which is standard, or objectively better?

Thanks,
Marcin

Marcin Tustin
Senior Software Engineer
________________________________


1111 Marcus Avenue
Lake Success, NY 11042
dealertrack.com<https://www.dealertrack.com/>
p 516.300.7072

dealertrack technologies(tm)

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140813/f469b784/attachment.htm>


More information about the testing-in-python mailing list