我有一个按预期工作的模拟。
from mock import patch
def second(arg):
return 3
def first():
return second('arg')
@patch('test.second')
def test_test(second_mock):
second_mock.return_value = 47 # We decide this
call_it = first()
second_mock.assert_called_once()
second_mock.assert_called_with('arg')
assert call_it == 47
但如果我将我的 second() 方法移动到另一个文件,则不会......
from mock import patch
from test_help import second
def first():
return second('arg')
@patch('test_help.second')
def test_test(second_mock):
second_mock.return_value = 47 # We decide this
call_it = first()
second_mock.assert_called_once()
second_mock.assert_called_with('arg')
assert call_it == 47
我得到了同样的错误: AssertionError: Expected 'second' to have been called once。调用 0 次。
我在这里想念什么?
我尝试了几种不同的格式化方式,但似乎都没有。在这种情况下,这甚至是单元测试的最佳实践/包吗?