Monkey patching

Hello ,
I’m wondering how i can make monkey patching for a specific part of my function , for example

foo():
     print()
     another logic and so on

how i can patch specific part of my function logic ? does anyone has an idea

Thank you.

Standard disclaimer - monkey patching is generally a bad idea and probably not the best solution to your problem.

We’re going to need to have more context to be able to answer your question.

Hello Jeremy,
i have a function that calls endpoint , and i’m writing test for it and in the testing environment i want to mock the request.post() and return a specific response to proceed my test.

That’s still not really much to go off of. Do you have a simplified minimal example demonstrating what you are trying to do?

oki , i’m working on django project , i have in the views a function that i should write a test case for it , the function take params and some processing then it makes external api call ,
for example ,

def do_something(identifier):
logic …
response = requests.post(url,data,)

i want to make monkey patching for this line only response = requests.post(url,data,)
as i’m using unittest mock patch decorator and i do want to duplicate the function
to write the test with mock like the below

@patch(‘views’,‘mock_do_something’)
test_doing_something()

so i want to find a way to get rid of this line response = requests.post(url,data,) and get the same functionality without it in the testing environment