PartCover Integration with MSBuild

Part Cover Integration with MSTest MSBuild.
Introduction:
Part Cover is an Open Source tool to calculate unit test code coverage. Current Version of this tool is Part Cover 2.2. Following are the Steps required to integrate the PartCover into MSBuild.
Prerequisites:
1) Part Cover Installed into your Machine.
2) Microsoft Visual Studio For .NET.
3) MSTest / any Unit Test Framework.
Part Cover comes along with its GUI PartCover Browser .This application is GUI wrapper over PartCover.exe (console version).
Parameters for running PartCover.exe (console version).

--target=
Argument specifies path to executable file to count coverage. may be either full path or relative path to file.

e.g. MSTest.exe or any executable for your unit tests Frame work.
--target-work-dir=
Argument specifies working directory to target process. By default, working directory will be working directory for PartCover.exe

e.g. it may be your own bin/Lib directory.
--target-args=
Argument specifies arguments for target process. If target argument contains spaces - quote . If you want specify quote (") in , then precede it by slash (\)

Arguments of your target executable i.e. MSTest.

e.g .Test Assemblies
--include=, --exclude=
These two specifies item to include or exclude from report. Item is in following format:
[]
where is simple regular expression, containing only asterix and characters to point item. For example:
[mscorlib]*
[System.*]System.IO.*
[System]System.Colle*
[Test]Test.*+InnerClass+SecondInners*
This is called Rule We can define Rules with these two switches –include and --exclude
--settings=
This specifies input settings in xml file.
--generate=
This generates setting file using settings specified. By default, file generated will be processed via console output.
--output=
This specifies output file for writing result xml. It will be placed in UTF-8 encoding. By default, output data will be processed via console output.

Output XML File which has Coverage related Data for your Test Assemblies. This can be then after transformed into the Code Coverage Reports using XSLT’s.
--log=:
This specifies log level for driver. If greater than 0, log file will be created in working directory for PartCover.exe.
--version
This shows version of PartCover.exe console application.



Integration with MSBuild:

Please Follow the Following steps to integrate PartCover into your MSBuild.
A) Infrastructure

1) As By Default PartCover get installed into your Program Files (C:\Program Files\Gubka Bob). Now we can copy whole PartCover directory to our Lib/Bin directory for integration purpose. So that we can use relative path.

e.g. we can create Partcover Folder under our Lib/Bin Directory in the same directory we can make another PartcoverBin Directory where we copy all the files (Binary as well as some other files that are necessary for the Binary).

e.g. Create Folder/ directory as: Lib\Part Cover\PartCoverBin. Copy all the Files into this directory related to PartCover.

2) Installation of PartCover comes along with two XSLT We can use these two XSLT’s to transform the output XML of PartCover.exe (Console) into HTML Reports.


For the same we can Copy these XSLT by creating folder called XSLT in to your PartCover Folder which is under your Lib/Bin Directory

e.g. Create Folder /directory as: Lib\Part Cover\XSLT.


3) Now create Output Directory / Folder where your XML output and Reports will be generated

e.g. Create Folder /directory as: Lib\Part Cover\Reports.


4) To Transform the XML Output file into HTML reports using XSLT We need utility called msxsl.exe which is command line tool of Microsoft used to transform the XML into html using XSLT Transformation.

Copy this command line tool at Lib\Part Cover.



B) Create Batch File


After Infrastructure what we need to do is to Create Batch file having the Commands to execute the PartCover and automate the integration of PartCover with MSBuild.

Following is the Sample Content of my Batch File. We can create the Batch File Variables to set Path for Binaries, Target Assemblies and Output Reports. Rests all are the commands necessary to execute code coverage analysis tool partcover.exe and generate the Reports.

Name this file as PartCover.bat and save it under your PartCover Directory.
@echo off
SET RootPath=C:\p4\V50\DEV\B11\Src\Lib
SET ReportPath=%RootPath%\PartCover\Reports\
SET StylesheetPath=%RootPath%\PartCover\xslt\
"%RootPath%\PartCover\PartCoverBin\PartCover.exe" --target "%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\MSTest.exe" --target-args "/noisolation /testcontainer:%RootPath%\Framework.Security.Tests.dll /testcontainer:%RootPath%\Framework.Logging.Tests.dll /testcontainer:%RootPath%\Framework.Common.Tests.dll /testcontainer:%RootPath%\Framework.Caching.Tests.dll /testcontainer:%RootPath%\Framework.Data.Tests.dll /testcontainer:%RootPath%\Framework.Caching.Tests.dll " --include "[Framework*]*" --target-work-dir %RootPath% --output "%ReportPath%Partcover.xml"
"%RootPath%\PartCover\msxsl.exe" "%ReportPath%Partcover.xml" "%StylesheetPath%Report By Assembly.xslt" -o "%ReportPath%ReportByAssembly.html"
"%RootPath%\PartCover\msxsl.exe" "%ReportPath%Partcover.xml" "%StylesheetPath%Report By Class.xslt" -o "%ReportPath%ReportByClass.html"


C) Integrate the same with MSBuild

We can call this batch File from our Build Following is modification of .csproj file for the integration

<Target Name="AfterCompile">
<Exec Command='"..\..\..\Lib\PartCover\PartCover.bat"' ContinueOnError='true'>
<Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
</Exec>
<Message Text="The exit code is $(ErrorCode)"/>
<Error Text="Error while executing BAT file" Condition="'$(ErrorCode)' > '0'" />
<OnError ExecuteTargets="MessageErrorHandler"/>
</Target>

<!--Error Handler-->
<Target Name="MessageErrorHandler">
<Message Text="An error has occurred while running the batch file"/>
</Target>


D) Result of Build
It will create the XML Output with Two Reports ReportByAssembly.html and ReportByClass.html into Reports directory.

Comments

Rahul said…
NET tools for developers, including a code and memory profiler.

Php Developer India

Popular Posts