Configure the location of .coverage file when using /Enablecodecoverage in vstest.console.exe via command line
As stated in General Command Line Options that /Enablecodecoverage
uses default settings if setting file is not specified.
I did not see an option in the command line call itself. Is it to be set in the .runsettings file?
Yes, you have to customize your .runsettings file applies whenever you use Analyze Code Coverage.
To customize run settings in a command line test
Launch the Visual Studio Developer Command Prompt:
On Windows Start, choose All Programs, Microsoft Visual Studio, Visual Studio Tools, Developer Command Prompt.
Run:
vstest.console.exe MyTestAssembly.dll /EnableCodeCoverage /Settings:CodeCoverage.runsettings
To customize run settings in a build definition
You can get code coverage data from a team build.
Note: Make sure your .runsettings file is checked in.
Edit:
vstest.console.exe by default creates a *.coverage file, then the file can be converted to *.xml format.
To get the *.coverage file you can use the following command:
"c:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "PATH_OF_YOUR_EXECUTABLE_OR_DLL" /InIsolation /EnableCodeCoverage
Create a new command line project in Visual Studio and add a reference to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.Coverage.Analysis.dll
.
Add the following code (paths are hard coded here, but could be supplied as arguments):
using Microsoft.VisualStudio.Coverage.Analysis;
namespace CoverageConverter
{
class Program
{
static void Main(string[] args)
{
using (CoverageInfo info = CoverageInfo.CreateFromFile(
"PATH_OF_YOUR_*.coverage_FILE",
new string[] { @"DIRECTORY_OF_YOUR_DLL_OR_EXE"},
new string[] { }))
{
CoverageDS data = info.BuildDataSet();
data.WriteXml("converted.coveragexml");
}
}
}
}
CodeCoverage.exe is another coverage tool to convert into *.xml format read more.
Edit 2:
You can use the /UseVsixExtensions
option with /EnableCodeCoverage
option to enable code coverage:
"C:\Program Files (x86)\Microsoft Visual Studio 11.0
\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
/UseVsixExtensions:true /EnableCodeCoverage "C:\Users\YourName\Documents\Visual Studio
2012\Projects\YourProjectFolder\YourApp.Tests\bin\Debug\YourApp.Tests.dll"
Above command will generate the .coverage file under the directory TestResults.
To generate the .coverage file to a specified directory you will have to use CodeCoverage.exe with vstest.console.exe. Following will be exact command:
"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" collect /output:"%CD%\VisualStudio.coverage" "%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "UnitTestProject1\bin\Debug\UnitTestProject1.dll"
Specify the path and name of your coverage file in output parameter above. You don't even have to specify /EnableCodeCoverage.
You can change the name with a CoverageFileName
node in a runsettings file and I believe set the output location with a ResultsDirectory
node like so:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<!-- Path relative to directory that contains .runsettings file-->
<ResultsDirectory>.\TestResults</ResultsDirectory>
</RunConfiguration>
<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>
<CoverageFileName>myname.coverage</CoverageFileName>
<CodeCoverage>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
Then run from command line:
vstest.console.exe MyTestAssembly.dll /EnableCodeCoverage /Settings:myrunsettings.runsettings
Note: I would appreciate if someone could test that as I don't have Visual Studio 2019 Enterprise and as of 2019, Enterprise is required to gather code coverage (it was available previously due to a bug).