I'm trying to mock a function that's imported from a library which is renamed.
The example
mylibrarywithlongname:
def helloworld():
return "hello world"
def helloworld_helper():
return helloworld()
Main program:
import mylibrarywithlongname as ml
from mock import MagicMock
def test():
ml.helloworld = MagicMock(return_value="oops")
print(ml.helloworld_helper())
print(ml.helloworld_helper())
test()
print(ml.helloworld_helper())
This returns
hello world
oops
oops
I'm trying to find the syntax to only mock within the test without having to copy the function and restore it manually.
The third line should return the original "hello world"
For this example, I'm using python 2.7 (because I try to mock an old project)
My attempt:
from mock import MagicMock, patch
@patch(ml.helloworld, MagicMock(return_value="oops"))
def test():
print(ml.helloworld_helper())
fails with the error
AttributeError: 'function' object has no attribute 'rsplit'
Aucun commentaire:
Enregistrer un commentaire