How to mock a protected/private method in a tested method?

michael-mammut picture michael-mammut · Aug 8, 2017 · Viewed 8.2k times · Source

I have a Python Clas with 2 methods.

The first, _getTemperature_() is protected and the second one is a public method. I have to write a unitTest but I have no clue, how to mock the protected method? I just found tutorials to mock a public method which is used at the test direct. But not in a method which sould be tested.

class StateOn(self):

    #Temperature in °C
    _target = 15

    # currrent Temperature by Sensor in °C
    def _getTemperature_(self):
        return valueFromSensorXY()


    def validateTemperature(self):
        if self._getTemperature_() >= self._target:
            return StateOff()

Does someone can give me a hint, or an explaination to find a solution about this issue?

Answer

Renato Byrro picture Renato Byrro · Jan 13, 2021

A bit late to the party, but here's an alternative method:

from unittest import mock

class MyClass():
    def __my_method(self):
        # Implement logic here...
        pass

def test_my_class():
    my_class = MyClass()
    my_class._MyClass__my_method = mock.Mock()
    # Run testing stuff...

By setting my_class._MyClass__my_method to mock.Mock() we effectively have a mock in place of the private method.

Note that we need to set my_class._MyClass__my_method and not my_class.__my_method, due to Python's name mangling of private attributes.