Importing Code Coverage Results in the NDepend Code Model

Make the most of your code coverage data with NDepend (2 minutes)

How to specify coverage results files to import in NDepend project?

Code coverage data is imported from coverage result files generated by the coverage tools.

Coverage results files can be specified from NDepend > Project Properties > Analysis > Code Coverage.

In this dialog you can also specify a directory. NDepend will attempt to find one or several coverage files in this directory at analysis time.

ndepend get coverage file from directory setting

The dialog can be directly accessed from Visual Studio menu: NDepend > Coverage > Import Code Coverage Data

ndepend import coverage data menu

Code coverage result files imported might be generated by different tools. In such situation NDepend will merge data properly.

How to specify coverage results files to import in Azure DevOps / TFS extension?

If you have a test task with Code Coverage Enabled, NDepend analysis will automatically import the resulting code coverage. Results will be significantly more useful with coverage imported.

However if you are running a specific or custom task to gather code coverage with a technology different than Microsoft Visual Studio coverage you can still use the Get Coverage Files from a specified directory NDepend task option option. NDepend will be smart enough to determine for each coverage file (there can be one or many), which coverage technology has been used to generate it and how to import this data.

ndepend azure devops extension get coverage file from a specified directory

Why importing coverage results files?

  • The global coverage percentage is shown in the NDepend dashboard. Also coverage results can be diff-ed with the baseline coverage results.
  • code coverage ratio and delta in the ndepend dashboard
  • Trend charts can be obtained for coverage data.
  • Several default code rules are about validating code coverage and it is easy to write your own rules.
  • Harness search facilities to explore the coverage data.
  • Visualize the code coverage data through colored treemaping in the metric view.

    For example the screenshot below shows code coverage by tests on the NDepend code base.

    NDepend Code Coverage visualized with a colored treemap

Exclusion of code elements from coverage statistics with an attribute?

All methods and types tagged with System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute will be excluded from NDepend coverage statistics, no matter the coverage technology used.

All methods and types tagged with System.Diagnostics.DebuggerNonUserCodeAttribute or System.Diagnostics.DebuggerHiddenAttribute will be excluded from NDepend coverage statistics only for Visual Studio coverage files.

In VisualNDepend menu: Project Properties > Analysis > Code Coverage > Settings you can provide your own exclusion attribute. See the screenshot in the previous section, there YourNamespace.ExclusionAttribute is used for example.

In such case you need to specify both the full namespace qualifier and the Attribute suffix.

Visual Studio 2019, 2017, 2015, 2013, 2012 and 2010 Code Coverage

NDepend supports both Visual Studio coverage file formats:

  • The binary format, file extension .coverage.
  • The XML format, file extension .coveragexml since Visual Studio 2010, and .xml for previous versions.

    Only the XML format with <CoverageDSPriv> root element is supported.

    An other less common XML format exists with a <results> root element and same extension .coveragexml. This other format is not supported.

.NET Core

.NET Core coverage requires <DebugType>Full</DebugType> to be set both on unit test projects and tested projects.

More information available here.

.coverage result files can be obtained with the tool VSTest.Console.exe with the /EnableCodeCoverage option, for example:

"%programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\VSTest.Console.exe" ^
 "..\bin\netcoreapp2.1\Tests1.dll" ^
 "..\bin\netcoreapp2.1\Tests2.dll" ^
 /EnableCodeCoverage /InIsolation 

You can manually export XML coverage files from binary coverage files from Visual Studio with this option:

export visual studio coverage from xml to binary format

To obtain code coverage from within the NDepend Azure DevOps / TFS extension, just tick the Code Coverage Enabled option in the Test task.

enable code coverage in the ndepend azure devops extension

OpenCover

Just reference OpenCover xml coverage files created with OpenCover.Console.exe, or reference a directory that contains these files.

Typically OpenCover.Console.exe will be invoked this way to obtain OpenCover xml coverage files.

"%LOCALAPPDATA%\Apps\OpenCover\OpenCover.Console.exe" ^
-output:"%CD%\OpenCoverDateFile.xml" ^ 
-register:user ^ 
-target:"%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" ^ 
-targetargs:"UnitTestProject1\bin\Debug\UnitTestProject1.dll"

JetBrains DotCover 1.1 and above

By default DotCover outputs XML coverage file. However NDepend cannot parse the default DotCover XML coverage file. You need to configure DotCover to output NDepend XML files. This can be done this way in your DotCover configuration file:

<?xml version="1.0" encoding="utf-8"?>
<AnalyseParams>
  <Executable>C:\App\Bin\MyApp.exe</Executable>
  <Output>..\DotCoverCoverage.xml</Output>
  <ReportType>NDependXml</ReportType>
</AnalyseParams>

Also the UI has a special option to export to NDepend:

jetbrains dotcover export to xml for ndepend

See also this article about using TeamCity, DotCover and NDepend, by Laurent Kempe:

Using TeamCity integrated DotCover coverage files with NDepend

NCover 3.X and above

NDepend reads NCover v3 XML format, hence if you are using NCover v4 or above, you need to export your coverage file with v3 format as explained in this NCover documentation. This looks like :

ncover export --project="MyProject" --execution="3/12/2017" --file="C:\Users\Support\Desktop\Report\coverage-export" --format=v3xml

If you are using TestDriven.NET and/or NCoverExplorer.exe you can access folder that contains coverage file from the Explore Coverage Folder menu:

test driven net explore coverage foder