2

是否pytest提供诸如unittest.mock检查模拟是否实际调用一次(或使用某些参数一次)之类的功能?

示例源代码:

my_package/my_module.py

from com.abc.validation import Validation


class MyModule:
    def __init__(self):
        pass

    def will_call_other_package(self):
        val = Validation()
        val.do()

    def run(self):
        self.will_call_other_package()

上述源代码的示例测试代码:

test_my_module.py

import pytest
from pytest_mock import mocker

from my_package.my_module import MyModule

@pytest.fixture
def mock_will_call_other_package(mocker):
    mocker.patch('my_package.my_module.will_call_other_package')


@pytest.mark.usefixtures("mock_will_call_other_package")
class TestMyModule:

    def test_run(self):
        MyModule().run()
        #check `will_call_other_package` method is called.

        #Looking for something similar to what unittest.mock provide
        #mock_will_call_other_package.called_once

4

3 回答 3

4

如果要使用进行修补的夹具,可以将修补移动到夹具中:

import pytest
from unittest import mock

from my_package.my_module import MyModule

@pytest.fixture
def mock_will_call_other_package():
    with mock.patch('my_package.my_module.will_call_other_package') as mocked:
        yield mocked
    # the mocking will be reverted here, e.g. after the test


class TestMyModule:

    def test_run(self, mock_will_call_other_package):
        MyModule().run()
        mock_will_call_other_package.assert_called_once()

请注意,您必须在测试中使用fixture 参数。仅使用@pytest.mark.usefixtures不会让您访问模拟本身。如果您不需要在所有测试中访问模拟(或autouse=True在夹具中使用),您仍然可以使用它在类中的所有测试中有效。

另请注意,您在这里不需要pytest-mock- 但正如@hoefling 所提到的,使用它可以使夹具更具可读性,因为您不需要该with子句:

@pytest.fixture
def mock_will_call_other_package(mocker):
    yield mocker.patch('my_package.my_module.will_call_other_package')

顺便说一句:您不需要导入mocker. 固定装置按名称查找,如果安装了相应的插件,它会自动可用。

于 2021-04-24T07:08:21.737 回答
1

你可以试试这个:

import pytest

from my_package.my_module import MyModule

def test_run(mocker):
    mocker.patch('my_package.my_module.will_call_other_package')
    MyModule().run()
    mock_will_call_other_package.assert_called_once()
于 2021-04-23T16:14:02.320 回答
1

首先,您可能不需要外部库的负担,例如 pytest_mock,因为使用与 unittestpytest的集成已经涵盖了您。

您也不需要使用 ,usefixtures因为每当您需要一个夹具时,您只需在您的测试方法中接收它。

基于您自己的代码的理想方案类似于以下内容:

import pytest
from unittest.mock import patch

from com.abc.validation import Validation


class MyModule:
    def __init__(self):
        pass

    def will_call_other_package(self):
        val = Validation()
        val.do()

    def run(self):
        self.will_call_other_package()


@pytest.fixture
def call_other_module():
    with patch("my_package.my_module.MyModule.will_call_other_package") as _patched:
        yield _patched


class TestMyModule:
    def test_run_will_call_other_package(self, call_other_module):
        call_other_module.assert_not_called()
        obj = MyModule()
        call_other_module.assert_not_called()
        obj.run()
        call_other_module.assert_called_once()

而且,如果您想确保确实修补了目标MyModule.will_call_other_package,请像这样修改您的测试:

class TestMyModule:
    def test_run_will_call_other_package(self, call_other_module):
        call_other_module.assert_not_called()
        obj = MyModule()
        call_other_module.assert_not_called()
        obj.run()
        call_other_module.assert_called_once()
        assert False, (MyModule.will_call_other_package, call_other_module)

你会看到类似的东西:

AssertionError: (<MagicMock name='will_call_other_package' id='140695551841328'>, <MagicMock name='will_call_other_package' id='140695551841328'>)

如您所见id,两个对象的 相同,确认我们的实验是成功的。

于 2021-04-25T06:29:48.927 回答