TDD as if you Meant It: Add Missing Tests for untested Branches (Episode 10)

TDD as if you Meant It: Add Missing Tests for untested Branches (Episode 10)

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:

If I add the test

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

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.

Video

Check the video below with the codecast:

What’s Next?

Check the next episode on TDD as if you Meant it here: http://blog.adrianbolboaca.ro/evolutionary-design

On the same page you can find more ideas on Evolutionary Design.

Credits

Many thanks to Keith Braithwaite for creating the concept of TDD as if you Meant It

Teddy bear thanks to Erik Talboom for all the pairing, discussions that lead to so many twists we discovered together with TDD as if you Meant It.

Special regards to JB Rainsberger for the fun pairing we did using TDD as if you Meant It

 

Subscribe

If you want to receive an email when I write a new article, subscribe here:

Subscribe for new articles

One Thought on “TDD as if you Meant It: Add Missing Tests for untested Branches (Episode 10)

  1. Hi Adi 🙂 I was just trying to learn more about TDD 🙂 Hopefully I see you soon 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation