Is 100% Test Code Coverage our True Aim as Engineers?

Unit test code coverage: that golden percentage that everyone is running after in the software industry. But why is it so important?Some folks assume that the higher and closer to 100% the coverage is, the more bug-free, maintainable, flexible to refac…


This content originally appeared on Level Up Coding - Medium and was authored by Catalin Patrascu

Unit test code coverage: that golden percentage that everyone is running after in the software industry. But why is it so important?

Some folks assume that the higher and closer to 100% the coverage is, the more bug-free, maintainable, flexible to refactoring the code is.

But is this the base metric that tells that all code is not error-prone? Like most of the answers in life, it’s a hard maybe.

First, we need to understand what unit test coverage percentage is: a measurement that tells us the number of lines of code run by unit tests divided by the total numbers of code in the application.

Supposing we have this method (which is not a pure function by the way):

1 int retrieveCurrentYear() {
2 final nowDate = DateTime.now();
3 return nowDate.year;
4 }

Running a unit test on it like this one:

expect(retrieveCurrentYear(), 2022); // 2022 is the time of writing this article

This will result in 100% code coverage because all four lines of code got executed while running the unit test.

Photo by bruce mars on Unsplash

We’ve done it! We are on top of the world… for less than one year 👏

Assuming we run this unit test next year (in 2023), it will fail, even though our code coverage was 100% 😥

So what’s my point here?

Code coverage is essential, and it should be relatively as high as possible, but chasing the highest possible percentage and doing the minimum possible tests to “pass” through those lines of code shouldn’t be our goal (like improper use of the pyramid of testing, not relying heavily on unit tests, but rather on snapshot testing, integration testing or other methods).

Instead, we should see what makes sense for our team and us; maybe a 70%-80% is good enough to give us a lot of confidence to move along.

For other teams, maybe a lower or higher value would make sense. The most important part is to aim for 100% certainty that once a line is covered, we won’t have any surprises/side effects in the future for it: quality vs. quantity (a complete example of how unit testing should be approached can be found in this story).

Thank you for taking the time to read all the above, and I hope this story helped you level up just even for a tiny bit. Also, please don’t hesitate to spread the knowledge to other fellow developers.

Get an email whenever Catalin Patrascu publishes.


Is 100% Test Code Coverage our True Aim as Engineers? was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Catalin Patrascu


Print Share Comment Cite Upload Translate Updates
APA

Catalin Patrascu | Sciencx (2022-01-27T03:21:46+00:00) Is 100% Test Code Coverage our True Aim as Engineers?. Retrieved from https://www.scien.cx/2022/01/27/is-100-test-code-coverage-our-true-aim-as-engineers/

MLA
" » Is 100% Test Code Coverage our True Aim as Engineers?." Catalin Patrascu | Sciencx - Thursday January 27, 2022, https://www.scien.cx/2022/01/27/is-100-test-code-coverage-our-true-aim-as-engineers/
HARVARD
Catalin Patrascu | Sciencx Thursday January 27, 2022 » Is 100% Test Code Coverage our True Aim as Engineers?., viewed ,<https://www.scien.cx/2022/01/27/is-100-test-code-coverage-our-true-aim-as-engineers/>
VANCOUVER
Catalin Patrascu | Sciencx - » Is 100% Test Code Coverage our True Aim as Engineers?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/27/is-100-test-code-coverage-our-true-aim-as-engineers/
CHICAGO
" » Is 100% Test Code Coverage our True Aim as Engineers?." Catalin Patrascu | Sciencx - Accessed . https://www.scien.cx/2022/01/27/is-100-test-code-coverage-our-true-aim-as-engineers/
IEEE
" » Is 100% Test Code Coverage our True Aim as Engineers?." Catalin Patrascu | Sciencx [Online]. Available: https://www.scien.cx/2022/01/27/is-100-test-code-coverage-our-true-aim-as-engineers/. [Accessed: ]
rf:citation
» Is 100% Test Code Coverage our True Aim as Engineers? | Catalin Patrascu | Sciencx | https://www.scien.cx/2022/01/27/is-100-test-code-coverage-our-true-aim-as-engineers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.