How to get code coverage in javascript

Standard

Long back I did some posts on getting code coverage in C#. And one of my previous posts was about implementing TDD in JavaScript. This post is about getting JavaScript code coverage. In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite. A program with high code coverage has been more thoroughly tested and has a lower chance of containing software bugs than a program with low code coverage. – Wikipedia.

For getting code coverage I am using a JavaScript library, blanket.js. You can download it from the website. It very easy to enable code coverage using blanket.js. First you need to include the blanket.js file in your test runner html page. Then add the data- (data-cover) attribute to the script file, for you want to measure the code coverage.

<script src="scripts/blanket.min.js"></script>
<script src="scripts/functions.js" data-cover></script>

Once you run the HTML page again, you will see a check box, Enable coverage, if you check on this, it will populate and display the code coverage information.

Code coverage option enabled

Code coverage option enabled

I faced two problems while working with blanketjs.

  1. I tried to get code coverage from inline javascript script tag, but it was not working. It was throwing some exception like – Uncaught TypeError: Cannot read property ‘nodeValue’ of undefined.
  2. Instead of running from the web server, I tried to execute from normal HTML file, but it was also failing – It was throwing some exception like this – XMLHttpRequest cannot load file:///C:/Demo/scripts/functions.js. Cross origin requests are only supported for HTTP. Looks like blanket.js is trying to load the script function, but it was failing.

    Hope it helps.

    Happy Programming :)

How to exclude test assembly from code coverage in VS 2012

Standard

If you are using VS 2012, by default Code coverage results are available for Test assembly also.

Test Assembly coverage also displayed

There is no direct way to disable this behavior, either you need to use the ExcludeFromCodeCoverage attribute or you need to use the runsettings file.

The ExcludeFromCodeCoverage is pretty straight forward; you need to decorate all the unit test classes with this attribute, like this

[ExcludeFromCodeCoverage]
[TestClass] 
public class MathTest
{
}

But this attribute only works in Class, Structs, Constructors, Methods, Properties and Events. So if you want to disable code coverage for an entire assembly this will not work.

The other option is using runsettings file. To use this VS feature you need to add a new file with .runsettings extension to the solution. Copy the XML content below and paste it to the .runsettings file.

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage" 
                     uri="datacollector://Microsoft/CodeCoverage/2.0" 
                     assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
        <Configuration>
          <CodeCoverage>
            <ModulePaths>
              <Include>
                <ModulePath>.*\.dll$</ModulePath>
              </Include>
              <Exclude>
                <ModulePath>.*\.test.dll</ModulePath>
              </Exclude>
            </ModulePaths>
            <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
            <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
            <CollectFromChildProcesses>True</CollectFromChildProcesses>
            <CollectAspDotNet>False</CollectAspDotNet>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

Select the .runsettings file from Test > Test Settings > Select Test Settings file menu.

Select runsettings file

Now if you analyze the code coverage with the new runsettings file, only the code file assembly coverage will be displayed.

Only Code assembly coverage is displayed

You can disable the use of .runsettings file by un-checking it in the Test Settings Menu item.

You can find more details about ExcludeFromCodeCoverage attribute in MSDN

Getting Code coverage using Open Cover and NUnit

Standard

In the last post I blogged about how to get code coverage using MSTest and Part cover. In this post I am blogging about measuring code coverage with Open cover. Today I found another alternative to measure code coverage. Its called Open cover. You can download Open Cover from here. Compared to Part cover, Open cover doesn’t have a UI part. Also open cover gives better performance compared to Part cover. You can find Performance comparison here. You can find the OpenCover command line parameters in Github wiki.

In this example I am using a NUnit as the unit testing application. And the following command gets the code coverage in XML file.

"C:\Program Files (x86)\OpenCover\OpenCover.Console.exe"
-filter:"-[DataAccess.*]* +[DataAccess*]* -[DataAccess.Test]"
-target:"C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-console-x86.exe"
-register:user -targetargs:"/nologo DataAccess.Test.dll"
-output:coverage.xml

The filter part is similar like, PartCover application, like I am profiling only DataAccess component and I am not profiling DataAccess.Test application. And no need to specify

the filters to remove System and NUnit type of assemblies.

Open cover generate results as XML files and with the help of Report Generator application you can view the coverage details.

And here is the syntax to get the details using Report Generator.

ReportGenerator.exe" coverage.xml "C:\Study\CodeCoverage\"

And here is the report generated using ReportGenerator summary and next is the detailed report.

Code coverage summary

Code coverage summary

And next is the detailed report.

Detailed Coverage details

Detailed Coverage details

Happy Code coverage :)