Optimizing Coverage in Unit Tests
Get familiar with how to optimize code coverage in your automated tests.
We'll cover the following
When we use code coverage, we must ensure that our coverage is as close to 100% as possible, because that’s a good indicator that most of our executable code is covered by automated tests. However, being cautious is important. The goal is not only to chase coverage but to ensure that this coverage is also meaningful. Using high-quality tests that provide partial coverage and testing the remaining code manually is more helpful than 100% coverage without meaningful validations.
Even TDD does not guarantee 100% coverage. Even with writing the tests first, we must still do the following:
Mock the services that connect to external endpoints.
Exclude the adapter layer (REST API controller methods) from the unit tests.
Exclude application startup code from unit tests.
Example of optimizing code coverage
Let’s explore how we can optimize coverage metrics. The following playground has an ASP.NET Core web application with partial unit test coverage:
Get hands-on with 1400+ tech skills courses.