S
S
Sergey2016-10-03 06:14:31
Python
Sergey, 2016-10-03 06:14:31

How to write unit tests for telnet in python?

Hello! I continue to comprehend python and came to the conclusion that tests - it really would probably be convenient with frequent rewriting / adding functions. But here's the question: I have, for example, some kind of microproject to automate some routine tasks ( https://github.com/sergkondr/noc_automate). It has methods for connecting to different devices via Telnet. How to test them? What would a test for such a method look like?

def _connect_to_juniper(self):
        try:
            telnet_instance = Telnet(self.host_IP, timeout=self.connection_timeout)
            sleep(self.command_sleep)
            telnet_instance.read_until(b'login: ')
            telnet_instance.write(self.login)
            telnet_instance.read_until(b'Password:')
            telnet_instance.write(self.password)
            telnet_instance.read_until(b'> ')
            return telnet_instance
        except:
            print('%s (%s)\t- not available' % (self.host_IP, self.host_name))
            print(NameError)
            raise NameError('connection error')

Or, for example, how to test the parsing of data received from the device?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OnYourLips, 2016-10-03
@vebeer

telnet_instance = Telnet(self.host_IP, timeout=self.connection_timeout)

Don't create an object inside code to work with it.
Otherwise, it is very difficult to test it. Pass the created object.
And when using tests, pass not an object of Telnet type, but its mock.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question