mardi 26 mai 2020

How to mock a function present inside another function in Python?

I want to mock a function present inside another function like this:

def parent_function():
    def child_function():
        # mock this function
        return something_1

    child_function()  # calling the child function and doing other things 
    return something_2

Here is how I tried to mock it:

@patch('package.parent_function.child_function')
def test_some_method(self, mocked_function):
    # doing testing

It threw the below error:

AttributeError: <function parent_function at 0x7f7d46a2d170> does not have the attribute 'child_function'

What am I doing wrong? and how can I solve this problem.

P.S. Please don't suggest me to modify the main code as I am not allowed to do that, I have to do just the testing.

Aucun commentaire:

Enregistrer un commentaire