Sunday, 22 March 2009

Integrating PartCover with Visual Studio 2008

One of essential tools in the software development is code coverage. Together with unit testing tools like NUnit it is easy to see how much of the code is covered by tests. Ideally Developer need to aim for 100% of code coverage when creating Unit Tests. This tutorial will use one of my software projects Windows Environment Variables Manager or EnvMan as an example.  On EnvMan website you can find another post that shows integration of PartCover with Visual Studio 2005. Technically there is no much difference between Visual Studio 2005 and 2008, this post covers version 2.2.0 of the PartCover and shows updated batch script as latest version of the PartCover required some changes to it.

PartCover.cmd

As first step we need to download and install latest version of the PartCover. It is available from SourceForge site. Once installed we need to create PartCover.cmd batch file.

REM === Start PartCover.cmd ===
"C:\Path\To\PartCover\partcover.exe"--target "C:\Path\To\NUnit\nunit-console.exe"--target-work-dir %1 --target-args %2 --output %1\partcover.xml --include %3

"C:\Path\To\PartCover\PartCover.Browser.exe" %1\partcover.xml
REM === End PartCover.cmd ===

Integrating PartCover with Visual Studio 2008

To integrate PartCover with Visual Studio select menu Tools\External Tools ...

In the External Tools Dialog box click Add and enter the following data in the fields.

Title:

Part Cover

Command:

C:\Path\To\PartCover\PartCover.cmd

Arguments:

$(ProjectDir)\bin\Debug $(ProjectDir)\bin\Debug\$(TargetName)$(TargetExt) [*Assembly Regular Expression*]*

Initial Directory:

$(ProjectDir)\bin\Debug

Tick Use output Window and Prompt for Arguments. Click OK

Running PartCover

To run PartCover select EnvManagerTest project and use Part Cover from Tools menu. Because we ticked "Prompt for Arguments" the following dialog box appears.

Replace "Assembly Regular Expression" with "EnvMan" so it looks like this [*EnvMan*]*. Please look at PartCover documentation for more details about regular expressions. After OK is clicked output window of Visual Studio will show a progress of the coverage test and Part Cover Code Browser window will appear. Unfortunately PartCover Code Browser does not open generated report file automatically. Use Open File menu to view generated partcover.xml report file.