C# Code Coverage with Team City integration

Hello,

I am new to the SonarQube product. To date, we have configured the sonarqube server, the SQL database and integrated the sonarqube runner with team city. We currently have a C#/.NET project that I am attempting to scan. In the scan results, it is showing 0.0 Code Coverage. This codebase is predominately C#/.NET along with some javascript and HTML. In the documentation, https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild , it states how to setup sonarqube for MSBuild however we are using team city to build the package. I have followed this link for the team city, sonarqube integration.https://confluence.jetbrains.com/display/TW/SonarQube+Integration#SonarQubeIntegration-ReSharperinspections(Inspections(.NET)runner)
I am wondering what steps need to be taken in order to gain code coverage on the package. Any help or guidance in the would be appreciated. Thank you.

Hi Sam,

Are you running a code coverage tool, such as OpenCover, when executing your tests? See the Sonarqube documentation for importing code coverage results, https://docs.sonarqube.org/pages/viewpage.action?pageId=6389770.

Hope that helps,

–Mark

1 Like

Mark,

Thanks for the response. That’s what I don’t quite get. This is being run though (automated ) Team City. The documentation for team city is here: https://confluence.jetbrains.com/display/TW/SonarQube+Integration#SonarQubeIntegration-dotCoverresults . In reading the documentation you sent me, it looks like for MSBuild is set to: sonar.cs.vscoveragexml.it.reportsPaths , Visual Studio is set to: sonar.cs.vscoveragexml.reportsPaths= **"%CD%\VisualStudio.coveragexml, docCover is set to: etc. The setting I have in Team City is Dsonar.resharper.cs.reportPath=%system.teamcity.build.tempDir%\inspectReport.xml.

This setting within TC should complete the code coverage correct?

Been a while since I have used Team City, however, the property you are using is for the ReSharper static code analyzer, not the property for Code Coverage with DotCover.

I found this page, https://confluence.jetbrains.com/display/TW/Integrating+TeamCity+dotCover+Report+with+SonarQube+Runner, for integrating DotCover with SonarQube Scanner. I think the SonarQube property you want to use is ‘sonar.cs.dotcover.reportsPaths’.

–Mark

1 Like

thanks a lot, @scmbuildguy, for your valuable replies.

@beik - did you manage to import the code coverage?

Andrei,

Not yet. I am going to run some tests today to see if i can get it working. I will update this with findings.

1 Like

hi @beik - any update on this, did you manage to make it work?

Andrei,

Through a lot of trial and error, we were able to get the test execution and unit tests running with dotCover. Reports are being generated, merged and being picked up by Sonarqube. The issue now is that Sonarqube is not reading the code coverage portion of it. Under code coverage, it shows “-”??

So it looks like it is providing coverage on the javascript and css files but not the C# files. in the build log, it is showing that the C# files are being scanned but that is not being populated in sonarqube??

Please run it with diagnostics turned on - add the /d:sonar.verbose=true at the BEGIN step.

Then please upload here the logs of the END step.

Andrei,

Attached are 2 files. There is one with out the final build step, which is to send to sonarquube anad one with the end build step, send to sonarqube, that fails build. In that log file, there is an error message regarding begin and end step. Documention is iffy and what exactly those steps are supposed to be comprised of? Thank you for your continued help.

development_w_final_142.txt (299.7 KB)

development_wo_final_141.txt (296.8 KB)

Andrei,

Below are the screenshots showing the build process as a whole and the steps setting within the begin and end. I am hoping to verify that everything is correct. Thank you.

@beik could you post a link to the iffy documentation please? If it’s our documentation we can fix it; if it’s for the third-party TeamCity plugin we can’t.

Looking at the failing log, the scanner begin step is immediately followed by the scanner end step. That isn’t how the scanner is intended to be run. The expected sequence is:

  1. scanner begin
  2. execute the build and test
  3. scanner end

There is more information in the docs for the Scanner for MSBuild here.

EDIT: the ordering in the screenshots looks ok, but doesn’t match what’s in the log files:

[10:22:27]W: Step 3/6: Build Payment (Visual Studio (sln)) (22s)
...
[10:28:00] : Step 4/6: Execute Unit Tests (Visual Studio Tests) (13s)
...
[10:28:14] : Step 5/6: Static Code Analysis (SonarScanner for MSBuild: begin analysis) (1s)
...
[10:28:15]E: Step 6/6: Complete SonarQube Report (SonarScanner for MSBuild: finish analysis)

I’m not familiar with either TeamCity or the TeamCity plugin for SonarQube (although I see the plugin is using v4.0.2 of the scanner which is rather old and doesn’t support either the new SDK-style MSBuild project format or MSBuild v16).

However, the scanner is just a command line tool, and the plugin is just a wrapper around the scanner that is intended to make it easier to provision the scanner onto your build agents and then invoke it for you. Looking at the docs for the scanner should give some pointers on how it you should configure the plugin i.e. the ordering of tasks, and the parameters to pass at each stage.

Alternately, if the plugin isn’t helping you then you could just call the scanner directly from script code in your build pipeline.

1 Like