如何使用 pytest 模拟导入的模块

How to mock an imported module with pytest

之前有人问过类似的问题,但我正在努力让它发挥作用。

如何从另一个文件模拟导入模块

我有一个文件:
b.py(命名与链接文档一致)

import cv2   # module 'a' in the linked docs

def get_video_frame(path):
    vidcap = cv2.VideoCapture(path)  # `a.SomeClass` in the linked docs
    vidcap.isOpened()
    ...

test_b.py

import b
import pytest # with pytest-mock installed

def test_get_frame(mocker):

    mock_vidcap = mocker.Mock()
    mock_vidcap.isOpened.side_effect = AssertionError

    mock_cv2 = mocker.patch('cv2.VideoCapture')
    mock_cv2.return_value = mock_vidcap

    b.get_video_frame('foo')    # Doesn't fail

    mock_vidcap.isOpened.assert_called()   # fails

我这样设置测试,因为在 where to patch 中它指定如果

In this case the class we want to patch is being looked up on the a module and so we have to patch a.SomeClass instead:

@patch(‘a.SomeClass’)

我已经尝试了一些其他的修补组合,但它表现出相同的行为,这表明我没有成功修补模块。如果要应用补丁,b.get_video_frame('foo') 将因 side_effect 而失败; assert_called 失败,支持这个。

编辑 为了缩短问题的长度,我省略了 get_video_frame 的其余部分。不幸的是,我们遗漏的部分是关键部分。完整的函数是:

def get_video_frame(path):
    vidcap = cv2.VideoCapture(path)  # `a.SomeClass` in the linked docs
    is_open = vidcap.isOpened()
    while True:
        is_open, frame = vidcap.read()
        if is_open:
            yield frame
        else:
            break

这一行只是创建了一个生成器:

b.get_video_frame('foo')

永远不会到达行 is_open = vidcap.isOpened(),因为在测试函数中,生成器在开始时保持冻结状态,因此不会产生副作用。

您在其他方面正确使用了 mocker 和 patch。