使用 python、pytest、mock 模拟另一个模块中的第 3 方函数

Joe*_*y O 6 python unit-testing mocking pytest

我有一个函数 func1() 正在生产中且无法修改。它调用另一个模块中的函数 function_to_be_mocked()。这需要输入参数。

我有另一个函数 func2() 调用 func1()。

我正在编写单元测试来测试 func2() ,并尝试模拟 function_to_be_mocked (因为它取决于我本地系统上没有(也不应该有)的一些键)。我唯一可以修改的是test_func2()。

我有如下设置(最小示例):

from othermodule import function_to_be_mocked
import pytest
import mock

def func1():
    function_to_be_mocked(None)

def func2():
    ret = func1()
    print (ret)

@mock.patch('othermodule.function_to_be_mocked', return_value = 3)
def test_func2(mocker):
    func2()
Run Code Online (Sandbox Code Playgroud)

othermodule.py 是:

def function_to_be_mocked(arg1):
    if not arg1 == 'foo':
        raise ValueError
Run Code Online (Sandbox Code Playgroud)

我的输出:

直接调用func2:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/blah/temp.py", line 9, in func2
    ret = func1()
  File "/Users/blah/temp.py", line 6, in func1
    function_to_be_mocked(None)
  File "/Users/blah/othermodule.py", line 3, in function_to_be_mocked
    raise ValueError
ValueError
Run Code Online (Sandbox Code Playgroud)

调用 test_func2() 我希望它会被嘲笑:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/blah/venv/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
    return func(*args, **keywargs)
  File "/Users/blah/temp.py", line 14, in test_func2
    func2()
  File "/Users/blah/temp.py", line 9, in func2
    ret = func1()
  File "/Users/blah/temp.py", line 6, in func1
    function_to_be_mocked(None)
  File "/Users/blah/othermodule.py", line 3, in function_to_be_mocked
    raise ValueError
ValueError
Run Code Online (Sandbox Code Playgroud)

所以模拟似乎不起作用。有谁有任何想法如何实现这一目标?

============ 在本行下方编辑 ===========

听起来我不能做我认为可以做的事情(因为事实上我无法修改与功能 1 或 2 相关的任何内容。我所能控制的只是测试。

让我提出以下问题,因为也许比我更有经验的眼睛可以看到前进的方向。

我有一个功能:

def function_to_be_tested(args):
    # Some processing steps

    # Function call that works locally
    function_that_returns_something_1()

    # Some logic

    # Function call that works locally
    function_that_returns_something_2()

    # Function that raises an exception when running locally,
    # and since I need to test the logic after this function 
    # (and cannot edit this code here to bypass it) I would 
    # (naively) like to mock it.
    function_I_would_like_to_mock()

    # Much more logic that follows this function. 
    # And this logic needs to be unit tested.
    return some_value_based_on_the_logic
Run Code Online (Sandbox Code Playgroud)

测试:

def test_function_to_be_tested():
    assert function_to_be_tested(args) == some_expected_value
Run Code Online (Sandbox Code Playgroud)

我可以在 function_I_would_like_to_mock() 之前轻松地对任何内容进行单元测试。

但由于这个函数在本地崩溃(并且我无法编辑代码以阻止它在本地崩溃),我觉得正确的方法是模拟它并强制返回一个合理的返回值。这样我就可以对除此之外的代码路径进行单元测试。

您建议什么好方法?

请注意,我唯一可以修改的是测试功能。我什至无法向主函数添加装饰器。

Pab*_*ski 4

选项A)

您愿意模拟的函数已加载到 func1 中。因此你必须将 @patch 装饰器应用到 f​​unc1

import pytest
from unittest import mock

@mock.patch('othermodule.function_to_be_mocked', return_value = 3)
def func1(mocker):
    from othermodule import function_to_be_mocked
    function_to_be_mocked(None)

def func2():
    ret = func1()
    print (ret)

def test_func2():
    func2()

test_func2()
Run Code Online (Sandbox Code Playgroud)

========编辑============

选项B)

import pytest
from unittest import mock


def func1():
    from othermodule import function_to_be_mocked
    function_to_be_mocked(None)

def func2():
    ret = func1()
    print (ret)

def test_func2():
    with mock.patch('othermodule.function_to_be_mocked', return_value = 3) as irrelevant:
        func2()

test_func2()
Run Code Online (Sandbox Code Playgroud)