是否可以在Python testcase中的功能补丁功能



这是一个示例。

main/sosings.py

from example.something import get_utc_time, get_jst_time
print(get_utc_time())
print(get_jst_time())

示例/someings.py

from django.utils import timezone
def get_utc_time():
    return timezone.now()
def get_jst_time():
    return timezone.now() + timezone.timedelta(hours=9)

我想喜欢跟随测试柜。但是,这是不可用的。
有人有想法吗?

testcase

@patch('main.something.example.something.timezone.now')
def test_execute(mock_now):
    ....

我必须将两个函数设置为补丁,例如:

@patch('main.something.get_utc_time')@patch('main.something.get_jst_time')

您需要在要更改的行为的事物的命名空间中进行修补。在这种情况下,您可能需要:

@patch('example.something.timezone.now')
def test_execute(mock_now):
    mock_now.return_value = 'a mock time'  # probably want to return a time not a string

最新更新