Вы можете использовать unittest.mock.patch
или pytest-mock
плагин с приспособлением mocker
.
Ваш пакет
pack/another_pack.py
:
from pack import utils
class SomeClass:
def foo(self):
return utils.my_function()
pack/utils.py
:
def my_function():
return 'original'
Тесты
import pytest
from unittest.mock import patch
from pack.another_pack import SomeClass
# Replace my_function with another function. You could pass parameters
# to the mocked function and handle them in the replacement.
@pytest.mark.parametrize("attr", ["a", "b", "c"])
def test_replace(attr):
def mock_return():
return attr
with patch("pack.another_pack.utils.my_function", new=mock_return):
assert SomeClass().foo() == attr
# If you just want to override the return value.
@pytest.mark.parametrize("attr", ["a", "b", "c"])
def test_return_value(attr):
with patch("pack.another_pack.utils.my_function") as my_func:
my_func.return_value = attr
assert SomeClass().foo() == attr
# With the pytest-mock plugin and the mocker fixture instead of unittest.mock.
@pytest.mark.parametrize("attr", ["a", "b", "c"])
def test_mock_plugin(attr, mocker):
my_func = mocker.patch("pack.another_pack.utils.my_function")
my_func.return_value = attr
assert SomeClass().foo() == attr
Обратите внимание, что во всех тестах первым аргументом patch
является имямодуль, в котором вы хотите смоделировать функцию (pack.another_pack
) с именем функции, как она выглядит в модуле (utils.my_function
).
my_function
смоделирована для всего модуля pack.another_pack
.