Issue
I am running a execfile() on python script but in this script there are calls to modules that I don't have. I would therefore like to replace them with fakeObject/mock
I have this instruction:
import mymodule.mtest.core as CCORE
API = CCORE.object()
API.initialize(sys.argv, comm=comm)
But i dont have mymodule.mtest.core and i want to replace CCORE.object() with a fake object containing the initialize() method
I tried someting like this:
sys.modules['mymodule'] = MyfakeObj
With MyFakeObj is a python module with a fake def initialiaze() method
But i have this error
ModuleNotFoundError: No module named 'mymodule.mtest'; 'mymodule' is not a package
How can i do this ?
Solution
thank you for your help. I tested your proposal but in my context the instructions
ith patch.object(mymodule.mtest.core, "object", return_value=Mock()):
does not works,
on the other hand it put me on the track to use Mock()
I use like this:
sys.modules['mymodule'] = MagicMock()
sys.modules['mymodule.mtest'] = MagicMock()
sys.modules['mymodule.mtest.core'] = MagicMock()
And i can exec my script with the instructions
import mymodule.mtest.core
the import no longer crashes
Answered By - jeyzorus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.