设置
当前,我正在重写各种测试用例并创建新的测试用例。
在我的示例中,应该发生以下情况:
例外情况:
这是运行2测试后的异常。第二个用户的登录,显然该用户已被我的路由器和交换机阻止。
Exception: Error reading SSH protocol banner Traceback (most recent call last):
File "C:\Python37\lib\site-packages\paramiko\transport.py", line 2044, in
_check_banner
buf = self.packetizer.readline(timeout)
File "C:\Python37\lib\site-packages\paramiko\packet.py", line 353, in
readline
buf += self._read_timeout(timeout)
File "C:\Python37\lib\site-packages\paramiko\packet.py", line 542, in
_read_timeout
raise EOFError() EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Python37\lib\site-packages\paramiko\transport.py", line 1893, in
run
self._check_banner()
File "C:\Python37\lib\site-packages\paramiko\transport.py", line 2049, in
_check_banner
'Error reading SSH protocol banner' + str(e) paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
connection negativ
---------------------------------------------------------------------- Ran 4 tests in 6.072s
OK
在这里您可以看到完整的测试 简短信息:testcore是使用Paramiko complete的特殊特殊软件包
import paramiko
import SSH
import unittest
from test import support
class SwitchAccount(unittest.TestCase):
def setUp(self):
self.s = testcore.control.ssh.SSH(host='xxx.xx.xx.xxx', username='admin', password='admin')
if self.s.login():
print('connection succesfull')
else:
print('connection negativ')
self.s.query_interactive = True
def test_change_Enforce_Enable(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('enforce-Password-Rules yes')
def test_create_user_rights_1(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('add 1 testuser_P1 testuser_P1')
q = self.s.query('logout')
def setUp(self):
self.s = testcore.control.ssh.SSH(host='xxx.xx.xx.xxx', username='testuser_P1', password='testuser_P1')
if self.s.login():
print('connection succesfull')
else:
print('connection negativ')
import time
print('Wait')
time.sleep(6)
def test_try_wrong_promp_command(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('aaa')
q = self.s.query('trace ')
import time
print('Wait')
time.sleep(6)
def test_create_user_rights_2(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('add 2 testuser_P2 testuser_P2')
import time
print('Wait')
time.sleep(6)
q = self.s.query('logout')
def setUp(self):
self.s = testcore.control.ssh.SSH(host='xxx.xx.xx.xxx', username='testuser_P2', password='testuser_P2')
if self.s.login():
print('connection succesfull')
else:
print('connection negativ')
def test_try_wrong_promp_command(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('aaa')
q = self.s.query('trace ')
if __name__ == '__main__':
unittest.main(verbosity=3)
unittest.main(warnings='ig-nore')
log_file = 'log_file.txt'
f = open(log_file, "w")
为了获得更好的概述,这里再次详细解释
使用的库
import Paramiko
import SSH
import unittest
from test import support
单元测试
class SwitchAccount(unittest.TestCase):
设置SSH
def setUp(self):
self.s = testcore.control.ssh.SSH(host='xxx.xx.xx.xxx', username='admin', password='admin')
if self.s.login():
print('connection succesfull')
else:
print('connection negativ')
self.s.query_interactive = True
第一次单元测试
def test_create_user_rights_1(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('add 1 testuser_P1 testuser_P1')
q = self.s.query('logout')
def setUp(self):
self.s = testcore.control.ssh.SSH(host='xxx.xx.xx.xxx', username='testuser_P1', password='testuser_P1')
if self.s.login():
print('connection succesfull')
else:
print('connection negativ')
import time
print('Wait')
time.sleep(6)
def test_try_wrong_promp_command(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('aaa')
q = self.s.query('trace ')
import time
print('Wait')
time.sleep(6)
短暂休息:
到目前为止,该测试工作正常,包括在路由器/交换机上创建用户。
第二次单元测试 现在我们进行第二次测试,并在这里引发异常。这甚至导致我什至无法登录路由器,而只有冷重启才有用。
def test_create_user_rights_2(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('add 2 testuser_P2 testuser_P2')
import time
print('Wait')
time.sleep(6)
q = self.s.query('logout')
def setUp(self):
self.s = testcore.control.ssh.SSH(host='xxx.xx.xx.xxx', username='testuser_P2', password='testuser_P2')
if self.s.login():
print('connection succesfull')
else:
print('connection negativ')
def test_try_wrong_promp_command(self):
if self.s.login():
q = self.s.query('account')
# switch to prompt account
q = self.s.query('aaa')
q = self.s.query('trace ')
最后
对我很重要