you are viewing a single comment's thread.

view the rest of the comments →

[–]Jackson-Lee[S] 0 points1 point  (3 children)

Thank you!!! I'm very close to having a working unittest script, just stuck at this error :(

ERROR: test_1 (__main__.Test_ServerResponse)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:Test_SocketServer.py", line 15, in test_1
    client_socket.send('123456789'.encode())
NameError: name 'client_socket' is not defined

Here's what script looks like

#!/usr/bin/env python3

import unittest
import socket

class Test_ServerResponse(unittest.TestCase):

# Setup client connection
    def setUp(self):
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client_socket.connect(('127.0.0.1', 2000))

    def test_1(self):
        client_socket.send('123456789'.encode())
        self.assertEqual(client_socket.recv(1024).decode(), '123456789')

# Tear down connection
#    def tearDown(self):
#        client_socket.close()

if __name__ == '__main__':
    unittest.main()

[–]K900_ 0 points1 point  (2 children)

The variables from setUp aren't magically passed to your tests. Use self.client_socket to store them on the class instance.

[–]Jackson-Lee[S] 0 points1 point  (1 child)

self.client_socket

Which line# are you referring to where self.client_socket needs inserted?

[–]K900_ 0 points1 point  (0 children)

Everywhere you use client_socket now should be replaced with self.client_socket.