Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2012 ExcludeFromCodeCoverage Method Shows In Results

I have the following method:

[ExcludeFromCodeCoverage]
private static string GetAuthorizationToken(HttpActionContext actionContext)
{
    var authorization = actionContext.Request.Headers.FirstOrDefault(h => h.Key.Equals("Authorization"));
    // ... removed for brevity
}

The ExcludeFromCodeCoverage attribute works for the whole method, except for h.Key.Equals("Authorization"), which shows as uncovered despite the attribute on the method.

How can I exclude this symbol from code coverage results?

like image 946
Fenton Avatar asked Jan 15 '23 08:01

Fenton


1 Answers

I can explain why it shows as not being covered even though you've specified the ExcludeFromCodeCoverage attribute on the class. The explanation lies in the IL that is actually generated by the compiler. It declares a delegate to match the lambda expression which looks something like this.

private static Func<string, bool> AnonymousMethodDelegate;

It then creates the named method that matches the delegate declaration.

[CompilerGenerated]
private static bool CompilerGeneratedMethod(string h)
{
  return h.Equals("Authorization");
}

Then finally is calls into the named method using the delegate in your method something like this

[ExcludeFromCodeCoverage]
private static string GetAuthorizationToken(HttpActionContext actionContext)
{
  AnonymousMethodDelegate = CompilerGeneratedMethod;
  Func<string, bool> predicate = AnonymousMethodDelegate;
  return Enumerable.FirstOrDefault<string>((IEnumerable<string>) actionContext, predicate);
}

I'm sure you've spotted the problem. The compiler has created a method which no longer has your ExcludeFromCodeCoverage attribute attached! This is the reason that NCover judges it as having not been covered even though you appear to have declared the ExcludeFromCodeCoverage attribute.

One way to exclude it would be to go back to basics and effectively do what the compiler is doing for you and declare the delegate and named method. This gives you the power to add the ExcludeFromCodeCoverage attribute to that named method. I'm not a big fan of this solution though for obvious reasons.

Obviously if you add the ExcludeFromCodeCoverage attribute at the class level it will also cover this generated method which would get around this problem but I assume this would also exclude code that you wanted to include in the coverage report.

The other alternative is that if you're using NCover then you could use the fact that all the compiler generated methods are decorated with the CompilerGenerated attribute. You can pass attributes to be excluded to NCover so all you would have to do is pass System.Runtime.CompilerServices.CompilerGeneratedAttribute either on the command line or via the MSBuild task. This is probably my preferred option.

like image 130
whudson05 Avatar answered Jan 25 '23 23:01

whudson05