ExcludeFromCodeCoverage and Linq

Visual Studio has an attribute called ExcludeFromCodeCoverage to exclude a method from code coverage. But when you use Linq the predicate is included. See sample screenshot below where the code is called with an empty list in the repository.

linq predicate included in codecoverage

Looking deeper you could discover the reason in the code coverage results pane.

linq predicate code coverage results pane

Linq generates a method for the predicate and uses a delegate to the method. This is generated at compile time. Some code coverage frameworks offer the option to exclude attributes (like ExcludeFromCodeCoverage) and adding CompilerGenerated would do the trick. Visual studio code coverage seems to have support for customization also.

💡 read The Art of Unittesting by Roy Osherove

  • is this really necessary?
  • isn’t there something wrong with my tests?
  • should I simply write more tests?

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s