模拟 glob 的问题不会调用 for 循环

Problem with mocking glob does not called for for loop

我正在使用 mock 来测试我开发的东西。在应用程序中,我使用 glob 循环查找目录中的内容,例如:'/tmp/*.png'。它将收集目录中的所有 .png 文件和 return 这些文件的列表。

当我模拟 glob 时,它 return 调用。但是,当用于在 for 循环中循环时效果不佳。

#stack.py
import os
import click
import hashlib
import glob

def bar(x):
    return os.path.basename(x)

def foo(path):
    images = glob.glob(path)
    for i in images:
        bar(i)


if __name__ == '__main__':
    foo()

#test_stack.py
import os
import unittest
import mock
import tempfile
import stack


class StackTest(unittest.TestCase):

    temp_dir = tempfile.gettempdir()
    temp_rg3 = os.path.join(temp_dir, "testfile.rg3")

    @mock.patch('stack.os')
    @mock.patch('stack.hashlib')
    @mock.patch('stack.glob')
    def test_stack(self, mock_glob, mock_hashlib, mock_os):
        stack.foo(self.temp_rg3)

        print(mock_glob.method_calls)
        print(mock_os.method_calls)

这是return:

[call.glob('/tmp/testfile.rg3')]
[]
[]

glob.glob(path) 中调用 glob 后,其 return 值不会反映在 images 中。因此 for 循环不会开始并且 bar(i) 不会被调用,因此 mock_os return 没有调用。

如果我理解你的问题,看来你还没有为你的 mock 设置 return 值。

当您生成 MagicMock 对象时,其默认 return 值是模拟实例本身,如 here 所述。此实例不是迭代器,因此在用 for 循环迭代时不会执行任何操作。

您可以提供如下 return 值,将模拟更改为您正在调用的特定函数:

@mock.patch('stack.os')
@mock.patch('stack.hashlib')
@mock.patch('stack.glob.glob', return_value=['a.png', 'b.png', 'c.png'])
def test_stack(self, mock_glob, mock_hashlib, mock_os):
    stack.foo(self.temp_rg3)

    print(mock_glob.method_calls)
    print(mock_os.method_calls)