I am currently writing Unit Tests to test the connection between a client and a server. At the moment I have the following tests; isConnectionSuccessful, isDisconnectionSuccesful, and isReconnectionSuccesful.
To try and cut down on repetition I have created a getter for a connected client. As the getter tests whether a client is connected I decided to just call it in isConnectionSuccesful as well, however this means a client is returned unnecessarily. For the other tests though, this approach appears to fit perfectly.
Is it ok to go with this approach, and just simply not assign the value from the getter for isConnectionSuccessful or is this a design flaw?
Just to clarify I have the following tests/methods:
isConnectionSuccessful
//call getConnectedClient
isDisconnectionSuccessful
//call getConnectedClient and assign it
//disconnect logic....
isReconnectionSuccessful
//call getConnectionClient and assign it
//disconnect
//reconnect
getConnectedClient
//instantiate client
//check it is connected
//return client
I think you’re talking about unit test design here. If so, tests follow somewhat different rules than main source. Your main goals in tests are for the test to be easily understandable and to provide a “safety net” to support changes to the code it’s testing. To apply those goals to your question, it might not be immediately apparent to a reader that
getConnectedClientdoes a connection check and thus is the only thing needed in theisConnectionSuccessfultest. That might be an argument for writing that test differently. On the other hand, if you feel the test is easier to understand the way it is, then maybe you just need to change the name ofgetConnectedClientto something likeconnectClientAndVerifyConnection.