Comparing nCover to Team System Code Coverage


Once you've implemented unit tests, one of the questions you might ask yourself is, how much of my code is covered by unit tests? This metric is known as code coverage.

I'll demonstrate how to obtain code coverage metrics in both nUnit and Team System, using the Account class from our original Unit Testing post.

If you're using nUnit, you'll need to obtain a seperate tool to measure code coverage. One such tool is nCover. I recommend reading through the nCover FAQ to get an idea of how it works, but in a nutshell, it's a generic instrumentation tool that counts what lines of code were executed. Note that we won't be running nCover directly against our assembly; we'll be running nCover against nUnit running the unit tests in our assembly. It's a subtle distinction which is reflected in the command line syntax:

C:\>NCover.Console.exe nunit-console.exe test.exe
NCover.Console v1.5.2 - Code Coverage Analysis for .NET - http://ncover.org
Copyright (c) 2004-2005 Peter Waldschmidt

Command: nunit-console.exe
Command Args: nunitconsoleapplication.exe
Working Directory:
Assemblies:
Coverage Xml: Coverage.Xml
Coverage Log: Coverage.Log

Waiting for profiled application to connect...
******************* Program Output *******************
NUnit version 2.2.6
Copyright (C) 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov,
Charlie Poole.
Copyright (C) 2000-2003 Philip Craig.
All Rights Reserved.

OS Version: Microsoft Windows NT 5.1.2600 Service Pack 2 .NET Version:
2.0.50727.42

...N
Tests run: 2, Failures: 0, Not run: 1, Time: 0.031 seconds

Tests not run:
1) AccountTest.TransferWithInsufficientFundsAtomicity : Decide how to implement
transaction management

***************** End Program Output *****************

The output is an XML file named Coverage.Xml by default. There's a default XSLT stylesheet, so it looks reasonable when you double-click on it and open it in a web browser:



Coverage is green; non-coverage is red. note that I have 100% test coverage of our Account class. I probably should have screened out the other classes, but you get the idea.

If you want a better, more detailed view of the nCover output, you'll need to download another client application. Jeff Key created one such app, nCover Browser. Here's what Coverage.Xml looks like when viewed in Jeff's nCover Browser:



Things are a bit easier in Team System due to the higher level of integration with the IDE.

To generate code coverage metrics for Team System unit tests, you must first enable assembly instrumentation in the test properties dialog:



Once you've enabled instrumentation, re-run the tests. You can now use the Code Coverage Results pane to browse your coverage percentage. Click the highlight button (indicated by the red arrow) to visually mark the lines of code that are covered by a unit test:



Looks like our Account class is still 100% covered by unit tests! Of course, demonstrating basic test coverage doesn't mean my unit tests work, but it's a start.

posted on Monday, February 06, 2006 12:03 PM by jatwood

Comments

# VSTS Links - 02/07/2006

Rob Caron posts the Team Foundation Installation Guide RC. He also has a post on upgrading to Team Foundation...
Tuesday, February 07, 2006 7:02 AM by Team System News

# re: Comparing nCover to Team System Code Coverage

This is not a 100% completely fair comparison, showing colorful coverage from VSTS and not in NCoverBrowser (or NCoverExplorer, another application that shows you Coverage.xml graphically). And in VS2003 its all very easy if you use TestDriven.NET, which install NUnit, NCoverBrowser/NCoverExplorer (I'm not sure from memory which one exactly) and adds VS integration of running tests, getting coverage overviews and debugging test, right in the IDE.
Thursday, February 23, 2006 12:12 AM by Jarno Peschier