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

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.

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 :blush:.

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

- task: SonarQubePrepare@4
  displayName: Prepare SonarQube analysis 
    SonarQube: 'SonarQube (XX)'
    scannerMode: 'MSBuild'
    projectKey: 'XXX-YYY'
    projectName: 'XXX-YYY'    
    extraProperties: |

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.

Hi @gashupl. Welcome to the community!

I think this page will tell you everything you need to know, including how to force the test code to be analysed.

Please note that if you do configure the scanner to analyze the test code as if it were product code then it will count towards the Lines Of Code limit for commercial versions of SonarQube.

Thank you @duncanp . This is exactly what I was looking for.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.