Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:46:12+00:00 2026-06-15T16:46:12+00:00

I am using the python mock framework for testing (http://www.voidspace.org.uk/python/mock/) and I want to

  • 0

I am using the python mock framework for testing (http://www.voidspace.org.uk/python/mock/) and I want to mock out a superclass and focus on testing the subclasses’ added behavior.

(For those interested I have extended pymongo.collection.Collection and I want to only test my added behavior. I do not want to have to run mongodb as another process for testing purposes.)

For this discussion, A is the superclass and B is the subclass. Furthermore, I define direct and indirect superclass calls as shown below:

class A(object):
    def method(self):
        ...

    def another_method(self):
        ...

class B(A):
    def direct_superclass_call(self):
        ...
        A.method(self)

    def indirect_superclass_call(self):
        ...
        super(A, self).another_method()

Approach #1

Define a mock class for A called MockA and use mock.patch to substitute it for the test at runtime. This handles direct superclass calls. Then manipulate B.__bases__ to handle indirect superclass calls. (see below)

The issue that arises is that I have to write MockA and in some cases (as in the case for pymongo.collection.Collection) this can involve a lot of work to unravel all of the internal calls to mock out.

Approach #2

The desired approach is to somehow use a mock.Mock() class to handle calls on the the mock just in time, as well as defined return_value or side_effect in place in the test. In this manner, I have to do less work by avoiding the definition of MockA.

The issue that I am having is that I cannot figure out how to alter B.__bases__ so that an instance of mock.Mock() can be put in place as a superclass (I must need to somehow do some direct binding here). Thus far I have determined, that super() examines the MRO and then calls the first class that defines the method in question. I cannot figure out how to get a superclass to handle the check to it and succeed if it comes across a mock class. __getattr__ does not seem to be used in this case. I want super to to think that the method is defined at this point and then use the mock.Mock() functionality as usual.

How does super() discover what attributes are defined within the class in the MRO sequence? And is there a way for me to interject here and to somehow get it to utilize a mock.Mock() on the fly?

import mock

class A(object):
    def __init__(self, value):
        self.value = value      

    def get_value_direct(self):
        return self.value

    def get_value_indirect(self):
        return self.value   

class B(A):
    def __init__(self, value):
        A.__init__(self, value)

    def get_value_direct(self):
        return A.get_value_direct(self)

    def get_value_indirect(self):
        return super(B, self).get_value_indirect()


# approach 1 - use a defined MockA
class MockA(object):
    def __init__(self, value):
        pass

    def get_value_direct(self):
        return 0

    def get_value_indirect(self):
        return 0

B.__bases__ = (MockA, )  # - mock superclass 
with mock.patch('__main__.A', MockA):  
    b2 = B(7)
    print '\nApproach 1'
    print 'expected result = 0'
    print 'direct =', b2.get_value_direct()
    print 'indirect =', b2.get_value_indirect()
B.__bases__ = (A, )  # - original superclass 


# approach 2 - use mock module to mock out superclass

# what does XXX need to be below to use mock.Mock()?
#B.__bases__ = (XXX, )
with mock.patch('__main__.A') as mymock:  
    b3 = B(7)
    mymock.get_value_direct.return_value = 0
    mymock.get_value_indirect.return_value = 0
    print '\nApproach 2'
    print 'expected result = 0'
    print 'direct =', b3.get_value_direct()
    print 'indirect =', b3.get_value_indirect() # FAILS HERE as the old superclass is called
#B.__bases__ = (A, )  # - original superclass
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:46:13+00:00Added an answer on June 15, 2026 at 4:46 pm

    I played around with mocking out super() as suggested by kindall. Unfortunately, after a great deal of effort it became quite complicated to handle complex inheritance cases.

    After some work I realized that super() accesses the __dict__ of classes directly when resolving attributes through the MRO (it does not do a getattr type of call). The solution is to extend a mock.MagicMock() object and wrap it with a class to accomplish this. The wrapped class can then be placed in the __bases__ variable of a subclass.

    The wrapped object reflects all defined attributes of the target class to the __dict__ of the wrapping class so that super() calls resolve to the properly patched in attributes within the internal MagicMock().

    The following code is the solution that I have found to work thus far. Note that I actually implement this within a context handler. Also, care has to be taken to patch in the proper namespaces if importing from other modules.

    This is a simple example illustrating the approach:

    from mock import MagicMock
    import inspect 
    
    
    class _WrappedMagicMock(MagicMock):
        def __init__(self, *args, **kwds):
            object.__setattr__(self, '_mockclass_wrapper', None)
            super(_WrappedMagicMock, self).__init__(*args, **kwds)
    
        def wrap(self, cls):
            # get defined attribtues of spec class that need to be preset
            base_attrs = dir(type('Dummy', (object,), {}))
            attrs = inspect.getmembers(self._spec_class)
            new_attrs = [a[0] for a in attrs if a[0] not in base_attrs]
    
            # pre set mocks for attributes in the target mock class
            for name in new_attrs:
                setattr(cls, name, getattr(self, name))
    
            # eat up any attempts to initialize the target mock class
            setattr(cls, '__init__', lambda *args, **kwds: None)
    
            object.__setattr__(self, '_mockclass_wrapper', cls)
    
        def unwrap(self):
            object.__setattr__(self, '_mockclass_wrapper', None)
    
        def __setattr__(self, name, value):
            super(_WrappedMagicMock, self).__setattr__(name, value)
    
            # be sure to reflect to changes wrapper class if activated
            if self._mockclass_wrapper is not None:
                setattr(self._mockclass_wrapper, name, value)
    
        def _get_child_mock(self, **kwds):
            # when created children mocks need only be MagicMocks
            return MagicMock(**kwds)
    
    
    class A(object):
        x = 1
    
        def __init__(self, value):
            self.value = value
    
        def get_value_direct(self):
            return self.value
    
        def get_value_indirect(self):
            return self.value
    
    
    class B(A):
        def __init__(self, value):
            super(B, self).__init__(value)
    
        def f(self):
            return 2
    
        def get_value_direct(self):
            return A.get_value_direct(self)
    
        def get_value_indirect(self):
            return super(B, self).get_value_indirect()
    
    # nominal behavior
    b = B(3)
    assert b.get_value_direct() == 3
    assert b.get_value_indirect() == 3
    assert b.f() == 2
    assert b.x == 1
    
    # using mock class
    MockClass = type('MockClassWrapper', (), {})
    mock = _WrappedMagicMock(A)
    mock.wrap(MockClass)
    
    # patch the mock in
    B.__bases__ = (MockClass, )
    A = MockClass
    
    # set values within the mock
    mock.x = 0
    mock.get_value_direct.return_value = 0
    mock.get_value_indirect.return_value = 0
    
    # mocked behavior
    b = B(7)
    assert b.get_value_direct() == 0
    assert b.get_value_indirect() == 0
    assert b.f() == 2
    assert b.x == 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the mock-0.6 library from http://www.voidspace.org.uk/python/mock/mock.html to mock out a framework for testing,
I am using mock for testing in Python. I am trying to unit test
I'm using python-mock to mock out a file open call. I would like to
Using Python, I want to know whether Java is installed.
Using Python I want to randomly rearrange sections of a string based on a
I installed Python Mock module using PIP. When I try to import mock running
I'm using the latest version of mock and python 2.7.3 I'm building my first
I am using mock with Python and was wondering which of those two approaches
Using Python I want to be able to draw text at different angles using
Using Python's Imaging Library I want to create a PNG file. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.