Python单元测试模拟函数调用原始函数并失败。

huangapple go评论78阅读模式
英文:

Python Unit Testing a mocked function calls the original function and fails

问题

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"
英文:

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"

答案1

得分: 2

这是因为一旦你导入了get_notification,它不再"属于" notification 模块,而是属于 test_notification 模块。
你需要修改你的补丁来指向 test_notification

mocker.patch("test_notification.get_notification", return_value="SUCCESS")
# 或者
mocker.patch(__name__ + ".get_notification", return_value="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")

答案2

得分: 0

以下是示例(debug.py)的翻译:

from unittest.mock import patch


def get_notification():
    return 'failed'


def test_get_notifications():
    with patch(
        # change 'debug' to your module name('notification')...
        'debug.get_notification',
        return_value='SUCCESS'
    ):
        assert get_notification() == "SUCCESS"

运行 pytest debug.py

===================================================================== 测试会话开始 ======================================================================
平台:Linux -- Python 3.10.6,pytest-7.4.0,pluggy-1.2.0
根目录:项目路径...
收集到 1 个项目                                                                                                                                                 

debug.py .                                                                                                                                               [100%]

====================================================================== 1 个测试通过,用时 0.01 秒 ======================================================================
英文:

Here is an example(debug.py):

from unittest.mock import patch


def get_notification():
    return 'failed'


def test_get_notifications():
    with patch(
        # change 'debug' to your module name('notification')...
        'debug.get_notification',
        return_value='SUCCESS'
    ):
        assert get_notification() == "SUCCESS"

Run pytest debug.py:

===================================================================== test session starts ======================================================================
platform linux -- Python 3.10.6, pytest-7.4.0, pluggy-1.2.0
rootdir: PATH TO PROJECT...
collected 1 item                                                                                                                                               

debug.py .                                                                                                                                               [100%]

====================================================================== 1 passed in 0.01s =======================================================================

huangapple
  • 本文由 发表于 2023年7月24日 16:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定