After struggling alot and reading so many blogs, to use PartCover 4.0 to find code coverage, at last I was able to configure PartCover 4.0 to find out code coverage for Automated Unit Tests written using Gallio - Automation platform for .NET.
As I am currently working on SharePoint 2010 Technologies, it forced me to use 64 bit windows OS. Along with 64 bit OS, .NET Framework 4.0 framework was another challenge as I started using the framework when it was in Beta version. But thanks to Shaun Wilde who has taken great efforts to prepare PartCover for .NET CLR 4.0.
Please find the url as below, those who want to download PartCover 4.0.
Below are the steps to configure PartCover 4.0 on 64 bit OS and to find coverage for Unit Tests written using Gallio.
- Create C# Class Library project, name it as ‘Mathematics’
- Rename class1.cs file to Arithmetic.cs and add following method in it
public int Divide(int dividend, int divisor)
{
if (divisor == 0)return 0;elsereturn dividend / divisor;}
Here I would like to write automated unit test for Divide method to cover both if and else part. - Now create another C# Class Library project to write unit test. Name this project as ‘Mathematics.Tests’
- Rename class1.cs file to ArithmeticTestFixture.cs
- Add reference to Gallio 3.2 and MBUnit 3.2 assemblies in the test project. To refer these assemblies you need to install Gallio. You can download Gallio from following URL
http://www.gallio.org/Downloads.aspx - Add reference to Mathematics project
- Delete all contents from the ArithmeticTestFixture.cs file and copy following code into it.
- using MbUnit.Framework;
- namespace Mathematics.Tests
- {
- [TestFixture]
- public class ArithmeticTestFixture
- {
- [Test]
- public void DivideTest()
- {
- int dividend = 4;
- int divisor = 2;
- int expected = 2;
- Arithmetic arithmetic = new Arithmetic();
- int actual = arithmetic.Divide(dividend, divisor);
- Assert.AreEqual(expected, actual); //Test else condition
- expected = 0;
- divisor = 0;
- actual = arithmetic.Divide(dividend, divisor);
- Assert.AreEqual(expected, actual); //Test if condition
- Compile both project
- Before we find percentage of code covered by the unit tests using PartCover, we need to make it to work on 32 bit OS. To do this:
- Run ‘Command Prompt’ as an administrator. Another headache of using vista/windows server 2008 :):)
- Set directory to folder in which CorFlags.exe resides. It should be “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin” for Windows Server 2008. It is path for latest Windows SDK installed on the machine.
- Now we need to force PartCover executables to work on 32 bit. To do this run following commands on command prompt
CorFlags.exe “C:\Program Files (x86)\PartCover\PartCover .NET 4.0\ PartCover.Browser.exe” /32BIT+ /FORCE
CorFlags.exe “C:\Program Files (x86)\PartCover\PartCover .NET 4.0\ PartCover.exe” /32BIT+ /FORCE
- We also need to force ‘Gallio.Echo.exe’ to work on 32 bit. We need to do this because we are going to use it run our unit tests. Run the following command to do this
- Now run PartCover.Browser.exe as an administrator
- Click on File => Run Target option to open ‘Run Target Settings’ dialog as below:
- Set ‘Executable File’ as C:\Program Files\Gallio\bin \ Gallio.Echo.exe
- Set Working Directory to Debug or Release folder in Bin directory of Mathematics.Tests project based on the configuration
- Specify the name of the test assembly in ‘Working Arguments’ as below:
Mathematics.Tests.dll
- In ‘Rules’ text box enter following rule to find code coverage for Arithmetic class in Mathematics assembly
+[Mathematics]Mathematics.Arithmetic
Common form for rules is
+[<Assembly Name>]<Namespace>
Regexp can contains digits, letters, asterisks and pluses. Asterisk means zero or more letters and digits in template, plus means inner state of classes.
Please refer PartCover Console Manual for more details.
- Click on ‘Start’ button to find the code coverage.
- After PartCover finishes executing tests you will receive error message as
- Now open Properties window of ‘Mathematics.Test ‘ project
- Go to Build tab and set Platform target to ‘x86’ as shown below.
- Save the properties and rebuild the project
- Come back PartCover again and click ‘Go to inputs’ on the error message box (if you have not closed it) or else click on File => Run Targets option to open Run Target Settings again
- Again hit Start button. Now you will see the code coverage for Arithmetic class as below
I hope this will help to people who are new or facing problem in finding code coverage using PartCover 4.0 and Gallio on 64 bit Windows OS.
CorFlags.exe “C:\Program Files\Gallio\bin \ Gallio.Echo.exe” /32BIT+ /FORCE