How to increase tests coverage on an existing codebase

July 2, 2014

TDD sure is cool but how do you TDD on an existing codebase ? ;)

Tests coverage sure got a lot of benefits and you sure should want to add them to existing code. But how ?

More clearly : how can you motivate your programmers to add tests (unit, functional, behavior, whatever) to a code that was committed 3 months ago and is working "perfectly" ?

Face the cold reality

If the code hasn't been TDD-ed or was brought to this world with no consideration whatsoever to testing, things like isolation, mocking and dependency-injection will be missed. A lot.

Thus don't go on the "increase the coverage by 1% a day" or others similar rules before you can grasp the whole picture. That sole grasping thing could take a few days :)

You Can’t Improve What You Can’t Measure

The good news is tests coverage for iOS is fairly easy nowadays so you can raise awareness among your programmers without frightening them.

Bugs are opportunity

More important than your testing frameworks or your testing techniques is your test plan. What to test, with which tests case, that's the hard part to do right.  And it is time-consuming. If you aren't lucky enough to have a Q&A departement, bugs are your new best friends.
bug = "a test case" + "a test that fail" (for free !)
So next time a bug appears in you Jira/Mantis/Redmine/Github/Bitbucket, ask yourself if it can be converted to a test.

Go for unit testing

Functional/behavior testing is the most interesting but also the most difficult (to program as well as to set up). So don't put the cart before the horse and accept to go slowly in the beginning of your endless quest. And MVVM should give you a pretty decent way to "test the views" of your application.

And just do it.