Importing Code Coverage Results in the NDepend Code Model
- How to specify coverage results files to import in NDepend project?
- How to specify coverage results files to import in Azure DevOps / TFS extension?
- Why importing coverage results files?
- Exclusion of code elements from coverage statistics with an attribute
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.
The dialog can be directly accessed from Visual Studio menu: NDepend > Coverage > Import Code Coverage Data
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.
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.
- 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.
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 2022, 2019, 2017, 2015, 2013, 2012 and 2010 Code Coverage
NDepend supports both Visual Studio coverage file formats:
- The binary format, file extension .coverage. This format is only readable from Windows, not from Linux or MacOS. The reason is that the DLL provided by Microsoft to read this format is compiled against .NET Framework and cannot be executed on Linux or MacOS.
The XML format, file extension .coveragexml since Visual Studio 2010, and .xml for previous versions. This format can be read on all OS.
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 coverage requires <DebugType>Full</DebugType> to be set both on unit test projects and tested projects.
.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:
To obtain code coverage from within the NDepend Azure DevOps / TFS extension, just tick the Code Coverage Enabled option in the Test task.
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"
NCrunch 4.5.0 and above
Since NCrunch version 4.5.0 released in November 2020, NCrunch can export coverage data to OpenCover format XML files.
NDepend can import coverage data from OpenCover format XML files as explained in the section above.
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"?>
Also the UI has a special option to export to 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