我正在尝试使用pytest和pytest_mock
运行以下测试def rm(filename):
helper(filename, 5)
def helper(filename):
pass
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file', 5)
但我得到例外AttributeError: 'function' object has no attribute 'assert_called_once_with'
我做错了什么?
答案 0 :(得分:6)
您无法在 vanilla 函数上执行.assert_called_once_with
函数:首先需要使用mock.create_autospec
装饰器将其包装起来。例如:
import unittest.mock as mock
def rm(filename):
helper(filename, 5)
def helper(filename):
pass
helper = mock.create_autospec(helper)
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file', 5)
或更优雅:
import unittest.mock as mock
def rm(filename):
helper(filename, 5)
@mock.create_autospec
def helper(filename):
pass
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file', 5)
请注意,断言将失败,因为您只能使用'file'
调用它。所以有效的测试将是:
import unittest.mock as mock
def rm(filename):
helper(filename, 5)
@mock.create_autospec
def helper(filename):
pass
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file')
编辑:如果在某个模块中定义了该函数,则可以在本地将它包装在装饰器中。例如:
import unittest.mock as mock
from some_module import some_function
some_function = mock.create_autospec(some_function)
def test_unix_fs(mocker):
some_function('file')
some_function.assert_called_once_with('file')
答案 1 :(得分:1)
在面向对象案例中:
class Foo:
def rm(self, filename):
self.helper(filename, 5)
def helper(self, filename, number):
pass
def test_unix_fs(mocker):
mocker.patch.object(Foo, 'helper')
foo = Foo()
foo.rm('file')
helper.assert_called_once_with('file', 5)