Testing servers
Testing a server: The idea Start the server (manually) [TestMethod] Send a request to the server Read the response from the server Compare the response to your expectations Assert.AreEqual(expected, response) Stop the server (manually) Example: EchoTcpServer Note: Although we use a Unit test tool this is not unit testing. It is integration testing
Start and stop the server automatically The test should be able to start and later stop the server automatically The server should have methods Start() and Stop() [ClassInitialize] executed once - before the first [TestMethod] [ClassCleanup] Executed once - after the last [TestMethod] Example: EchoTcpServer
Start and stop the server from the test private const int Port = 14593; private static EchoServer _server; [ClassInitialize] public static void TestStart(TestContext context) { _server = new EchoServer(Port); Task.Run(() => _server.Start()); } [ClassCleanup] public static void TestStop() { _server.Stop(); }
The servers Stop() method Server refuses new connections Existing connections (Tasks) must be given a chance to finish Example: EchoTcpServer