Python:运行第二个单元测试

时间:2018-08-31 10:05:17

标签: python python-3.x unit-testing paramiko python-unittest

设置

  1. 当前使用的Python版本 2.使用的软件包是Paramiko和Unitest
  2. 在此测试用例中尚未使用Pytest,但是如果有人在这里可以使用Pytest实施,我将不胜感激。

当前,我正在重写各种测试用例并创建新的测试用例。

在我的示例中,应该发生以下情况:

  • 应该在路由器/交换机上创建具有不同权限的用户
  • 创建了特权为1(权限非常低)的用户“ Testuser 1”
  • 这将执行一个提示命令,由于法律原因他无法执行
    • 如果他可以执行此命令,则测试应该失败。如果他不能做到这一点,则测试为阳性。
    • 执行后,该用户最后要退出交换机/路由器
    • 现在,下一步,将创建另一个用户(起初这没问题
    • 新创建的用户“ Testuser 2”现在应该像“ Testuser 1”一样登录。但是这里涉及到这个问题:

例外情况:

这是运行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 ')   

最后

对我很重要

  • 说明此错误是如何产生的?
  • 还有哪些其他解决方案甚至可以在这里帮助pytest
  • 在这里通过单元测试或Pytest安装测试套件需要付费吗?

0 个答案:

没有答案
相关问题