Search code examples
unit-testing.net-coreazure-devopssonarqubestatic-code-analysis

Static code analysis of .NET Core Unit test projects *.cs files with SonarQube


I have a Visual Studio .NET Core solution containing Unit Test projects. All the projects have a reference to the Microsoft.NET.Test.Sdk Nuget package is which according to my knowledge is a “must have” for unit tests execution. I would like to perform static code analysis for unit test code (because it is also a code which is part of our deliverables). However, every time I am running Sonar Analysis as a part of Azure DevOps pipeline – I’m seeing “0” Lines of Code inside unit test *.cs files (and also other files inside unit test project) in the SonarQube report. enter image description here

I have also found out interesting thing. In case I remove reference to the Microsoft.NET.Test.Sdk Nuget package – static code analysis is performed without any problems and code is visible for Sonar Qube scanner. Also – the “dotnet build” command does not report any warnings for unit test projects when running on Azure DevOps build agent – even in case it should 😊.

My SonarQube Prepare task configuration pipeline file is very simple and looks like:

- task: SonarQubePrepare@4
  displayName: Prepare SonarQube analysis 
  inputs:
    SonarQube: 'SonarQube (XX)'
    scannerMode: 'MSBuild'
    projectKey: 'XXX-YYY'
    projectName: 'XXX-YYY'    
    extraProperties: |
      sonar.sources=src/**/*

Detailed dotnet build command run on Azure DevOps agent looks like:

/usr/bin/dotnet build /home/vsts/work/1/s/src/XXX.IntegrationTests/Tests/XXX.YYY.IntegrationTests.csproj -dl:CentralLogger,"/home/vsts/work/_tasks/DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b/2.181.0/dotnet-build-helpers/Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll"*ForwardingLogger,"/home/vsts/work/_tasks/DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b/2.181.0/dotnet-build-helpers/Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll" --configuration Release

I am wondering what is so special about dotnet core projects with Microsoft.NET.Test.Sdk reference. Is there build process somehow different for them? Is there any way to “force” static code analysis for them?

Any help will be appreciated.


Solution

  • Ok. It occurs this is "by design" SonarQube behavior. Details and methods how to overwrite it may be found on: https://github.com/SonarSource/sonar-scanner-msbuild/wiki/Analysis-of-product-projects-vs.-test-projects