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 .
source share