opensubscriber
   Find in this group all groups
 
Unknown more information…

p : python-list@python.org 25 October 2011 • 7:09PM -0400

Strange classmethod mock behavior
by Fabio Zadrozny

REPLY TO AUTHOR
 
REPLY TO GROUP




I'm trying to mock a classmethod in a superclass but when restoring it
a strange behavior happens in subclasses (tested on Python 2.5)

Anyone knows why this happens? (see test case below for details)
Is there any way to restore that original method to have the original behavior?

import unittest

class Test(unittest.TestCase):

    def testClassmethod(self):
        class Super():
            @classmethod
            def GetCls(cls):
                return cls

        class Sub(Super):
            pass

        self.assertEqual(Sub.GetCls(), Sub)

        original = Super.GetCls
        #Mock Super.GetCls, and do tests...
        Super.GetCls = original #Restore the original classmethod
        self.assertEqual(Sub.GetCls(), Sub) #The call to the
classmethod subclass returns the cls as Super and not Sub as expected!

if __name__ == '__main__':
    unittest.main()
--
http://mail.python.org/mailman/listinfo/python-list

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

Related Messages

opensubscriber is not affiliated with the authors of this message nor responsible for its content.