Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Team City Code Coverage and Unit Test results not showing on Sonar

I am trying to upload Unit Test and dotCover Code Analysis results from TeamCity to Sonar server. It shows code coverage and unit test results in the TeamCity but no code coverage/unit test on Sonar.

enter image description here

TeamCity Unit test step:

enter image description here

Followed by Powershell script:

enter image description here

I have the following additional parameter in Sonar Runner step:

Dsonar.cs.vstest.reportsPaths=TestResults.trc Dsonar.cs.dotcover.reportsPaths='%sonar.coverageReport%'

Does anyone know how to fix this?

Thanks.

like image 564
developer Avatar asked Dec 21 '15 14:12

developer


People also ask

How do I check my TeamCity code coverage?

Code Coverage in TeamCity The code coverage results can be viewed on the Overview tab of Build Results. A detailed report is displayed on the dedicated Code Coverage tab. The chart for code coverage is also available on the Statistic Charts tab of a build configuration.

How do I get code coverage results?

Code coverage option is available under the Test menu when you run test methods using Test Explorer. The results table shows the percentage of the code executed in each assembly, class, and procedure. The source editor highlights the tested code.

How do I generate test coverage report in SonarQube?

Test coverage reports are not generated by SonarQube itself. They must be generated by an external tool and then imported into SonarQube by specifying a parameter telling the scanner where to look for the report. The data is then displayed in your SonarQube analysis.

Does sonar give code coverage?

SonarQube measures code quality based on different metrics. The most important metric is the code coverage metric. In this case, no tests have been written, which means you have no code coverage. The cool thing about SonarQube is that it indicates the number of lines that aren't covered by tests.


2 Answers

Managed to fix the issue using the below script in step 3:

$Files= Get-ChildItem %system.teamcity.build.tempDir% ` 
    -Filter coverage_dotcover*.data ` 
    | where-object {$_.length -gt 50} ` 
    | Select-Object -ExpandProperty FullName 

$snapshot =[string]::Join(";",$Files) 

& %teamcity.tool.dotCover%\dotCover.exe merge ` 
  /Source=$snapshot ` 
  /Output=%env.TEMP%\dotCoverReport.dcvr` 


& %teamcity.tool.dotCover%\dotCover.exe report ` 
  /Source=%env.TEMP%\dotCoverReport.dcvr ` 
  /Output=%sonar.coverageReport% ` 
  /ReportType=HTML
like image 77
developer Avatar answered Jan 02 '23 11:01

developer


What TeamCity version do you have? In TeamCity 9.1.1 there was a bug that caused test report files to be written in build temp directory. In this case Sonar plugin, that expects trx file to be in checkout directory, does not find it. There are several ways to solve it: the first is to upgrade to TeamCity 9.1.2 and above, the second is to pass absolute path to the report paths variable:

-Dsonar.cs.vstest.reportsPaths=%system.teamcity.build.tempDir%/TestResults.trc

The third way would be to pass absolute path, that points to checkout directory, to results file field of mstest runner:

%system.teamcity.build.checkoutDir%/TestResults.trc

like image 31
Oleg Rybak Avatar answered Jan 02 '23 12:01

Oleg Rybak