[英]Unable to mock a python function return value using patch
我有 2 個文件和一個測試文件。 由於某種未知的原因,我無法讓它通過。 這個測試總是失敗,因為它仍然返回 'hello_world' 而不是使用 'goodbye_world' 的模擬值。
實用程序.py
def hello_world():
return "hello_world"
foo.py
from utils import hello_world
class Foo:
def function_a():
return hello_world()
測試.py
import foo
from unittest.mock import patch
from unittest import TestCase
class TestStuff(TestCase):
@patch('foo.hello_world')
def test_hello_world(mock_value):
mock_value.return_value = 'goodbye_world'
temp = foo.Foo()
self.assertEqual(temp.function_a(), 'goodbye_world')
哦,我想通了。 我應該從 src 目錄進行修補。 所以它應該是'src.foo.hello_world'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.