Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

How to configure Visual Studio 2010 code coverage for ASP.NET MVC unit tests

I just got Visual Studio 2010 code coverage work with ASP.NET MVC application unit tests. Everything is simple after you have spent some time with forums, blogs and Google. To save your valuable time I wrote this posting to guide you through the process of making code coverage work with ASP.NET MVC application unit tests.

After some fighting with Visual Studio I got everything to work as expected. I am still not very sure why users must deal with this mess, but okay – I survived it. Before you start configuring Visual Studio I expect your solution meets the following needs:

  • there are at least one library that will be tested,
  • there is at least on library that contains tests to be run,
  • there are some classes and some tests for them,
  • and, of course, you are using version of Visual Studio 2010 that supports tests (I have Visual Studio 2010 Ultimate).

Now open the following screenshot to separate windows and follow the steps given below.

Visual Studio 2010 Test settings
Visual Studio 2010 Test Settings window.
Click on image to see it at original size.
 

  1. Double click on Local.testsettings under Solution Items. Test settings window will be opened.
  2. Select “Data and Diagnostics” from left pane.
  3. Mark checkboxes “ASP.NET Profiler” and “Code Coverage”.
  4. Move cursor to “Code Coverage” line and press Configure button or make double click on line. Assemblies selection window will be opened.
  5. Mark checkboxes that are located before assemblies about what you want code coverage reports and apply settings.
  6. Save your project and close Visual Studio.
  7. Run Visual Studio as Administrator and run tests. NB! Select Test => Run => Tests in Current Context from menu.
  8. When tests are run you can open code coverage results by selecting Test => Windows => Code Coverage Results from menu.

Here you can see my example test results.

Visual Studio 2010 Test Results window
Visual Studio 2010 Test Results window. All my tests passed this time. :)
Click on image to see it at original size.
 

And here are the code coverage results.

Visual Studio 2101 Code Coverage Results
Visual Studio 2101 Code Coverage Results. I need a lot more tests for sure.
Click on image to see it at original size.
 

As you can see everything was pretty simple. But it took me sometime to figure out how to get everything work as expected.

Problems?

You may face some problems when making code coverage work. Here is my short list of possible problems.

  1. Make sure you have all assemblies available for code coverage. In some cases it needs more libraries to be referenced as you currently have. By example, I had to add some more Enterprise Library assemblies to my project. You can use EventViewer to discover errors that where given during testing.
  2. Make sure you selected all testable assemblies from Code Coverage settings like shown above. Otherwise you may get empty results.
  3. Tests with code coverage are slower because we need ASP.NET profiler. If your machine slows down then try to free more resources.

Comments

Ch.Tdd said:

Does it work only with MSTest? Could i use VS to calc NUnit tests coverage?

# May 5, 2010 1:47 AM

DigiMortal said:

Hi Ch.Tdd and thanks for question! I know that Visual Studio is able to recognize some other unit testing frameworks but I have made no attempts to use them this far. I suppose mbUnit should be supported because Visual Studio is able to recognize it.

# May 5, 2010 2:06 AM

andrewstopford said:

Hi,

In answer to other frameworks such as MbUnit being able to use this please see - weblogs.asp.net/.../gallio-and-vs2010-code-coverage.aspx

Andy

# May 5, 2010 12:57 PM

cd said:

Is this just in ultimate version?

# May 5, 2010 2:54 PM

punkouter said:

how is this done any differnt in web forms? looks the same to me

# May 14, 2010 4:58 PM

ming said:

I cannot find Local.testsettings under Solution Items as my solution does not even have Solution Items.

What's the prerequisite to follow this tutorial?

# June 19, 2010 6:16 AM

Noel said:

Thanks for being the only place to document *where* to actually click to select which projects will generate code coverage. That stupid Test Settings grid/list is a UX fail!

# July 12, 2010 9:39 AM

Sachin said:

Hi,

I am facing a problem as most of the settings are missing from the left panel when i click on Local.testsettings.

Do i need to install VS again or ther is some other way out?

# January 31, 2011 2:32 AM

DigiMortal said:

Which version of VS2010 you have?

# January 31, 2011 3:48 PM

Ben said:

@Sachin I don't think this feature is supported in Visual Studio 2010 Professional. In which case you'll have to upgrade your Visual Studio version (which Microsoft would prefer), or do what everyone else actually does, which is move to NUnit! Also, if you are using some kind of CI server, such as TeamCity, then you would need to install Visual Studio on that server, which of course is idiotic.

# February 8, 2011 6:47 AM

krisatwfl said:

If my Visual Studio 2010 doesnt support tests, can I use an external tool to run the tests?

My VS 2010 is not ultimate. I plan to run the test scripts using qtp. Can I still use the code coverage tool from VS.

# February 23, 2011 4:32 PM

Ravi said:

Hi, I am running the Test on VS 2010 Ultimate with Sharepoint 2010. I configured all settings and test runs but with warning:

Diagnostic data adapters are not supported when you run tests that target .Net Framework 3.5. The following diagnostic data adapter is not enabled

And

Code coverage in-place instrumentation: Cannot fully backup the binary

And I am not getting the Code Coverage report

Thanks,

Ravi

# April 29, 2011 3:29 AM