Tuesday, March 29, 2011

Using PartCover 4.x to find code coverage

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.
  1. Create C# Class Library project, name it as ‘Mathematics’

  2. 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;
    else
    return dividend / divisor;
    }

    Here I would like to write automated unit test for Divide method to cover both if and else part.

  3. Now create another C# Class Library project to write unit test. Name this project as ‘Mathematics.Tests’

  4. Rename class1.cs file to ArithmeticTestFixture.cs

  5. 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

  6. Add reference to Mathematics project

  7. 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
          }
        }
      }


  8. Compile both project

  9. 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:

    1. Run ‘Command Prompt’ as an administrator. Another headache of using vista/windows server 2008 :):)

    2. 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.

    3. 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

    This will force both PartCover and PartCover browser to work on 32 bit.

  10. 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

  11. CorFlags.exe “C:\Program Files\Gallio\bin \ Gallio.Echo.exe” /32BIT+ /FORCE

  12. Now run PartCover.Browser.exe as an administrator

  13. Click on File => Run Target option to open ‘Run Target Settings’ dialog as below:


    1. Set ‘Executable File’ as C:\Program Files\Gallio\bin \ Gallio.Echo.exe

    2. Set Working Directory to Debug or Release folder in Bin directory of Mathematics.Tests project based on the configuration

    3. Specify the name of the test assembly in ‘Working Arguments’ as below:
      Mathematics.Tests.dll

    4. 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.



    5. Click on ‘Start’ button to find the code coverage.

    6. After PartCover finishes executing tests you will receive error message as


    7. Now open Properties window of ‘Mathematics.Test ‘ project

    8. Go to Build tab and set Platform target to ‘x86’ as shown below.

    9. Save the properties and rebuild the project

    10. 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

    11. 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.

1 comment: