How to integrate the dotcover HTML reports with sonarqube 4.5? - c#

I have been struggling with this issue. I have been able to generate the HTML reports with the help of DotCover. Also, I have given the path of the reports in the Sonar Portal too under Project Settings--> C# --> CodeCoverage --> DotCoverReportPath and also in the Sonar.Properties file for the particular solution (Mine is a DotNet application).
When I run the Sonar-Runner it seems to parse the HTML files and also says that Execution success, but however my dashboard on the Sonar System shows "No Data" for the coverage.
Please help me out if any of you have faced a similar issue and have been able to figure out the error.

SonarQube requires that the absolute paths of the source files found in the coverage HTML reports to be strictly equal to the absolute paths of the files being analyzed.
This implies that you cannot run your tests and collect code coverage on a machine A, transfer the report to machine B, and then run the SonarQube analysis from machine B. From within the same machine, you also cannot checkout the code once to run the tests & collect code coverage, and then check it out another time to run the SonarQube analysis.
This is a common case of why code coverage doesn't get imported into SonarQube, but without the logs, it is hard to tell whether this is your case or not.

Related

How to correctly configure SonarQube Scanner for MSBuild with OpenCover

I have a .NET solution with bunch of projects. I wan to analyze the project with SonarQube, and show unit tests details. I'm using NUnit for tests and OpenCover to generate coverage reports. I have a simple batch file to run the required SonarQube tasks in sequence.
Now I want some of the assemblies to be excluded from the coverage analysis & calculation, and do the aggregate analysis only on a subset of assemblies. I'm using opencover filters for the purpose, but they do not seem to work.
Let's say I have assemblies like
MyProject.ViewModels
MyProject.DomainModels
MyProject.Core
MyProject.CoreTests
My.Shared.Data
MyProject.Data.Tests
MyProject.Data.Console
MyShared.Console
UIModules.Services
UIModules.Services.UnitTests
UIModules.Web
UIModules.Web.Tests
And to process them I have a batch file like the following. I have added some filter to exclude the Tests, Models & Console assemblies.
Seems like the filters are not working properly. In the code coverage shown on SonarQube, though the Test projects are excluded, still the Models & Console projects are taken into account for overall coverage calculation.
SonarQube.Scanner.MSBuild.exe begin /k:"key" /n:"My Project" /v:"1.0" /d:sonar.cs.nunit.reportsPaths="NUnitResults.xml" /d:sonar.cs.opencover.reportsPaths="opencover.xml"
REM build the project/solution
msbuild MyProject.sln /t:rebuild
REM run NUnit tests
"C:\Program Files (x86)\NUnit 2.6.4\bin\nunit-console.exe" /result=NUnitResults.xml Core\bin\Debug\MyProject.CoreTests.dll Data\bin\Debug\MyProject.Data.Tests.dll Services\bin\Debug\UIModules.ServicesTests.dll Web\bin\Debug\UIModules.Web.Tests.dll
REM run OpenCover coverage
"C:\Users\Me\AppData\Local\Apps\OpenCover\OpenCover.Console.exe" -register:user "-target:C:\Program Files (x86)\NUnit 2.6.4\bin\nunit-console.exe" "-targetargs:Core\bin\Debug\MyProject.CoreTests.dll Data\bin\Debug\MyProject.Data.Tests.dll Services\bin\Debug\UIModules.ServicesTests.dll Web\bin\Debug\UIModules.Web.Tests.dll /noshadow" "-output:opencover.xml" "-filter:+[MyProject*]* +[My.Shared*]* +[UIModules*]* -[*Tests]* -[*.Console]* -[*Models]*"
REM process end analysis
SonarQube.Scanner.MSBuild.exe end
Can someone guide me to the correct direction.
Not sure where you are looking for the rules.
The coverage filter rules in the coverage tool should influence the output of the coverage.
When the coverage gets imported additional rules apply.
Sonar Qube has additional rules. And the runner can have filter rules built in as well.
Some Runners can ignore Sonar settings.
Since you are talking about coverage I assume you want to skip testing data.
For that the Testing projects names need to end in Test or Tests.
This is mentioned here:
https://docs.sonarqube.org/display/SCAN/Miscellaneous+Advanced+Usages
I also tend to exclude AssemblyInfo.cs which I set in my Sonar Host.
In Configuration -> Analysis Scope -> Global Source File Exclusions with my Pattern of **/AssemblyInfo.cs
I hope this gets you started.
I think you should leave the Other projects as not covered and list that.
If you really want to exclude per project you could try to pass additional parameters to the sonar scanner like sonar.exclusions
It is possible that sonar.exclusions is ignored by the runner.
See this answer for inspiration on different exclusion methods:
The root directory used for the resolution of the sonar.exclusions relative path is the location of each .csproj file.

How to exclude specific files in Sonar?

I am using SonarQube on a C# .Net application via TeamCity. This application has jQuery in a folder with a bunch of other scripts. I would like to exclude a number of JQuery files from analysis but have Sonar analyze all the other files in the folder. One such JQuery file is at the following path:
...MyProject\Content\Scripts\jquery-ui-1.8.custom.min.js
I have added the following line to the additional parameters section of the SonarQube Runner build step:
-Dsonar.exclusions=**/Content/Scripts/jquery*
in an effort to exclude all files that start with jquery in that folder however the file is not being excluded from the analysis. Any ideas here?
I locally created an example project, containing a java file and a file called Content\Scripts\jquery-ui-1.8.custom.min.js.
~/sonar-scanner/bin/sonar-scanner led to these logs:
...
INFO: Quality profile for js: Sonar way
...
INFO: Sensor JavaScript Squid Sensor [javascript]
INFO: 1 source files to be analyzed
...
and the javascript file was visible in SonarQube.
~/sonar-scanner/bin/sonar-scanner -Dsonar.exclusions=**/Content/Scripts/jquery* led to these logs:
...
INFO: Excluded sources:
INFO: **/Content/Scripts/jquery*
INFO: 2 files indexed
INFO: 1 file ignored because of inclusion/exclusion patterns
...
and the javascript file was successfully excluded (not visible in SonarQube).
Conclusion
Your mentioned configuration should work. My guess is, that the analysis report cannot be processed for another reason and you are seeing old data in SonarQube.
Please verify your sonar runner logs and the status of your background tasks in the SonarQube administration pages.

Sonar - Code Coverage Reported, but Unit Test Count Not Reported

Recently, I've been running into a perplexing issue regarding Sonar that I can't seem to figure out. I couldn't really find anything on this specific issue online, so I figured I'd reach out and see if anyone has experienced this as well.
I am using the following:
Sonar 6.0
TFS 2017 Update 1
SonarQube VSTS Extension 2.1.1
Build server with Visual Studio 2017
When I am performing Sonar analysis after a build and after tests complete, it is detecting both the resulting trx file and coverage file.
But once the analysis is done, it only reports code coverage. It does not report the unit test count.
Originally, I was using the following arguments in the "Additional Settings" in the Sonar Begin Analysis step to specifically point it to the trx:
/d:sonar.exclusions=**Scripts**,**Content** /d:sonar.cs.vscoveragexml.reportsPaths="$(Common.TestResultsDirectory)\*.coverage" /d:sonar.cs.vstest.reportsPaths="$(Common.TestResultsDirectory)\*.trx"
But I just tried removing all those arguments as I was told by a sonar rep in the past that you want to specify parameters from the site instead of by command line. When I removed my parameters, nothing changed (which I guess is to be expected). It still was able to find the trx file and still did not report unit test count.
I would try adding the vstest report path in the UI, but the issue is the path and file name are dynamically generated. The path depends on what build server/vsts agent the build is sent to, and the trx file name is generated based on date-time, and as far as I could find, there is no way to force the trx file to have a specific name.
I also checked the trx file to make sure I could open it and it reported test data.
Essentially, as far as I can tell, it should be reporting the unit test count. It did in the past but seems to have been not working as of recently. I'm not sure if this maybe was fixed in a newer version of Sonar? Unfortunately, I can't upgrade because dashboards were removed in newer versions of Sonar, and the organization I work for uses them. Has anyone experienced this issue before, and if so, were you able to fix it? Let me know if you need any additional information!
Try to use /d:sonar.cs.vstest.reportsPaths=..\**\TestResults\**\*.trx instead (Additional Settings).
On the other hand, there is SonarQube 2.1.2 aviable.
My result: https://1drv.ms/i/s!AresBGZVYryjhSpb9taJWFht7YB9

FxCop Rule in SonarQube 4.5.7 on C# project

I'm tring to run analysis on SonarQube, using an FxCop custom Rule.
In SonarQube 4.5.7 I add the rule to the set of rules, I activate it and then run the analysis.
To run the analysis I use the sequence of following commands:
1) MSBuild.SonarQube.Runner.exe begin /k:my.project.C-Sharp-ConsoleApp /n:C-Sharp-ConsoleApp /v:1.1
2) "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild" /T:Rebuild
3) MSBuild.SonarQube.Runner.exe end
I see that the rule is executed, because when I run the second command I read the following part of log:
...
(RunCodeAnalysis target) ->
MSBUILD : warning CR1000: MyRules : Field 'CSharpSortNames.m_variabile' is not in Hungarian notation. Field name should be prefixed with 's'. [C:\Users\Alessandro\Documents\Visual Studio 2015\Projects\C-Sharp-ConsoleApp\C-Sharp-ConsoleApp\C-Sharp-ConsoleApp.csproj]
My custom rule checkID is CR1000, and after the third command, I see that an error of this rule is founded, but the web app doesn't let me see where. For all other errors the web app let me see the precise line where is the error by a link to the .cs file. For my rule it doesn't.
Anyone can help me about this?
Further problem is in SonarQube 5.4 the same rule is activated but web app does not show the error.
The root cause here is that FxCop uses information from the PDB file for providing location information. However, the PDB only contains information that would be useful for debugging scenarios, which means that FxCop rule violations that are associated with non-executable code (e.g.: field declarations or interface definitions) will not have location information available. (FWIW, there is an open SonarQube issue for addressing this, but it would be non-trivial to accomplish unless SonarQube were to directly examine the source files to attempt to locate the field declaration. I rather suspect they might not bother given that it simpler to address via a Roslyn analyzer.)
Further problem is in SonarQube 5.4 the same rule is activated but web
app does not show the error.
That's because older versions of the C# plugin for SonarQube completely ignored FxCop violations without location information. This was addressed in version 5.2 of the plugin, which only became in early May 2016 (and is presumably what you used when you installed SonarQube 5.5). It is compatible with version 5.4 of SonarQube, so you should be able to use it with your older installs if you like.

Sonarqube C# unable to display Integration test/coverage results

We are using TFS/VS 2013 Microsoft build stack to build our .NET ASP application (runs unit tests), then deploys the application based on the build to a web server which then has our API rest and Selenium UI tests ran against it.
I am getting the .coverage and .trx file, and I am able to merge the .coverage into a single .coveragexml. This is able to be processed by sonar and does display the correct results for coverage and test results, but places all the results under 'Unit Test Success or Coverage'.
Is there any way to separate out the results, i.e. Integration Tests or a widget that can display multiple test runs against a single project?
I can somewhat accomplish this by setting up multiple projects (using the same source) and attaching different .coverage and .trx to the project, but this doesn't give a clear picture of the results since the tests are all ran against the same source and bits.
I would like to display our unit tests (not all are .net), C# integration tests, API, and UI tests into separate "widgets"
Here is a copy of my sonar-project.properties file:
# Root project information
sonar.projectKey=XXX.XXX.Presentation
sonar.projectName=XXX.XXX.Presentation
sonar.projectVersion=1.0
# Enable the Visual Studio bootstrapper
sonar.visualstudio.enable=true
sonar.visualstudio.solution=XXX.XXX.sln
sonar.visualstudio.testProjectPattern=.*Tests
# Code Coverage
sonar.cs.vscoveragexml.reportsPaths=MergedResults.coveragexml
# Unit Test Results
sonar.cs.vstest.reportsPaths=TestResults/*.trx
# Some properties that will be inherited by the modules [P.S : Forward slashes]
sonar.sources=.
sonar.projectBaseDir=.
# Info required for SonarQube
sonar.language=cs
sonar.sourceEncoding=UTF-8
Integration code coverage is not yet supported by the C# plugin.
See http://jira.sonarsource.com/browse/SONARNTEST-5
Same story for integration test results:
http://jira.sonarsource.com/browse/SONARNTEST-22

Categories

Resources