2

I wrote a small server chat that does very basic things and I would like to write the tests around it. Unfortunately I quite lost regarding. I would need some help to get on the right tracks.

I have a class called Server() and it contains a method called bind_socket(). I would like to write unit test (preferably using pytest) to test the following method:

class Server(Threading.Thread):
""" Server side class

Instanciate a server in a thread.

"""

MAX_WAITING_CONNECTIONS = 10

def __init__(self, host='localhost', port=10000):
    """ Constructor of the Server class.

    Initialize the instance in a thread.

    Args:
        host (str):     Host to which to connect (default=localhost)
        port (int):     Port on which to connect (default=10000)

    """
    threading.Thread.__init__(self)
    self.host = host
    self.port = port
    self.connections = []
    self.running = True

def bind_socket(self, ip=socket.AF_INET, protocol=socket.SOCK_STREAM):
    self.server_socket = socket.socket(ip, protocol)
    self.server_socket.bind((self.host, self.port))
    self.server_socket.listen(self.MAX_WAITING_CONNECTIONS)
    self.connections.append(self.server_socket)

I'm wondering what is the best way to write a test for this method as it doesn't return anything. Should I mock it and try to return the number of of call of socket(), bind(), listen() and append() or is it the wrong way to do proceed? I'm quite lost on that, I did many tests either with pytest and unittest, watch conferences and read articles and I still don't have anything working. Some explanation and/or examples would be greatly appreciated.

Thanks a lot

mattberjon
  • 105
  • 2
  • 8
  • Your class has two responsibilities: being a server and being a thread. This should be a warning sign. – Peter Wood Feb 11 '17 at 22:41
  • @PeterWood, thank you for the comment, you picked my interest, could you please explicit a bit more your thought? – mattberjon Feb 11 '17 at 22:47

1 Answers1

0

For each line of bind_socket you should ask yourself the questions:

  • What if this line didn't exist
  • (for conditionals... I know you don't have any here) What if this condition was the other way around
  • Can this line raise exceptions.

You want your tests to cover all these eventualities.

For example, socket.bind can raise an exception if it's already bound, or socket.listen can raise an exception. Do you close the socket afterwards?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • To answer your last question, yes I have a method that calls server_socket.close(). So, I shouldn't try to mock them but to improve the code to raise errors when ever possible and write the tests accordingly? – mattberjon Feb 11 '17 at 22:52
  • No you don't. If `bind_socket` fails when calling `bind` you don't close the socket. – Peter Wood Feb 11 '17 at 22:54
  • Ok, I think I understand that. I should check if the socket is already open, if not, bind it otherwise raise an exception. – mattberjon Feb 11 '17 at 22:56
  • Well, it might be okay to just let it raise an exception if you're happy with that (see [AFNP](http://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain)). But that should be one of your tests. – Peter Wood Feb 11 '17 at 22:59
  • That's the easiest task to do at the moment. I'm quite new to unit testing. On the paper it seem accessible, but once I try to write them, I'm struggling a lot and I can't succeed to start with it. I'm crunching on it for hours without any success. – mattberjon Feb 11 '17 at 23:05