4

I'm writing a simple program that talks to a router via telnet and issues some simple commands to it. I want to be able to test these commands, that the program responds correctly to the servers output and sends the commands at the right moments. However i can't figure out a good way to do it. Best I've come up with is to create a small telnet server for my unit tests but that seems a bit excessive and i'm hoping there's a simpler way to do it.

Anyone got any ideas?

grimurd
  • 2,750
  • 1
  • 24
  • 39

1 Answers1

4

Best practice in cases like this is to mock the server object. Since you're using python 3, you can simply use unittest.mock to create a mocked server object for use in testing. Docs here: https://docs.python.org/3/library/unittest.mock.html

Atra Azami
  • 2,215
  • 1
  • 14
  • 12
  • Mocking the telnetlib server is something that i hadn't considered. But i don't think that i can test it properly that way. I can test if it is sending the correct commands but i don't think that i can send replies from the mocked object to see if my code responds correctly. Or am i wrong? – grimurd Jan 07 '15 at 09:54
  • 2
    @GrimurD: Mock frameworks allow you to define the return values of method calls. In your test setup, you need to predefine the results which a real server would send. – Aaron Digulla Jan 07 '15 at 11:17