Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to exclude Entity Framework Autogenerated Code from Code Coverage Statistics?

I have seen the [DebuggerNonUserCode] and [ExcludeFromCodeCoverage] attributes in resources and other SO questions about exlcuding code from coverage statistics, and wanted to know if it was possible to automatically add this attribute to the classes in the code generated by the Entity Framework using .NET 4.0.

Also would it need to be class level or could it be on the diagram.Designer.cs level, needing one attribute for all code generated by that diagram?

like image 601
StuperUser Avatar asked Nov 06 '22 04:11

StuperUser


1 Answers

Since partial classes (which Entity Framework creates) merge attributes, extended functionality in other partial classes are also excluded if the attribute is class level in the template, it will have to be applied at the method level.

The best way that I've found to do this is using T4 (as recommended in @Craig Stuntz's answer) to:

  • include: using System.Diagnostics.CodeAnalysis; at the top of the file

Then apply [ExcludeFromCodeCoverage] to getters, setters and Factory methods by searching for:

  • #>get
  • #>set
  • Template_FactoryMethodComment

and placing them in the appropriate place.

This was made a lot easier using Tangible's T4 editor Extension for VS.

This is my first attempt and it seems to work, "your milage may vary", so complete a test run to make sure everything's working as necessary.

like image 99
StuperUser Avatar answered Nov 09 '22 12:11

StuperUser