I have the following scripts:
notification.py:
def get_notification(some_data):
data = details["type"]
notification = {"config1":data, "config2":some_data}
return notification
test_notification.py:
from notification import get_notification
import pytest
def test_get_notifications(mocker):
mocker.patch('notification.get_notification',return_value="SUCCESS")
result = get_notification(some_data)
assert result == "SUCCESS"
Instead of returning "SUCCESS" by the mocked function, the original function is getting called.
I tried using @patch.object which also resulted in calling of the original function.
@patch.object(notification, 'get_notification')
def test_get_notifications(get_notification_mock):
get_notification_mock.return_value="SUCCESS"
result = get_notification(some_data)
assert result == "SUCCESS"
This is because once you have imported get_notification
, it does not "belong" to the notification
module but to the test_notification
module.
You have to modify your patch to point to the test_notification:
mocker.patch("test_notification.get_notification", return_value="SUCCESS")
# or
mocker.patch(__name__ + ".get_notification", return_value="SUCCESS")