20

I am using Visual Studio 2010 and would like to exclude the generated service reference code from my code coverage statistics.

I found an article pre 2010 that mentions using DebuggerNonUserCode and DebuggerHidden attributes. I have tried this an it works as advertised. DebuggerNonUserCode is set at the class level, but with 50+ classes generated in each of the generated service reference code files, this is not an attractive option.

Does anyone have any alternative solutions?

btlog
  • 4,760
  • 2
  • 29
  • 38

4 Answers4

17

The generated classes are partial. If you create a new class in your project with the same namespace and class declaration you can add the [ExcludeFromCodeCoverage] attribute to your partial class. That way you don't have to go back and edit the Reference.cs file whenever you refresh your reference.

gvlasov
  • 18,638
  • 21
  • 74
  • 110
Paul
  • 815
  • 1
  • 13
  • 23
5

In Reference.cs, you can find an existing attribute, like [System.Diagnostics.DebuggerStepThroughAttribute()] and do a search and replace with [System.Diagnostics.DebuggerStepThroughAttribute()][System.Diagnostics.DebuggerNonUserCode()].
The major drawback is that you have to redo this each time you update the reference.

I don't understand why MS does not make the code coverage tool smart enough to skip service reference generated code.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
Kevin
  • 51
  • 1
  • 1
3

System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage can be used on top of the class. This is a poor option since you need to redo this anytime you regenerate your code. Maybe Microsoft could do this for us automagically when creating service references, entity framework types, etc...

beezler
  • 646
  • 6
  • 18
2

You could create a code generator that emits partial classes with the DebuggerNonUserCode attribute.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • Unfortunately the cost of creating the code generator, testing etc, outways the cost of adding this manually to the existing generated code. Thanks for the idea. – btlog Aug 09 '10 at 13:59