Child pages
  • Integrating TeamCity dotCover Report with SonarQube Runner
Skip to end of metadata
Go to start of metadata

This guide assumes there is a Build Configuration (e.g. named "Tests + dotCover") building a .NET project and running tests with dotCover enabled (one or several build steps): 

Integrating with chained build

1. Create another Build Configuration (e.g. named "Send to SonarQube") and add the following 2 dependencies on initial configuration ("Tests + dotCover"):

a) A snapshot dependency

b) An artifact dependency for ".teamcity/.NETCoverage/dotCover.dcvr" file and getting artifacts from "Build from the same chain".

Note: since TeamCity version 2019.1, dotCover.dcvr is no longer a hidden artifact that is published by default. It is now created in the build temporary folder ("%system.teamcity.build.tempDir%\..\agentTmp\dotNetCoverageResults\dotCover.dcvr") and removed when the build finishes. To enable publishing of this artifact, specify the path to the .dcvr file explicitly in the Artifacts paths of a build configuration.

2. In the new configuration ("Send to SonarQube") add a Command Line step with the following script: 

%teamcity.dotCover.home%\dotCover.exe report /ReportType=HTML /Source="dotCover.dcvr" /Output="dotCover.html"

3. Add SonarQube Runner to the new configuration and add an additional command line argument with "-Dsonar.cs.dotcover.reportsPaths=dotCover.html"

Integrating with merging reports

After the last build step with enabled dotCover, add a new step to merge all the collected coverage into a single XML in a well-known place as described here.

  • No labels

8 Comments

  1. HI, I followed the steps above and I can see a report being pushed in SonarQube, however the coverage published in SonarQube is showing no coverage. The html report produced in the second step (dotCover.html) is showing the correct coverage stats, yet it has no sources. I am assuming the issue is due to the missing source code, can you please confirm if this would be a problem. Secondly, how do I get the sources included in the html report?

  2. It was very non-trivial to get this going.  For the scenario where you want to wrap the build and test with SonarScanner MsBuild runner, here is what we had to do to finally get it to work:

    1. Set Additional Parameters on SonarScanner MSBuild runner to: '/d:sonar.cs.dotcover.reportsPaths="coverage.html"'
    2. Verify that you are using SonarScanner 4.8+ so that it can work with msbuild 16 (VS 2019)
    3. Create a powershell build step immediately after the testing build step and before the sonarscanner msbuild end step:  Use the following Powershell:

    $snapshotfiles = Get-ChildItem "%system.teamcity.build.tempDir%\..\agentTmp" -recurse -Filter *.dcvr | select -ExpandProperty FullName
    $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="coverage.html"

    This will give you a coverage metric in SonarQube and will even highlight what code was covered by testing.  But it won't give you unit testing metrics.  Still working on figuring out how to get that to show up.

  3. Hi Mike, following on from your instructions, I have encountered the following error?

    [09:43:18] Step 3/6: SonarScanner for MSBuild: begin analysis (1s)

    [09:43:18] [Step 3/6] Starting: C:\BuildAgent\tools\sonar-scanner-msbuild.4.8.0.12008\MSBuild.SonarQube.Runner.exe "/d:project.home=." "/d:sonar.host.url=http://sonarqube.ourserver.com:9000" "/k:Main_1Builds_OurProject_CodeAnalysis" "/n:Debug - Code Analysis" "/v:2020.3.0.179" "/d:sonar.login=OUR_KEY" begin
    [09:43:18] [Step 3/6] in directory: C:\BuildAgent\work\9c853ec0ef280c7b
    [09:43:18] [Step 3/6]
    [09:43:18] [Step 3/6] Unhandled Exception: System.MissingMethodException: Method not found: '!!0[] System.Array.Empty()'.
    [09:43:18] [Step 3/6] at SonarScanner.MSBuild.Program.Execute(String[] args, Boolean showDeprecatedWarning)
    [09:43:18] [Step 3/6] at MSBuild.SonarQube.Runner.Program.Main(String[] args)
    [09:43:19] [Step 3/6] Process exited with code 255
    [09:43:20] [Step 3/6] Process exited with code 255 (Step: SonarScanner for MSBuild: begin analysis)
    [09:43:20] [Step 3/6] Step SonarScanner for MSBuild: begin analysis failed
    Any idea why this is happening?
    Note: sonar.host.url and sonar.login values have been masked out
    1. https://stackoverflow.com/questions/31253747/method-not-found-0-system-array-empty suggests that such exception could be thrown when needed .Net framework version is not installed on the machine. SonarScanner uses .Net 4.6. 

      1. Thanks Andrey.

        I rebuilt using an agent with .Net 4.6 and got below error:

         

        [10:21:30] Step 3/6: SonarScanner for MSBuild: begin analysis (17s)
        [10:21:30] [Step 3/6] Starting: C:\BuildAgent\tools\sonar-scanner-msbuild.4.8.0.12008\MSBuild.SonarQube.Runner.exe "/d:project.home=." "/d:sonar.host.url=http://sonarqube.ourserver.com:9000" "/k:Main_1Builds_OurProject_CodeAnalysis" "/n:Debug - Code Analysis" "/v:2020.3.0.179" "/d:sonar.login=Our_Key" begin
        [10:21:30] [Step 3/6] in directory: C:\BuildAgent\work\9c853ec0ef280c7b
        [10:21:31] [Step 3/6] WARNING: ------------------------------------------------------------------------
        [10:21:31] [Step 3/6] This executable is deprecated and may be removed in next major version of the SonarScanner for MSBuild. Please use 'SonarScanner.MSBuild.exe' instead.
        [10:21:31] [Step 3/6] ------------------------------------------------------------------------
        [10:21:31] [Step 3/6] SonarScanner for MSBuild 4.8
        [10:21:31] [Step 3/6] Using the .NET Framework version of the Scanner for MSBuild
        [10:21:31] [Step 3/6] Pre-processing started.
        [10:21:31] [Step 3/6] Preparing working directories...
        [10:21:31] [Step 3/6] 10:21:31.623 Updating build integration targets...
        [10:21:31] [Step 3/6] 10:21:31.658 Fetching analysis configuration settings...
        [10:21:42] [Step 3/6] 10:21:41.97 Failed to request and parse 'http://sonarqube.ourserver.com:9000/api/server/version': The remote server returned an error: (502) Bad Gateway.
        [10:21:42] [Step 3/6]
        [10:21:42] [Step 3/6] Unhandled Exception: System.Net.WebException: The remote server returned an error: (502) Bad Gateway.
        [10:21:42] [Step 3/6] at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
        [10:21:42] [Step 3/6] at System.Net.WebClient.DownloadString(Uri address)
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.PreProcessor.SonarWebService.<>c__DisplayClass15_0.<DownloadServerVersion>b__0()
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.PreProcessor.SonarWebService.DoLogExceptions[T](Func`1 op, String url, Action`1 onError)
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.PreProcessor.SonarWebService.DownloadServerVersion()
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.PreProcessor.SonarWebService.GetServerVersion()
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.PreProcessor.SonarWebService.GetProperties(String projectKey, String projectBranch)
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings, IDictionary`2& serverSettings, List`1& analyzersSettings)
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.DoExecute(ProcessedArgs localSettings)
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.BootstrapperClass.PreProcess()
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.BootstrapperClass.Execute()
        [10:21:42] [Step 3/6] at SonarScanner.MSBuild.Program.Execute(String[] args, ILogger logger)
        [10:21:42] [Step 3/6] at MSBuild.SonarQube.Runner.Program.Main(String[] args)
        [10:21:47] [Step 3/6] Process exited with code 255
        [10:21:47] [Step 3/6] Process exited with code 255 (Step: SonarScanner for MSBuild: begin analysis)
        [10:21:47] [Step 3/6] Step SonarScanner for MSBuild: begin analysis failed
        1. I attempted to add Set Additional Parameters on SonarScanner MSBuild runner to: '/d:sonar.cs.dotcover.reportsPaths="coverage.html"'

          But it appears to fail when using the SonarScanner MSBuild runner as shown above.

          Also, I do not see coverage.html file anywhere in the build artifacts.

          1. The problem seems to be in the server URL - Scanner cannot communicate with SonarQube server.

            But I believe that's too long conversation for a confluence page. Please reach us on our forum or support. 

            1. Thanks I have raised a support ticket.