Technology Toolbox

Your technology Sherpa for the Microsoft platform

Jeremy Jameson - Founder and Principal

Search

Search

Code coverage analysis with Visual Studio 2010 and .NET 3.5 solutions (e.g. SharePoint 2010)

Sometimes I wish I didn't develop in the world of SharePoint. Whenever I have the luxury of developing plain ol' ASP.NET solutions on .NET Framework 4, I am amazed at how easy things are.

In my experience doing .NET development with Visual Studio, things typically just work. When doing SharePoint development, on the other hand, things often behave, well, let's just say erratically.

For example, back in the days when I worked primarily in Visual Studio 2008 and Microsoft Office SharePoint Server (MOSS) 2007, I discovered an issue when running unit tests on a 64-bit VM. Fortunately I was able to workaround that bug.

Thanks to a couple of poor decisions by Microsoft product teams (specifically, targeting SharePoint 2010 to .NET 3.5 x64 only, and restricting test projects in Visual Studio 2010 to .NET Framework 4), it wasn't until Visual Studio 2010 Service Pack 1 that you could actually run unit tests against SharePoint 2010 solutions.

However, even after getting back the ability to run tests in Visual Studio against custom code written for SharePoint solutions, I noticed that some things still didn't work. For example, code coverage analysis is trivial to enable in Visual Studio, so there is really no reason not to periodically review how much code your tests are covering, right?

Unfortunately, the instructions in the Visual Studio documentation for configuring code coverage only work when your assemblies target .NET Framework 4. When developing for SharePoint 2010, the Visual Studio projects must target .NET Framework 3.5.

While doing some research this morning for this post, I found someone (a Microsoft MVP, no less!) who implies code coverage "just works" when developing for SharePoint 2010 -- but that certainly hasn't been my experience. Just to be sure I'm not losing my mind, this morning I followed the steps in the sample MSDN walkthrough for creating a unit test for SharePoint and subsequently enabled code coverage inside Visual Studio. This resulted in the following message from Visual Studio:

Debugging tests running on a remote computer or with code coverage enabled is not supported. The tests will be run under the debugger locally and without code coverage enabled.

Okay, perhaps what that Microsoft MVP meant is that you need to run the unit tests outside the debugger (e.g. by using Ctrl + F5). Ummm...yeah...I tried that -- it doesn't work. When I click the Show Code Coverage Results toolbar button in the Test Results window, I see the following:

Cannot find any coverage data (.coverage or .coveragexml) files. Check test run details for possible errors.

Fortunately, there is a way to perform code coverage analysis when working with Visual Studio 2010 and projects that target .NET Framework 3.5 (e.g. SharePoint 2010 solutions). However, you have to resort to some command-line tools, as described in the following post:

In my next post, I'll share the PowerShell script that I created to make this additional work relatively painless. First I need to get my daughter off to school (we are on delayed start this morning due to 6 more inches of snow -- woohoo!).

Comments

  1. # re: Code coverage analysis with Visual Studio 2010 and .NET 3.5 solutions (e.g. SharePoint 2010)

    August 2, 2012 1:45 AM
    sridhar
    valid point presented neatly gives good knowledge and understanding

Add Comment

Optional, but recommended (especially if you have a Gravatar). Note that your email address will not appear with your comment.
If URL is specified, it will be included as a link with your name.

To prevent spam from being submitted, please select the following fruit: Apple

Watermelon
Pear
Cherries
Strawberry
Apple
Grapes
 
Please add 1 and 4 and type the answer here: