为什么我的模拟对象无法识别对它的任何调用?

Why does my mock object not recognize any calls to it?

这是一些单元测试,我已将其简化为可重现问题的最小示例。

import unittest
import zipfile
from unittest.mock import patch

"""My 'production' function"""
def list_txt_in_zipfile(zip_file_path):
    with open(zip_file_path, "rb") as file:
        zf = zipfile.ZipFile(file)
        print("zipfile.ZipFile is", zipfile.ZipFile)
    return [f_name for f_name in zf.namelist() if f_name.endswith(".txt")]


class UnzipperTest(unittest.TestCase):

    """My test method"""
    @patch("__main__.zipfile.ZipFile")
    def test_list_txt_in_zipfile(self, mock_ZipFile):
        txt_list = list_txt_in_zipfile("my.txt.zip")
        mock_ZipFile.assert_any_call() # AssertionError
        print("mock_ZipFile is", mock_ZipFile)


if __name__ == '__main__':
    unittest.main()

断言错误:

AssertionError: ZipFile() call not found

当我的测试方法调用 list_txt_in_zipfile 时,它会记录一个模拟对象,而不是真实的 zipfile.Zipfile(来自 Python 的 zipfile.py)。

如果我在我的生产函数中打印那些 "two" 个对象 zipfile.ZipFile 并在我的测试方法中打印 mock_ZipFile,它们是完全相同的对象:

zipfile.ZipFile is <MagicMock name='ZipFile' id='3069297420'>
mock_ZipFile is <MagicMock name='ZipFile' id='3069297420'>

为什么 mock_ZipFile 无法声明对它的任何调用?

我实际上已经通过将 list_txt_in_zipfile 函数分成两个函数(open() 部分和 zipfile.ZipFile() 的其余部分)成功地测试了它,但就目前而言,这不是函数不大,所以我想把这段逻辑放在一个地方。

更新:测试方法中的 my.txt.zip 确实存在,我还没有设法引入 unittest.mock.mock_open 来模拟 open() 因为我被模拟 zipfile.ZipFile.

assert_any_call 断言已使用指定参数 调用模拟对象。您没有在断言中传递任何参数,但在函数本身 zipfile.Zipfile() 中使用一个参数调用文件对象。