Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure the location of .coverage file when using /Enablecodecoverage in vstest.console.exe via command line

Is there a way to set the location of where the .coverage file would be located if we set /Enablecodecoverage in vstest.console.exe?

I did not see an option in the command line call itself. Is it to be set in the .runsettings file?

like image 993
rsjohnso Avatar asked Jan 30 '17 18:01

rsjohnso


1 Answers

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. enter image description here 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.

like image 145
stefan Avatar answered Oct 07 '22 20:10

stefan