C# analysis of unit test files result in zero lines of code (despite correct project categorization)


Enterprise Edition Version 8.9.6 (build 50800) 
Processing plugin: csharp version
Scanner: SonarScanner for MSBuild 4.10
using SonarScanner
using Java AdoptOpenJDK (64-bit)
using Windows 10 10.0 amd64


When trying to analyze unit test code (<SonarQubeTestProject>false</SonarQubeTestProject>) the analysis finds all files (*.cs) as it should, however the LoC displayed in the Web Frontend are wrong and hence not counted for the statistic/dashboard.

Example test file: LoC is displayed as “—” (first column)

If I navigate into the file itself the LoC seem to work, I see 36 LoC counted for this file and displayed in the header. Also all issues are identified as expected.

The following scanner properties are set in order to care only for the unit test code, works as expected:

<Property Name="sonar.inclusions">**.Test**,**.UnitTests**</Property>
<Property Name="sonar.exclusions">**/AssemblyInfo.cs,**/Disposable*Base.cs,**/*.Generated.cs,**/GeneratedImplementation.cs,**/*.Designer.cs</Property>

Also the scanner log seems ok:

Sonar: (...UnitTests.csproj) Categorizing project as test or product code...
Sonar: (...UnitTests.csproj) SonarQubeTestProject has been set explicitly to false
Sonar: (...UnitTests.csproj) Project categorized. SonarQubeTestProject=false

PS: I was not able to upload more images because your server won’t let me, response is always that uploading did not work out, should try again.

@acook this is by design. Issues are raised against test code, but metrics (including LOC) are not calculated. See here.

@duncanp thanks for your quick response.

Apart from the sales topic to pay for LoC, as a developer I am very interested in knowing how many LoC there are, regardless if it’s productive or test code.

In my case I analyze productive code and test code in separate SonarQube projects.
This means that the test code analysis has no LoC and is therefore empty in the dashboard, because the “main branch has no lines of code”.
An unpleasant workaround would be adding at least a dummy file with 1 line of productive code.

In my company there are whole teams who own just test code (MSTest projects, etc.)

Hence I guess this can be improved somehow.

That wouldn’t work: the project would still be classified as a test project. The wiki page explains how the classification is done.

In that case you could explicitly tell the scanner to treat the test projects as product code e.g. by passing -p:SonarQubeTestProject=false to MSBuild/dotnet build.

This topic was automatically closed after 10 days. New replies are no longer allowed.