1

I need to start a server on localhost:3000 when starting python TestCase and then close it after TestCase finished.

I just realized that http.server does not detach the server so after starting the server, the TestCase does not continue until the server is not stopped.

class ServerTest(TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.server = socketserver.TCPServer(("", 3000), handler)
        cls.server.serve_forever()
        super().setUpClass()

    ... TESTS THAT SEND REQUESTS TO localhost:3000 ...

    @classmethod
    def tearDownClass(cls) -> None:
        cls.server.server_close()

Is it possible to make it work with http.server?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • 1
    Check out solutions at https://stackoverflow.com/questions/268629/how-to-stop-basehttpserver-serve-forever-in-a-basehttprequesthandler-subclass – matszwecja Jan 12 '22 at 10:32
  • @matszwecja Nice, thanks! – Milano Jan 12 '22 at 10:40
  • Does this answer your question? [How to stop BaseHTTPServer.serve\_forever() in a BaseHTTPRequestHandler subclass?](https://stackoverflow.com/questions/268629/how-to-stop-basehttpserver-serve-forever-in-a-basehttprequesthandler-subclass) – polm23 Dec 19 '22 at 05:51

0 Answers0