Is My CodeCoverage.runsettings file wrong?

Headlilne: Microsoft has a new .RunSettings file to replace the previous .TestSettings file so that you can use it with other systems (like NUnit), more easily support Team Build, and improve performance (among other motivations). But does it work yet? I cannot get the GeneratedCode attribute to prevent code coverage analysis of the code.

There are several good posts on the new use of the .runsettings files in Visual Studio 2012. For example:

The User Story

But here is my user story:

As a developer I would like to exclude certain code when I run Analyze Code Coverage from the Test menu of Visual Studio 2012 so that I know what code is not properly tested.

I would like for all my logic and data access to be covered, however there are certain exceptions:

  • I don’t care about code coverage of my unit test projects!
  • I don’t want to test all the code generated by a Resources.resx file.
  • I don’t want to test all the generated code from the Entity Framework.

The Experiments

To see how all this works I created a small solution that looks like this:

image

And here are the classes shown above:

This class is decorated with the CompilerGenerated attribute.

MyCompilerGeneratedClass
  1. [CompilerGenerated]
  2. public class MyCompilerGeneratedClass
  3. {

This class has no attributes on the method or the class.

MyCoveredClass
  1. public class MyCoveredClass
  2. {
  3.     public void Exercise()
  4.     {
  5.         var name = typeof(MyCoveredClass).Name + ".Exercise()";
  6.         throw new NotImplementedException(String.Format(Properties.Resources.NotImplemented, name));
  7.     }
  8. }

This has the DebuggerHidden attribute on the method (it cannot be applied to the class).

MyDebuggerHiddenClass
  1. public class MyDebuggerHiddenClass
  2. {
  3.     [DebuggerHidden]
  4.     public void Exercise()

This has the DebuggerNonUserCode attribute on the class.

DebuggerNonUserCode
  1. [DebuggerNonUserCode]
  2. public class MyDebuggerNonUserCodeClass
  3. {
  4.     public void Exercise()

And finally, this one has the GeneratedCode attribute on the class.

MyGeneratedCodeClass
  1. [GeneratedCode("Entity Framework", "5.0")]
  2. public class MyGeneratedCodeClass
  3. {
  4.     public void Exercise()

Here are the code coverage results without the use of the CodeCoverage.runsettings file:

image

So you will notice some interesting things:

  • Code Coverage took it upon itself to exclude DebuggerHidden and DebuggerNonUserCode attributed code.
  • Today (unlike yesterday on a different project) it did not cover the Resources class. (CodeCoverageLibrary.Properties.Resources.Designer.cs)

So now I’m going to turn on the use of the CodeCoverage.runsettings file:

image

For unit tests, I add this into my CodeCoverage.runsettings file:

image

I name my projects <ProjectBeingTested>.Tests, so you might need to change this to match your convention.

Then I added the lines for all the different attributes from various websites:

image

With these in place, let’s run Code Coverage again. Here are the new results:

image

So we got rid of the noise from the unit test project, however, the Code Coverage does not seem to have honored my request to exclude the CompilerGenerated or the GeneratedCode attributes. Here are other options I tried with the attribute list shown above:

image

Now I really didn’t expect for any of those to have an impact because they should all look pretty much the same in the intermediate language (IL). But I’m not that knowledgeable, so I tried them all.

So we are on my user story except that GeneratedCode from the Entity Framework cannot be excluded using that attribute.

The Workaround

Headline: Use the ExcludeFromCodeCoverage attribute.

This is an attribute I wish developers didn’t even know about. I’ve searched that out in the past to prevent its usage. But now I have a choice… Call that code DebuggerHidden or DebuggerNonUserCode – which will bite me later when I’m debugging, or use the ExcludeFromCodeCoverage.

Has anyone else been able to make the GeneratedCode attribute work in preventing code from being analyzed in code coverage in Visual Studio 2012?


Posted

in

by

Comments

6 responses to “Is My CodeCoverage.runsettings file wrong?”

  1. Dan Cohen Avatar
    Dan Cohen

    I would love to know if this ever gets resolved, or what the magic trick is. Attempting to solve the same problems at work. Seems 2012 is a major step back for people using MSTest.

  2. Sean Carrington Avatar
    Sean Carrington

    Hello,

    Just for any others who might find your blog from Google. I have a solution to this issue :).

    I managed to get this working using the following:-

    .*GeneratedCodeAttribute$

    1. karlz Avatar

      Thanks for the info! I’ll try it out.

    2. Drew Avatar
      Drew

      Works great! Thanks.

  3. SK Avatar
    SK

    Yes.. That worked for me to exclude the web reference classes from the code coverage.
    Thanks a lot.!

  4. […] code coverage – in fact, the default runsettings file is supposed to be configured this way – but this doesn’t seem to work.  However, even if this would work, it probably wouldn’t be what we want – even though the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.