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.
我目前正在编写单元测试来测试客户端和服务器之间的连接。目前我有以下测试; isConnectionSuccessful,isDisconnectionSuccesful,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.
为了尝试减少重复,我为连接的客户端创建了一个getter。由于getter测试客户端是否连接,我决定只在isConnectionSuccesful中调用它,但这意味着客户端被不必要地返回。然而,对于其他测试,这种方法看起来非常合适。
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?
可以采用这种方法,只是不为isConnectionSuccessful的getter赋值,还是设计缺陷?
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
1 个解决方案
#1
1
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 getConnectedClient
does a connection check and thus is the only thing needed in the isConnectionSuccessful
test. 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 of getConnectedClient
to something like connectClientAndVerifyConnection
.
我想你在这里谈的是单元测试设计。如果是这样,测试遵循与主要来源不同的规则。您在测试中的主要目标是使测试易于理解,并提供“安全网”以支持对其测试代码的更改。要将这些目标应用于您的问题,读者可能不会立即明白getConnectedClient进行连接检查,因此是isConnectionSuccessful测试中唯一需要的东西。这可能是以不同方式编写测试的论据。另一方面,如果您觉得测试更容易理解,那么您可能只需要将getConnectedClient的名称更改为connectClientAndVerifyConnection。
#1
1
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 getConnectedClient
does a connection check and thus is the only thing needed in the isConnectionSuccessful
test. 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 of getConnectedClient
to something like connectClientAndVerifyConnection
.
我想你在这里谈的是单元测试设计。如果是这样,测试遵循与主要来源不同的规则。您在测试中的主要目标是使测试易于理解,并提供“安全网”以支持对其测试代码的更改。要将这些目标应用于您的问题,读者可能不会立即明白getConnectedClient进行连接检查,因此是isConnectionSuccessful测试中唯一需要的东西。这可能是以不同方式编写测试的论据。另一方面,如果您觉得测试更容易理解,那么您可能只需要将getConnectedClient的名称更改为connectClientAndVerifyConnection。