Naming Conventions
I will always stress the importance of testing the behaviour of the code rather than the implementation. This starts with naming your test cases appropriately. Take the following code as an example:
public Connection getConnection(String connectionName)()
{
Connection connection;
switch(connectionName)
{
case "PrimaryDb":
connection = ConnectionType.ONE;
case "SecondaryDb":
connection = ConnectionType.TWO;
default:
connection = null;
}
return connection;
}
Now let's analyse the following test case names:
- getConnection_Can_Get_A_Database_Connection
- getConnection_canGetTheConnectionForDatabase1
- canGetTheConnectionForThePrimaryDatabase
Naming Variables
Current IDE's will most likely be equipped with auto-complete features. They can save a huge amount of keystrokes and if you are writing in a losely typed language like PHP, can help ensure you are not accidentally creating new variables. As I mentioned previously, self documenting code is an extremely important aspect of Unit Testing and variable names are part of that. Take the following example of a test case:
@Test
public void canReduceTheAmountOfFundsInASavingsAccount()
{
account = new Account(AccountType.SAVINGS);
account.setFunds(50);
account.subtractFunds(35);
assertEquals(15, account.getFunds());
}
The test is very easy to follow because the tester used descriptive variables and an Enum to specify the type of account. I have seen people take shortcuts and may name their variables "a" or "act". That may save a couple keystrokes, but it is much harder to follow when revisiting failed test cases.
0 comments:
Post a Comment