[Coverage & Test Data] Generate Reports for C#, VB.net

To include test coverage in your analysis, you’ll need to execute your unit tests just after build and before the SonarScanner for MSBuild’s end step. We’ve documented here how to execute tests for a few common engines.

Visual Studio Code Coverage

Run Unit Tests & Coverage

"%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /EnableCodeCoverage "UnitTestProject1\bin\Debug\UnitTestProject1.dll"
Convert the Code Coverage Report from Binary into XML

"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output: "%CD%\VisualStudio.coveragexml" "%CD%\VisualStudio.coverage"

Note: If you use dotnet instead of vstest, the commands are slightly different, but the output is the same (.coverage and .xml files)
dotnet test ... --collect "DotnetCodeCoverage"
CodeCoverage.exe analyze /output: "%CD%\DotnetCoverage.coveragexml" "%CD%\DotnetCoverage.coverage"

dotCover

Run Unit Tests & Coverage

"%LOCALAPPDATA%\JetBrains\Installations\dotCover02\dotCover.exe" analyse /ReportType=HTML /Output="%CD%\dotCover.html" "/TargetExecutable=%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /TargetWorkingDir=. "/TargetArguments=UnitTestProject1\bin\Debug\UnitTestProject1.dll"

OpenCover

Run Unit Tests & Collect Code Coverage

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

NCover 3

Run Unit Tests & Collect Code Coverage

"%ProgramFiles(x86)%\NCover\NCover.Console.exe" "%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "UnitTestProject1\bin\Debug\UnitTestProject1.dll" //x "%CD%\coverage.nccov"

VSTest

Run Unit Tests and Save Results in the “TestResults” folder using a generated *.trx filename

"%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /Logger:trx "UnitTestProject1\bin\Debug\UnitTestProject1.dll"

NUnit

Run Unit Tests and Save Results in file "NUnitResults.xml"

packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console.exe --result=NUnitResults.xml "NUnitTestProject1\bin\Debug\NUnitTestProject1.dll"

or, for older NUnit 2

"%ProgramFiles(x86)%\NUnit 2.6.4\bin\nunit-console.exe /result=NUnitResults.xml "NUnitTestProject1\bin\Debug\NUnitTestProject1.dll"

xUnit

Run Unit Tests and Save Results in file "XUnitResults.xml"

packages\xunit.runner.console.2.1.0\tools\xunit.console.exe XUnitProject1\bin\Debug\XUnitProject1.dll -xml XUnitResults.xml

3 Likes