

Wrapping upĪlthough code coverage reports are easy to set up there’s still some work needed to get numbers right. This is why it is important to keep these metrics correct. If these numbers lie then the work ahead may seem way bigger than it actually is. Over time these metrics may be important part of estimating the need for unit tests. I don’t want to say that same effects appear with every application but there will be changes in numbers. Reports above were generated to relatively small web application and change in numbers was pretty big. I think it makes a lot of sense to exclude from code coverage reports the code that will never be covered with tests by us. Should I use ExcludeFromCodeCoverage attribute? As a result the numbers on my code coverage report changed around 50 percent and that’s huge even for small web application. I left out load of primitive models, DTO-s, constructors and scaffolded ASP.NET Identity code-behind files. Now I add ExcludeFromCodeCoverate attribute to all classes and members that doesn’t need testing. There’s no code excluded – everything is counted in. I take my simple demo application and generate code coverage report for it. } ExcludeFromCodeCoverage attribute in action ĮxcludeFromCodeCoverage attribute works also on class member level. EditFormModel class shown above can be left out from code coverage by simply adding the attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests.

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. Without any additional information these classes will be part of code coverage calculations.

Usual candidates are primitive models and Data Transfer Objects (DTO). I think almost all applications have some classes we don’t want to test.

This blog post focuses on how to leave out from code coverage all code that will not be covered with unit tests and get numbers shown on code coverage reports correct. Recently I blogged about how to generate nice code coverage reports for ASP.NET Core and.
