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.
TeamCity Unit test step:
Followed by Powershell script:
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.
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With