Thoughts on Test Coverage

A classic debatable subject is whether a 100% percent code coverage is really a realistic requirement or goal. Opinions vary, but I feel the common consensus is “It depends”. For instance, critical, re-usable libraries are expected to have a very high unit test code coverage. I think all agree that the quality of tests outweigh the quantity. Martin Fowler has a great article tackling that same question. As a matter of fact, Martin Fowler shares that 100% code coverage “would smell of someone writing tests to make the coverage numbers happy, but not thinking about what they are doing.”

For me, 100% code coverage is a MUST! This may not mean that there is a test for every code construct. Model classes for instance, would not benefit much from unit tests and are usually decorated with the “ExcludeFromCodeCoverage” attribute (if using .Net) so they are factored out when calculating code coverage metrics.

What I mean by achieving 100% code coverage is that developers should make sure code is 100% looked at and closely thought through as a team during code reviews as well as iteratively revisited during code reviews to ensure that future state has no impact on any decisions previously made to forgo test coverage on certain code areas.

In some instances, if a code section is deemed unnecessary to write a test for, [ExcludeFromCodeCoverage] may be added to indicate this decision for the team; thereby acting as a conversation starter during code reviews and an indicator that such a discussion took place and consensus was reached. Care should be exercised when using this exclusion tag as with great power comes greater responsibility. Decorating a class with this attribute means it will be excluded from any future code coverage analysis. This could be okay for current state, but should additional logic be added in the future, this may cause necessary tests to slip away.

I think the above thoughts could generate a good amount of debate among us opinionated software engineers. I look forward to hearing your take on this 😊

Credits: I appreciate all the comments and ideas that I am receiving. I may be starting with a very brief paragraph, but I will be editing/adding as our discussion progresses.