Consider a function:
JavaScript
x
3
1
def some_function():
2
print("some function")
3
and it’s mock function:
JavaScript
1
3
1
def some_function_dummy():
2
print("some function")
3
How do I mock some_function()
function using monkeypath.setattr()
?
Something similar to:
monkeypatch.setattr(<class name>, "some_function", dummy_function)
Not sure what <class name>
here should be.
Advertisement
Answer
obj
should be the current module:
JavaScript
1
7
1
import sys
2
3
# Get the current module
4
this = sys.modules[__name__]
5
6
monkeypatch.setattr(this, "some_function", dummy_function)
7
If some_function
is inside another file, you need to import this file to your current module:
JavaScript
1
4
1
import another_file
2
3
monkeypatch.setattr(another_file, "some_function", dummy_function)
4