Good coding habits to make your test clean

There are many blog posts that have mentioned the benefits of writing unit test. Unit tests are important, since they provide the faster feedback about the correctness of your code. It is better to start with failing test and add enough code to make your test from red to green. But unfortunately most of the time I have found that we, software developers do not write clean code in our test methods. We do not think much about the test method name, many occasions we write big test methods which are difficult to follow. I am going to write some good habits that can be followed to make the test clean, readable and maintainable.

1. Careful about primitive assertions: a primitive assertion is one that uses primitive elements like != or == or magic numbers (-1 or 0) than the behaviour it is checking. Primitive assertion makes tests difficult to follow. So if you see them, then try to refactor it. For example, the below test

public void shouldOutputContainHello(){
  String output = "Hello World";
  assertTrue(output.indexOf("Hello") != -1);

can be replaced with

public void shouldOutputContainHello(){
  String output = "Hello World";
  assertThat(output, containsString("Hello"));

2. Try to have meaningful test name. Your test’s name should tell the intention of the test. Personally I prefer to start the test’s name with “should”. I think “should” makes test intention more clear rather than “testThis” or “testThis1” or “testThat”.

3. Your test should have one reason to fail. Having too many assertions in a single test reduces readability a lot. Many occasions you will find yourself wasting time looking for the reason to fail.

4. Do not write too many setup code in your test method. Have them in a separate private method and give it a meaningful and descriptive name.

5. Your test should not have multiple tests inside it. When you do this, your test will have multiple reasons to fail, lot of code duplication, lot of code smells.

6. Magic numbers numeric values among code that don’t communicate their meaning. Magic numbers are bad and should be avoided.

7. Do not write overprotective tests, such as

public void shouldCountEqualToFour(){
  Data data = project.getData();

Here the null check is redundant.

8. Careful about code duplication, conditional logic, flaky test (constantly failing test), hardcoding file path, persistent temporary file.

9. Try to use JUnit’s Parameterized or TestNG DataProvider to get rid of parameterized mess.

10. If you do not need a test, then just remove it rather than commenting out or adding @Ignore. Whenever you see this, look for the reason. Refactoring the test can sometimes solve the issue. If you do not see the reason, then delete it.

Effective Unit Testing – Lasse Koskela

Good coding habits to make your test clean

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s