Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python';s在另一个方法中调用的方法的模拟返回值不';行不通_Python_Unit Testing_Mocking - Fatal编程技术网

python';s在另一个方法中调用的方法的模拟返回值不';行不通

python';s在另一个方法中调用的方法的模拟返回值不';行不通,python,unit-testing,mocking,Python,Unit Testing,Mocking,我想模拟这样的单元测试方法: 获取树测试.py from company.marketing_tree import get_tree class MidNightTests(TestCase): @mock.patch("company.analytics.get_fb_data", autospec=True) def test_first_midnight(self, mock_fb_data): mock_fb_data.return_value = {}

我想模拟这样的单元测试方法:

获取树测试.py

from company.marketing_tree import get_tree

class MidNightTests(TestCase):
 @mock.patch("company.analytics.get_fb_data", autospec=True)
    def test_first_midnight(self, mock_fb_data):
        mock_fb_data.return_value = {}
        get_tree()
from company.analytics import get_fb_data

def get_tree():
    executor = ThreadPoolExecutor(max_workers=2)
    data_caller = executor.submit(get_data)
    info_caller = executor.submit(get_info)

def get_data():
    executor = ThreadPoolExecutor(max_workers=2)
    first_data = exeuctor.submit(get_fb_data)
get_tree.py

from company.marketing_tree import get_tree

class MidNightTests(TestCase):
 @mock.patch("company.analytics.get_fb_data", autospec=True)
    def test_first_midnight(self, mock_fb_data):
        mock_fb_data.return_value = {}
        get_tree()
from company.analytics import get_fb_data

def get_tree():
    executor = ThreadPoolExecutor(max_workers=2)
    data_caller = executor.submit(get_data)
    info_caller = executor.submit(get_info)

def get_data():
    executor = ThreadPoolExecutor(max_workers=2)
    first_data = exeuctor.submit(get_fb_data)
我确实看到
mock\u fb\u data.return\u value={}
被创建为一个mock对象,但是当我调试
get\u data()
方法时,我看到get\u fb\u data是一个函数,而不是一个mock对象


我错过了什么

您需要修补正确的位置。在
get_tree
中,您创建了一个全局名称
get_fb_data
,代码直接使用该名称:

from company.analytics import get_fb_data
您需要修补该名称,而不是原始的
company.analytics.get\u fb\u data
name;修补程序的工作方式是替换一个指向模拟的名称:

class MidNightTests(TestCase):
    @mock.patch("get_tree.get_fb_data", autospec=True)
    def test_first_midnight(self, mock_fb_data):
        mock_fb_data.return_value = {}
        get_tree()
请参阅
unittest.mock
文档的