SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

Code Coverage with NCover

Revision 1057 of SharpDevelop2 sees support for code coverage.  You can now run your unit tests and check how much of your code is covered.  To use this feature NCover needs to be installed, the latest release, at the time of this post, was 1.5.2 (beta 3).


Currently NCover 1.5.2 can only be run under an account with administrator rights.  There is a workaround for this problem which you can find at the end of this post. 

NCover 1.5.3 no longer has this problem and can be run from a non-administrator account without requiring a workaround.

Running Tests with Code Coverage

To see how to use the code coverage feature in SharpDevelop we will use the Money test project that ships with NUnit.  First thing to do is to open it up and build it.  Then open up the Unit Tests pad (View | Tools | Unit Tests) and load the unit tests in to the tree view by clicking the Reload button.  Then select the tree node, right click and select "Run tests with code coverage".

You can also run the code coverage by right clicking a test fixture name or test method name and selecting "Run with code coverage", which is under the "Unit Testing" menu.

After this menu option has been selected, the output window should be displayed and you should see the NCover output as it profiles your assemblies.

If any errors were found in your tests, after the profiling has finished, then the Errors window will be displayed.

Code Coverage Results

To see the code coverage results open the Code Coverage window (View | Tools | Code Coverage).

The tree view shows the classes that have been profiled along with the percentage of the code covered. The list view, on the right hand side, shows more detailed output on the lines visited.

Double clicking a class or method in the tree view will open up the corresponding source file.  Double clicking a row in the list view will open up the corresponding source file and move the cursor to the appropriate line.

The toolbar button at the top left corner of the Code Coverage results allows you to enable/disable the highlighting of the covered source code. 

When this option is enabled, opening a source file will highlight the lines of code that have been covered in green, and uncovered lines in red.

Code Coverage Options

The colours used to highlight the covered code can be changed in the options page (Tools | Options | Tools | Code Coverage).

This options page also allows you to specify the location of the NCover console application.  By default SharpDevelop assumes it is installed in "C:\Program Files\NCover".

Code Coverage Project Options

By default all assemblies that can be profiled will be, this means you will see code coverage for tests as well as your other classes.  To specify which assemblies you want to be profiled, open up the project options (Project | Project Options), select the Code Coverage tab and enter the names of the assemblies you want to be profiled, without the file extension, each delimited by a semi-colon.

  • There is currently no way to stop a code coverage run.
  • Can only get code coverage for the entire assembly or assemblies, not individual test fixtures or test methods.
  • Running code coverage from the Unit Tests pad does not update the results in the tree.
  • The path to the test assembly cannot contain any spaces otherwise you will see the message "No MbUnit results file generated" in the Errors window.  NCover seems to strip quotation marks from any arguments passed to the profiled application.

Running NCover as non-admin

The only way I could get NCover 1.5.2 to work as non-admin was to re-register the CoverLib COM component.

Under an Administrator account, run "regsvr32 coverlib.dll" from the directory where NCover is installed (C:\Program Files\NCover).

NCover 1.5.3 no longer has this issue and installs for all users, making it possible to run NCover from a non-admin account without needing this workaround.

NCover Explorer
Code Coverage in Visual Studio with TestDriven.Net

[Update 1st Feb]

Added issue, found by Daniel, about the test assembly's path containing any spaces.

[Update 15th Feb]

Peter Waldschmidt has released NCover 1.5.3 which now installs for all users, so it can be run as non-admin without any workaround.  Added this information to the post.
Published Jan 31 2006, 05:42 PM by MattWard
Filed under:



The Geek Zone said:

I've started experimenting with SharpDevelop 2.0. So far, I've been pleasantly surprised at the ...
May 31, 2006 7:45 AM
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this ( email address.