Ever since NCover has not been free any more, I use PartCover, which is available under the GNU General Public License (GPL).
In this post I will show how you could generate a nice 'Code Coverage Analysis' report by using ReportGenerator and how you could integrate PartCover and ReportGenerator in your MSBuild script.
Generating the report
First you have to run MSTest (or NUnit, or whatever you use for testing) within PartCover. The command could look this:
PartCover.exe --target "MS_TEST_PATH\MSTest.exe" --target-args "/noisolation /testcontainer:YOUR_ASSEMBLIES" --include "NAMESPACES" --output "partcover.xml"
PartCover generates a XML file containing the coverage information.
ReportGenerator converts this XML file into a readable HTML report. ReportGenerator can transform multiple XML files at once, so you could also merge multiple coverage results into one HTML report. The command could look like this:
ReportGenerator.exe "report1.xml;report2.xml" "OUTPUT_DIRECTORY"
MSBuild
Now lets have a look at the MSBuild target which runs PartCover. Since PartCover uses some COM stuff, we register a DLL before PartCover can be executed. After PartCover has completed, the DLL is unregistered again. That way we don't have to install PartCover.
<Target Name="Test" DependsOnTargets="Clean; Compile"> <MakeDir Directories="$(ReportsPath)\coverage" /> <Exec Command="regsvr32.exe /s lib\PartCover\PartCover.CorDriver.dll" /> <Exec Command="lib\PartCover\PartCover.exe --target "$(MSTestPath)\MSTest.exe" --target-args "/noisolation /testcontainer:$(TestFiles)" --include "$(ReportNamespaces)" --output "$(ReportsPath)\coverage\partcover.xml"" ContinueOnError="true" /> <Exec Command="regsvr32.exe /u /s lib\PartCover\PartCover.CorDriver.dll" /> </Target>
And the MSBuild that executes ReportGenerator:
<Target Name="Report" DependsOnTargets="Test"> <Exec Command="lib\ReportGenerator\ReportGenerator.exe "$(ReportsPath)\coverage\partcover.xml" "$(ReportsPath)\coverage\HTML"" ContinueOnError="true" /> </Target>
SampleApplication
I attached a sample application (based on MSTest) which should work without any modifications on Windows Vista and Windows 7 (32 and 64 Bit) with Visual Studio 2008 Professional installed to the default directory. For Windows XP you have to adjust the path to MSTest.
Open a "Visual Studio Command Promt" with administrator privileges and execute the following command to run the MSBuild script:
msbuild build.proj
Updates
28.06.2010: When you use PartCover 4, replace the first script with the following:
<Target Name="Test" DependsOnTargets="Clean; Compile"> <MakeDir Directories="$(ReportsPath)\coverage" /> <Exec Command="regsvr32.exe /s lib\PartCover\PartCover.dll" /> <Exec Command="lib\PartCover\PartCover.exe --target "$(MSTestPath)\MSTest.exe" --target-args "/noisolation /testcontainer:$(TestFiles)" --include "$(ReportNamespaces)" --output "$(ReportsPath)\coverage\partcover.xml"" ContinueOnError="true" /> <Exec Command="regsvr32.exe /u /s lib\PartCover\PartCover.dll" /> </Target>