I’ve been wrestling with code coverage under VS2005 for the last couple of hours and I thought I would share my experiences.   Firstly, let me explain the project structure.  We have a number of class libraries, which are front ended by about half a dozen web services.     In our testing we wanted to be able to unit test both the web services and the underlying class libraries as well.   Now, you would think that enabling code coverage would be a simple thing to do.  I would have expected the experience to go something like this.   Open up a code coverage tab and select the project or dll that you wish to instrument. Well, except for the most simplest case it is not that simple.

So how do I enable code coverage?    The first thing you need to do is ensure that you have a test run configuration added to your solution.  If you aren’t sure if you have one, go to the test menu and select the “Select Active Test Configuration”.  If you see No Active Test Configuration  this means that you don’t have one currently added to your solution.  The simplest way to add one is to right click on your solution and select Add New Item.  This will popup a window like the one below.  Just click on Test run Configuration and click on Add.

Once you have added the test run configuration to your solution, the next step is to enable code coverage on the dll.   Again, this is from the Test menu select Edit Test Run Configuration This will bring up the test configuration dialog shown below.  From here select Code Coverage.

 

So far so simple.  However, this is were the fun begins.    There are a couple of gotchas.

  1. Keep a very close eye on the directory that is displayed next to the Artifact that you are going to instrument.   You need to make sure the directory of where the file is going to be outputted is where the program is going to be run.  If like me you are wanting to instrument this on a build server when doing a release build you need to make sure that you select the release configuration BEFORE  you add an item into to be instrumented.  What happens if you don’t do this is that you don’t get any coverage results out at all.
  2. If you are instrumenting both a Web Application project and a class libraries in the one test run make sure you selected the dll’s from the Web Application project rather than the output directory of the class libraries.   At this stage I’m not sure why this is required.  However, I think it has something to do with the way code coverage works but I need to do more investigation before I’m sure.
  3. If you have C++ assemblies in the same project, don’t try and instrument them at the same time.  I’m still trying to find out why this is but it cause the test manager not to execute any tests.

Once you have done that you are away and you should get code coverage results out.   If you don’t go back an look at where the path is pointing in the code coverage section of your testrunconfig file.

Advertisements