Is there a way to test code coverage within visual studio if I\'m using MSTest? Or do I have to buy NCover?
Is the NCover Enterprise worth the money or are the old
(Note, this answer (here/below) is for DotNet FRAMEWORK. I've created a dotnet-core answer here : How to get code coverage report in donetcore 2 application )
...............................
For future readers:
Wow, this was NOT fun. I hope this helps someone out there in internet land.
Please note that the existence of "CodeCoverage.exe" may depend on the version of Visual Studio you have. And you may have to install VS (some enhanced version) in the build server.
set __msTestExe=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe
set __codeCoverageExe=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe
rem (the below is a custom C# console application, code seen below)
set __customCodeCoverageMergerExe=CoverageCoverterConsoleApp.exe
rem below exe is from https://www.microsoft.com/en-us/download/details.aspx?id=21714
set __msXslExe=C:\MyProgFiles\MsXslCommandLine\msxsl.exe
REM the below calls will create the binary *.coverage files
"%__codeCoverageExe%" collect /output:"D:\BuildStuff\TestResults\AAA_DynamicCodeCoverage.coverage" "%__msTestExe%" /testcontainer:"D:\BuildStuff\BuildResults\My.UnitTests.One.dll" /resultsfile:"D:\BuildStuff\TestResults\My.UnitTests.One.trx"
"%__codeCoverageExe%" collect /output:"D:\BuildStuff\TestResults\BBB_DynamicCodeCoverage.coverage" "%__msTestExe%" /testcontainer:"D:\BuildStuff\BuildResults\My.UnitTests.Two.dll" /resultsfile:"D:\BuildStuff\TestResults\My.UnitTests.Two.trx"
"%__codeCoverageExe%" collect /output:"D:\BuildStuff\TestResults\CCC_DynamicCodeCoverage.coverage" "%__msTestExe%" /testcontainer:"D:\BuildStuff\BuildResults\My.UnitTests.Three.dll" /resultsfile:"D:\BuildStuff\TestResults\My.UnitTests.Three.trx"
rem below, the first argument is the new file, the 2nd through "N" args are the result-files from the three "%__codeCoverageExe%" collect above
rem this will take the three binary *.coverage files and turn them into one .xml file
"%__customCodeCoverageMergerExe%" "D:\BuildStuff\TestResults\DynamicCodeCoverage.merged.coverage.converted.xml" "D:\BuildStuff\TestResults\AAA_DynamicCodeCoverage.coverage" "D:\BuildStuff\TestResults\BBB_DynamicCodeCoverage.coverage" "D:\BuildStuff\TestResults\CCC_DynamicCodeCoverage.coverage"
"%__msXslExe%" "D:\BuildStuff\TestResults\DynamicCodeCoverage.merged.coverage.converted.xml" "D:\BuildStuff\Xsl\VSCoverageToHtml.xsl" -o "D:\BuildStuff\TestResults\CodeCoverageReport.html"
You can also combine the 3 UnitTests.dlls into one call
REM the below calls will create the binary *.coverage files
"%__codeCoverageExe%" collect /output:"D:\BuildStuff\TestResults\ZZZ_DynamicCodeCoverage.coverage" "%__msTestExe%" /testcontainer:"D:\BuildStuff\BuildResults\My.UnitTests.One.dll" /testcontainer:"D:\BuildStuff\BuildResults\My.UnitTests.Two.dll" /testcontainer:"D:\BuildStuff\BuildResults\My.UnitTests.Three.dll" /resultsfile:"D:\BuildStuff\TestResults\My.UnitTests.AllOfThem.trx"
rem below, the first argument is the new file, the 2nd through "N" args are the result-files from the three "%__codeCoverageExe%" collect above
rem this will take the one binary *.coverage files and turn them into one .xml file
"%__customCodeCoverageMergerExe%" "D:\BuildStuff\TestResults\DynamicCodeCoverage.merged.coverage.converted.xml" "D:\BuildStuff\TestResults\ZZZ_DynamicCodeCoverage.coverage"
"%__msXslExe%" "D:\BuildStuff\TestResults\DynamicCodeCoverage.merged.coverage.converted.xml" "D:\BuildStuff\Xsl\VSCoverageToHtml.xsl" -o "D:\BuildStuff\TestResults\CodeCoverageReport.html"
VSCoverageToHtml.xsl
I also found some xsl in the internet. (the 3 links below are pretty much the same xsl)
http://codetuner.blogspot.com/2011_09_01_archive.html
http://jp.axtstar.com/?page_id=258
http://codetuner.blogspot.com/2011/09/convert-mstest-code-covarage-results-in.html
I'm posting the xsl here "just in case" those URL's die in the future. Put the below xsl in a file called "VSCoverageToHtml.xsl" (as referenced above).
Code Coverage Report
Code Coverage Report
Name
Blocks Covered
Blocks Not Covered
Coverage
[+]
background-color:
#86ed60;
#ffff99;
#FF7979;
%
0.00%
Here is a small command line tool to help.
https://www.microsoft.com/en-us/download/details.aspx?id=21714
using System;
using Microsoft.VisualStudio.Coverage.Analysis;
using System.Collections.Generic;
/* References
\ThirdPartyReferences\Microsoft Visual Studio 11.0\Microsoft.VisualStudio.Coverage.Analysis.dll
\ThirdPartyReferences\Microsoft Visual Studio 11.0\Microsoft.VisualStudio.Coverage.Interop.dll
*/
namespace MyCompany.VisualStudioExtensions.CodeCoverage.CoverageCoverterConsoleApp
{
class Program
{
static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Coverage Convert - reads VStest binary code coverage data, and outputs it in XML format.");
Console.WriteLine("Usage: ConverageConvert ... ");
return 1;
}
string destinationFile = args[0];
//destinationFile = @"C:\TestResults\MySuperMergedCoverage.coverage.converted.to.xml";
List sourceFiles = new List();
//files.Add(@"C:\MyCoverage1.coverage");
//files.Add(@"C:\MyCoverage2.coverage");
//files.Add(@"C:\MyCoverage3.coverage");
/* get all the file names EXCEPT the first one */
for (int i = 1; i < args.Length; i++)
{
sourceFiles.Add(args[i]);
}
CoverageInfo mergedCoverage;
try
{
mergedCoverage = JoinCoverageFiles(sourceFiles);
}
catch (Exception e)
{
Console.WriteLine("Error opening coverage data: {0}", e.Message);
return 1;
}
CoverageDS data = mergedCoverage.BuildDataSet();
try
{
data.WriteXml(destinationFile);
}
catch (Exception e)
{
Console.WriteLine("Error writing to output file: {0}", e.Message);
return 1;
}
return 0;
}
private static CoverageInfo JoinCoverageFiles(IEnumerable files)
{
if (files == null)
throw new ArgumentNullException("files");
// This will represent the joined coverage files
CoverageInfo returnItem = null;
string path;
try
{
foreach (string sourceFile in files)
{
// Create from the current file
path = System.IO.Path.GetDirectoryName(sourceFile);
CoverageInfo current = CoverageInfo.CreateFromFile(sourceFile, new string[] { path }, new string[] { path });
if (returnItem == null)
{
// First time through, assign to result
returnItem = current;
continue;
}
// Not the first time through, join the result with the current
CoverageInfo joined = null;
try
{
joined = CoverageInfo.Join(returnItem, current);
}
finally
{
// Dispose current and result
current.Dispose();
current = null;
returnItem.Dispose();
returnItem = null;
}
returnItem = joined;
}
}
catch (Exception)
{
if (returnItem != null)
{
returnItem.Dispose();
}
throw;
}
return returnItem;
}
}
}
Also see:
Code Coverage files merging using code in VS 2012 Dynamic Code Coverage