I have recently started using TDD or u can say testing for my project and there i found some new thing(new thing for me) which is called "Code coverage" which shows how much your code is covered during the testing process. And as much i know most of the seniors use to say its not possible to have 100% code coverage or its not good practice to get 100% code coverage. This thing keeps me wondering like how this code coverage works i mean they covers code on which bases? please tell the main usage of Testing.
I am attaching image of code coverage with this question.
Actually, 100% code coverage is possible but depends a bit on the language:
About the usefullness of 100% code coverage:
Even 100% code coverage does not mean the code is perfect:
Addition:
In case 100% code coverage is not wanted (because everything takes time, thus money), first focus on the high risk areas in the code. Skip trivial methods at first and start with complicated/high risk functions.
Also it is important to use design patterns or a code structure to 'help' unit testing:
First, in order to understand the value of code coverage you have to understand what you want to achieve with it. Code coverage helps you determine the quality of your program code, e.g. is it robust or prone to errors, is it cohesive or does it have hidden dependencies, is it easy to change or not, etc.
Code with high code coverage tends to be better code, but it is no guarantee that it is good code. This is because the code quality strongly depends on how well your test cases are build, e.g. if you are testing your intended behavior well, or for false or destructive inputs, for corner cases or other special cases, etc. If your test suite is badly written, you can still achieve high (or 100%) code coverage, but your code will be of low quality.
Second, the reason why most experienced developers will tell you 100% test coverage is not required or even a bad practice is that the time you need to invest to make code coverage 100% is better invested in a more complete test suite. It will usually even be easier to achieve 100% code coverage with a poorly written test suite than with a well designed one.
Third, because you will (almost) never have a complete test suite, just because I don't know lots of people who can consider all possible cases that code may go wrong, you should develop an urgency to amend you test suits continuously (not infinitely) rather than settling on the false felling of full code coverage.
I hope this view on code coverage helps you make it more useful for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With