Skip to content
Advertisement

How to mock a function that is not part of any class using monkeypatch in Python?

Consider a function:

JavaScript

and it’s mock function:

JavaScript

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

If some_function is inside another file, you need to import this file to your current module:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement