My python file has many methods defined in it. In many of the methods in the file, I am calling a function - let's say "fun1".
I want to mock the 'fun1' function for each of the methods differently. Is there a way to patch an object differently at method level and not at file level?
Contents of aPythonFile.py
=======================================
import fun from a_module
def a():
res= fun(aarg1, aarg2)
return res
def b():
res = fun(barg1, barg2)
return res
This came to my mind, as I know, the same "fun" can be patched differently for different files, then why not be able to patch differently based on the method in the file where it is called :-
@mock.patch("package.aPythonFile.fun") - Is Valid
@mock.patch("package.aPythonFile.a.fun") - ?? (possible?)
@mock.patch("package.aPythonFile.b.fun") - ?? (possible?)
@mock.patchapplies to an individual test method, so if you have atest_aandtest_b, they can be patched with different methods. You do realize one of the answers in that thread literally has the mocking done at the method level, you can just change the argument passed toside_effectto the method you specifically require for the specific test...aPythonFile.pyof yours. Your edited code has only make your question even more confusing. You need to show clearly what you are doing. If you want different return values forfun1that were called throughout an execution you may wish to refer to this thread. In any case please clean up your question because as it is now is quite unclear on what you are asking, but at least you made it not obviously a duplicate of the other question, though as is it is still unanswerable.