0

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.

Arghya C
  • 9,805
  • 2
  • 47
  • 66

2 Answers2

0

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:

Johannes
  • 6,490
  • 10
  • 59
  • 108
0

The root directory used for the resolution of the sonar.exclusions relative path is the location of each .csproj file.

jBravo
  • 873
  • 1
  • 9
  • 28