Changing a Class’s Base Class for Unit Testing

What will you learn?

In this tutorial, you will learn how to dynamically change a Python class’s base class for the purpose of unit testing. This technique allows you to inject mock behaviors or attributes without the need to create additional subclasses.

Introduction to the Problem and Solution

When writing tests for classes in Python, there are often situations where we need to simulate specific behaviors or attributes of the class being tested. While creating subclasses with overridden methods or attributes is a common approach, it can be cumbersome and lacks flexibility.

A more dynamic solution involves altering the base class of our target class during runtime. This dynamic adjustment enables us to introduce mock behaviors or attributes seamlessly without the overhead of creating multiple subclasses.


class TestClass(BaseClass):
    # Test methods here

# Dynamically change the base class for unit testing
TestClass.__bases__ = (MockBaseClass,)

# Copyright PHD

Note: For more comprehensive examples and insights, visit


In Python, classes are objects themselves, possessing attributes similar to any other object. The __bases__ attribute of a class stores its base classes as a tuple. By directly modifying this attribute, we can alter the inheritance hierarchy of a given class.

In the provided code snippet: – Define a TestClass inheriting from BaseClass. – Dynamically adjust the base classes of TestClass by assigning (MockBaseClass,) to its __bases__ attribute. – This modification effectively replaces BaseClass with MockBaseClass, enabling us to test our code with different behaviors injected through MockBaseClass.

This technique proves valuable in scenarios where conventional mocking frameworks may be overly complex or when precise control over mocks is necessary at runtime.

    How does changing a base class differ from traditional mocking libraries?

    Changing the base class at runtime offers more precise control over replacing specific aspects of the original implementation without relying on external frameworks.

    Can I revert back to the original base class after testing?

    Yes, you can retain a reference to the original base classes before modification and restore them after completing testing.

    Are there any performance implications of dynamically changing base classes?

    Modifying attributes like __bases__ directly may incur some performance overhead compared to traditional inheritance but is typically insignificant unless excessively utilized in critical code sections.

    Can I use this technique with built-in Python classes?

    Yes, this method can be applied even with built-in Python classes if required for your testing needs.

    Is it considered good practice to modify core attributes like __bases__ directly?

    While caution should be exercised due to potential side effects and readability concerns, direct manipulation can be acceptable in controlled environments such as testing scenarios.

    How does changing bases affect method resolution order (MRO)?

    The method resolution order adheres to C3 linearization rules even after dynamically altering bases since MRO relies on both static and dynamic factors during object creation rather than post-initialization modifications.


    By dynamically adjusting a Python class’s base at runtime, you gain flexibility and precision when crafting unit tests that demand customized behavior or state manipulation. While this approach empowers you with fine-grained control in mocking scenarios, it’s advisable to use it thoughtfully alongside conventional techniques based on your specific requirements.

    Leave a Comment