我正在为项目构建一个netmiko包装器,并且我试图编写一些简单的单元测试来验证功能。但是我在嘲笑netmiko库时遇到了麻烦。我能够很轻松地模拟ConnectHandler对象的创建,但是验证处理程序的方法是否按预期方式工作。
在下面的代码中,您将看到我的简单包装和单元测试。问题在于test_disconnect。我试图验证断开连接方法是否被调用,但是由于某种原因,模拟正在报告未调用该方法。我确定这与我嘲笑netmiko的方式有关,但是我不确定自己在做什么错。
MockSandbox.py
from netmiko import ConnectHandler, NetMikoTimeoutException
class SshLib(object):
def __init__(self, ip, user, passwd, port=22):
self.ip = ip
self.user = user
self.passwd = passwd
self.port = port
self.connection = None
def login(self):
args = {"device_type": "cisco_ios",
"ip": self.ip,
"username": self.user,
"password": self.passwd,
"port": self.port}
try:
self.connection = ConnectHandler(**args)
return True
except NetMikoTimeoutException:
return False
def disconnect(self):
self.connection.disconnect()
MockSandboxTests.py
import mock
import unittest
from MockSandbox import SshLib
class SshLibTests(unittest.TestCase):
@mock.patch("MockSandbox.ConnectHandler")
def test_connect(self, mock_ssh):
ssh = SshLib("1.1.1.1", "user", "password")
return_val = ssh.login()
args = {"device_type": "cisco_ios",
"ip": "1.1.1.1",
"username": "user",
"password": "password",
"port": 22}
mock_ssh.assert_called_with(**args)
self.assertTrue(return_val)
@mock.patch("MockSandbox.ConnectHandler")
def test_disconnect(self, mock_ssh):
ssh = SshLib("1.1.1.1", "user", "password")
ssh.login()
ssh.disconnect()
mock_ssh.disconnect.assert_called()
if __name__ == '__main__':
unittest.main()
测试输出
Failure
Traceback (most recent call last):
File "C:\Python27\lib\unittest\case.py", line 329, in run
testMethod()
File "C:\Python27\lib\site-packages\mock\mock.py", line 1305, in patched
return func(*args, **keywargs)
File "C:\Users\user\Python\MockSandboxTests.py", line 26, in test_disconnect
mock_ssh.disconnect.assert_called()
File "C:\Python27\lib\site-packages\mock\mock.py", line 906, in assert_called
raise AssertionError(msg)
AssertionError: Expected 'disconnect' to have been called.
Ran 2 tests in 0.020s
FAILED (failures=1)
答案 0 :(得分:1)
mock.patch("MockSandbox.ConnectHandler")
返回该类的模拟,因此
mock_ssh.disconnect
是一个类方法。您想改为检查实例方法。通过mock_ssh.return_value
访问模拟实例:
mock_ssh.return_value.disconnect.assert_called()