jeudi 21 avril 2016

How to test a program that has server connection (sockets)

So I've been working on a project in my 1st year of Computer Engineering, that includes me connecting to the school server (the # symbol) and I have to stay in a chat with json formatted text, and etc. This is the main code:

tcp_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_s.connect( ("##.###.##.##", ####) )
nome = ""

if len(sys.argv) == 2:
    nome = sys.argv[1]
    print "Nome: " + nome
else:
    nome = raw_input("Nome: ")

msg = dict()
msg['from'] = nome


while 1:
    r,w,e = select.select([sys.stdin, tcp_s], [], [])
    if sys.stdin in r:
        msg['msg'] = raw_input(">")
        data = json.dumps(msg)+"\n" 
        tcp_s.send(data)

    elif tcp_s in r:
        msg2 = tcp_s.recv(4096)
        print msg2

What I don't know how to do and can't find anywhere useful to learn it, is how to test it. I know what unitary and functional tests are, and a lot of the times they are quite simple. But how can I make a unitary test to the connection of a server using pytest?

Would be grateful for a quick answer since I have until today's midnight to learn how to do it and deliver it, thank you!

Aucun commentaire:

Enregistrer un commentaire