I have a function, Foo, and already used in Bar and some other places in the Python package. And for the unit test, I need to replace that function with fake_Foo. The fake_Foo should override every previous instance. How to do that?
I have tried Foo = fake_Foo and global Foo but it does not work.
TL;DR: I am trying to override _command function here with fake_command function here for every previous instance.
CodePudding user response:
I did it with:
from _pytest.monkeypatch import MonkeyPatch
def fake_Foo(monkeypatch=MonkeyPatch()):
def fake__Foo(args):
...
monkeypatch.setattr("module.Foo", fake__Foo)
Just call fake_Foo() when you need to override.
