If you’re writing your tests after your app code, do you ever find yourself struggling to hit those last few percentages of code coverage?
There are debates on how much code coverage is enough (80%? 90%? 95%? 100%?), but in general I feel like your code should have close to 100% code coverage. It’s nice to know that there are no hidden corners of your application that are lacking tests, especially if those spots are crucial parts of your application.
In practice, 100% code coverage probably isn’t always realistic or worth your time, but it’s a good ideal to strive for.
A nice side effect of using TDD is that it should result in 100% code coverage. If all the app code you’re writing is to satisfy tests, then in theory you shouldn’t have written any app code that isn’t being tested.
If you have an if/else statement in your app code where two possible outcomes could occur, it’s very likely you had some sort of product requirement stating “if A, then B should happen; if C, then D should happen”.
For example, “If the user is logged in, they should be able to see this page content. If the user is not logged in, they should not be able to see this page content.”