2

I have started using Sonarqube and I have setup a local sonar server to test how it works.

Earlier I used /d:sonar.cs.vscoveragexml.reportsPaths and generated .coveragexml file. Now I'm trying to generate .trx files using MSTest commands.
So these are the commands I have used to run the sonar analysis.

MSBuild.SonarQube.Runner.exe begin /k:"93ca937be91ab25536462fgdfg566915" /n:"Solution" /v:"1" /d:sonar.cs.vstest.reportsPaths="C:\SonarQube\Solution.trx"

MSBuild.exe "Solution.sln" /t:Rebuild /p:Configuration=Release

MSTest /testcontainer:.\SolutionTests\bin\Release\SolutionTests.dll /resultsfile:"C:\SonarQube\Solution.trx"

MSBuild.SonarQube.Runner.exe end

After running all these commands in a command prompt, it shows code coverage as 0% and shows the no of tests run as 22. enter image description here

Is there any other command which i am missing to get the code coverage. I understand there is a command something like below :

"C:\Program Files (x86)\Microsoft Visual Studio\2017\TestAgent\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:"C:\SonarQube\Solution.trx"

I'm unable to find the exact command to analyze .trx file. If anyone can help in this matter, it would be very helpful. Many thanks in advance.

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91

1 Answers1

0

SonarQube documentation is not good in explaining all features.

You need to do two things-

One is for Unit Test case execution using trx file that will show number of unit test cases.
Second is you need to analyze the .coveragexml file which is generated by CodeCoverage.exe when you analyze the code coverage. To explain this- once Unit tests are executed then .trx file is logger file and .coverage file is having coverage data. This .coverage file when analyzed by CodeCoverage.exe then it generates .coveragexml file which we need to pass to sonar scanner for msbuild for analyzing and generating report. So, your command lines will be like below-

MSBuild.SonarQube.Runner.exe begin /k:"93ca937be91ab25536462fgdfg566915" /n:"Solution" /v:"1" /d:sonar.cs.vstest.reportsPaths="C:\SonarQube\*.trx" /d:sonar.cs.vscoveragexml.reportsPaths="C:\SonarQube\*.coveragexml"

MSBuild.exe "Solution.sln" /t:Rebuild /p:Configuration=Release

//now you should try to run test cases and get .coverage file. I am using vstest. Please check for your vstest.console.exe path as per your Visual Studio installation

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /EnableCodeCoverage "%CD%\SolutionTests\bin\Release\SolutionTests.dll" /ResultsDirectory:"%CD%" /Logger:trx
 
//now as .coverage file is there, we will analyze it and generate .coveragexml using CodeCoverage.exe

"C:\Program Files (x86)\Microsoft Visual Studio\2017\TestAgent\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:"C:\SonarQube\VSCoverage.coveragexml" "<give here path for .coverage file"

MSBuild.SonarQube.Runner.exe end
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103