I'm trying to integrate the sonar analysis into by TeamCity build process. I have a NUnit build step which runs my unit tests and then runs dotCover for the coverage.
My next step is the sonar-runner. The configuration that currently exists is; gallio.mode=dotCover, sonar.gallio.mode=reuseReport but I also need sonar.gallio.reports.path.
Does anybody know the path to the dotCover report generated in the the previous step?
Spent some amount of time on the same issue, but with newer Sonar c# plugin (v.2.3) - Gallio support has been dropped, but the report is still required.
To answer the question directly, TeamCity puts dotcover snapshot file into a temp folder with a name like coverage_dotcover27574681205420364801.data
(where digits are random). So
$snapshot = Get-ChildItem "%system.teamcity.build.tempDir%" `
-Filter coverage_dotcover*.data `
| select -ExpandProperty FullName -First 1
%teamcity.dotCover.home%\dotCover.exe report `
/ReportType=HTML /Source="$snapshot" `
/Output="%sonar.coverageReport%"
Now you can specify your report in sonnar runner as sonar.cs.dotcover.reportsPaths='%sonar.coverageReport%'
Where %sonar.coverageReport% is a defined property in a TeamCity
It seems TeamCity 2017 no longer creates coverage_dotcover*.data files. Instead it creates *.dcvr files.
There are potentially multiple files which need to be merged before you can create the report. As a result the powershell need updating.
So using the steps provided by Oleksandr, just update the script to be:
$snapshotfiles = Get-ChildItem "%system.teamcity.build.tempDir%" `
-recurse -Filter *.dcvr `
| select -ExpandProperty Name
$snapshots = $snapshotfiles -join ";"
%teamcity.dotCover.home%\dotCover.exe merge /Source=$snapshots
/Output=dotcovermerge.dcvr
%teamcity.dotCover.home%\dotCover.exe report `
/ReportType=HTML /Source=dotcovermerge.dcvr `
/Output="%sonar.coverageReport%"
Then the property %sonar.coverageReport% can be passed to the sonarqube scanner. Btw, you need to create a parameter in TC for %sonar.coverageReport% e.g. "sonarcoverage.html"
I couldn't find a way to do this using the built in NUnit runner. I managed to get it working by using a powershell build step to manually call the required commands.
First step is to run the NUnit tests via Gallio within a dotCover cover call:
& dotCover cover `
/TargetExecutable="C:\Program Files\Gallio\bin\Gallio.Echo.exe" `
/TargetArguments="/report-type:XML /report-name-format:test-report /runner:IsolatedProcess /report-directory:.\Gallio .\Path\Test.dll" `
/Filters="+:WhatToCover" `
/Output=coverage.snapshot
The Gallio test report is then available to be picked up by Sonar with reuseReport, TeamCity automatically detects the test results.
You can make TeamCity directly process the coverage snapshot by writing a service message to standard output:
Write-Host "##teamcity[importData type='dotNetCoverage' tool='dotcover' path='coverage.snapshot']"
To get the coverage info into a format usable by Sonar you need to use the dotCover report command and the undocumented report type TeamCityXML:
& dotCover report /Source=coverage.snapshot /Output=coverage-report.xml /ReportType=TeamCityXML
We are using SonarScanner for MSBuild and needed to add the team city temporary build path to the begin step.
/d:sonar.cs.dotcover.reportsPaths="%system.teamcity.build.tempDir%".
Build your project using MSBuild
Run your test tool, instructing it to produce a report at the same location specified earlier to the MSBuild SonarQube Runner
Run the SonarScanner.MSBuild.exe end command
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