在单元测试中模拟python类并验证实例
我正在尝试对一个SFTP助手类进行单元测试,该类对PySTFP模块进行一些调用。我想模拟pysftp的实际网络调用,这样就不会有任何副作用,只需确保该类使用正确的参数正确调用底层SFTP方法即可 以下是迄今为止我的代码的一个简单示例:在单元测试中模拟python类并验证实例,python,unit-testing,mocking,pysftp,Python,Unit Testing,Mocking,Pysftp,我正在尝试对一个SFTP助手类进行单元测试,该类对PySTFP模块进行一些调用。我想模拟pysftp的实际网络调用,这样就不会有任何副作用,只需确保该类使用正确的参数正确调用底层SFTP方法即可 以下是迄今为止我的代码的一个简单示例: import pysftp import unittest import mock class SFTPHelper(object): def __init__(self, host, username, password, files_dir):
import pysftp
import unittest
import mock
class SFTPHelper(object):
def __init__(self, host, username, password, files_dir):
self.host = host
self.username = username
self.password = password
self.files_dir = files_dir
def list_files(self):
with pysftp.Connection(
self.host,
username=self.username,
password=self.password) as sftp:
return sftp.listdir(self.files_dir)
class TestSFTPHelper(unittest.TestCase):
@mock.patch('pysftp.Connection')
def test_list_files(self, mock_connection):
sftp_helper = SFTPHelper('somehost', 'someuser', 'somepassword', '/some/files/dir')
sftp_helper.list_files()
# this assertion passes
mock_connection.assert_called_with(
'somehost', password='somepassword', username='someuser')
# this assertion does not pass
mock_connection.listdir.assert_called_with('/some/files/dir')
断言错误:
AssertionError: Expected call: listdir('/some/files/dir')
Not called
我假设它不起作用,因为我需要声明函数是在实例上调用的,但是我如何获取我的方法中使用的Pystp.Connection的实例?您可以配置mock以返回一个新的mock对象,其中定义了
\uuuuuuu enter\uuuuuu
和\uuuuuuuuuuu退出\uu
方法。例如:
@mock.patch.object(
target=pysftp,
attribute='Connection',
autospec=True,
return_value=mock.Mock(
spec=pysftp.Connection,
__enter__=lambda self: self,
__exit__=lambda *args: None
)
)
def test_list_files(self, mock_connection):
# (contents of test case)
此外,您可能需要使用:
mock_connection.return_value.listdir.assert_called_with('/some/files/dir')
而不是:
mock_connection.listdir.assert_called_with('/some/files/dir')
作为旁注,您还可以将示例中的assert\u called\u的两种用法都替换为assert\u called\u once\u,替换为
最终结果是:
$ python -m unittest test_sftp_helper.TestSFTPHelper.test_list_files
.
----------------------------------------------------------------------
Ran 1 test in 0.017s
OK
您可以将模拟配置为返回一个新的模拟对象,并定义了\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
方法。例如:
@mock.patch.object(
target=pysftp,
attribute='Connection',
autospec=True,
return_value=mock.Mock(
spec=pysftp.Connection,
__enter__=lambda self: self,
__exit__=lambda *args: None
)
)
def test_list_files(self, mock_connection):
# (contents of test case)
此外,您可能需要使用:
mock_connection.return_value.listdir.assert_called_with('/some/files/dir')
而不是:
mock_connection.listdir.assert_called_with('/some/files/dir')
作为旁注,您还可以将示例中的assert\u called\u的两种用法都替换为assert\u called\u once\u,替换为
最终结果是:
$ python -m unittest test_sftp_helper.TestSFTPHelper.test_list_files
.
----------------------------------------------------------------------
Ran 1 test in 0.017s
OK
1.你能给出一个比“不起作用”更清晰的问题描述吗?2.您试图模拟一个上下文管理器,您应该仔细阅读它。@jonrsharpe用断言更新了它error@dnit13只是stackoverflow问题中的一个输入错误,我是在实际代码中这样做的。fixed@SpencerWood尝试在完成with
后返回?1。你能给出一个比“不起作用”更清晰的问题描述吗?2.您试图模拟一个上下文管理器,您应该仔细阅读它。@jonrsharpe用断言更新了它error@dnit13只是stackoverflow问题中的一个输入错误,我是在实际代码中这样做的。fixed@SpencerWood使用
完成后是否尝试返回?