ExcludeFromCodeCoverage Exclude Automatically Generated Code

Is there a way to mark an automatically generated class as ExcludeFromCodeCoverage. I use this attribute in other areas and work fine. But if you open the autogenerated guy code and mark the classes as ExcludeFromCodeCoverage, once you regenerate this class, it will be written on top.

I can create partial classes in the code behind dbml and apply this attribute to it, but it works, however, it will do for many partial classes.

+6
source share
1 answer

You can use PostSharp or another AOP structure to create an aspect that will apply ExcludeFromCodeCoverageAttribute to the specified types or namespaces:

 [Serializable] [AttributeUsage(AttributeTargets.Assembly)] [MulticastAttributeUsage(MulticastTargets.Class | MulticastTargets.Struct)] [ProvideAspectRole(StandardRoles.PerformanceInstrumentation)] public sealed class DisableCoverageAttribute : TypeLevelAspect, IAspectProvider { public IEnumerable<AspectInstance> ProvideAspects(object targetElement) { Type disabledType = (Type)targetElement; var introducedExclusion = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof (ExcludeFromCodeCoverageAttribute))); return new[] {new AspectInstance(disabledType, introducedExclusion)}; } } 

Then just apply this aspect to the assembly and specify the namespace you want to exclude. At compile time, PostSharp will add ExcludeFromCodeCoverageAttribute to all classes in the My.AutogeneratedCode namespace:

 [assembly: DisableCoverage(AttributeTargetTypes="My.AutogeneratedCode.*")] 

An example code and explanation can be found here .

+4
source

Source: https://habr.com/ru/post/918972/


All Articles