About

During the first episode of this series I added some initial tests where I didn’t cover all the cases. Now it’s the moment to run coverage on the code and check the areas where I am missing tests.

My intention was to show you how to fix such a branch that was not covered by tests in the beginning. We can add missing tests even a lot later. So my mistake was intentional, because it’s a mistake I see very often and I wanted to show you what I do in this context.

Code Coverage with Tests

After extracting many classes and methods, I ca use code coverage to see which line(s) of code are not covered by tests. But code coverage is complicated. There are many types of code coverage:

Statement

Condition

Line

Method

Class

… and even more

So when you focus only on a metric, like code coverage, you need to know what to expect. You can read more at ISTQB.

Fallacies of Code Coverage

Let’s say we have the most complicated code in the world, and we want to have 100% code coverage:

1

2

3

4

publicvoidadd(inta,intb){

// I'm adding only positive numbers here!

returnMath.abs(a)+Math.abs(b);

}

If I add the test

1

2

3

publicvoidtestAddition(){

assertEquals(3,add(1,2));

}

then I will have 100% line coverage and 100% statement coverage. So then I’m done. I have added all the tests, right?!

But if then I add the test

1

2

3

publicvoidtestNegativeAddition(){

assertEquals(-4,assertEquals(-1,-3));

}

it will surely fail.

So take care when you use code coverage as a metric, it can trick you into thinking you added all the necessary tests, but if fact you have many missing tests.

TDD as if you Meant it and Full Test Coverage

TDD as if you Meant It doesn’t guarantee full test coverage, you need to have tools and use them to check your work

You can have full test coverage, but you need to have in mind some good practices:

Ask yourself when adding each test if you covered everything

When you have second logical flows (if, while, for, etc) always check really well if you added all the needed tests

Use a code coverage tool to tell you which branch is covered by tests

So the technique of TDD as if you Meant It will give you the context of having full test coverage, but you need to take the necessary measures to make sure.

Prize for Pawel

Even from the first episode Pawel Duda told me that I forgot to add a test for the “nobody won” branch. And he was right, I didn’t add the test with the purpose of adding it a lot later and show you how I do this. So when we meet he will receive a prize from me.