[英]Mocking python class in unit test and verifying an instance
我正在嘗試對SFTP幫助程序類進行單元測試,該類對pysftp模塊進行了一些調用。 我想模擬來自pysftp的實際網絡調用,因此沒有副作用,只需確保該類使用正確的參數正確調用了基礎SFTP方法。
到目前為止,這是我的代碼的一個簡單示例:
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
我認為它不起作用,因為我需要斷言該函數是在實例上調用的,但是如何獲取在我的方法中使用的pysftp.Connection實例呢?
您可以配置模擬,以返回定義了__enter__
和__exit__
方法的新模擬對象。 例如:
@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_called_with
在你的榜樣assert_called_once_with
。
最終結果:
$ python -m unittest test_sftp_helper.TestSFTPHelper.test_list_files
.
----------------------------------------------------------------------
Ran 1 test in 0.017s
OK
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.