是否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