Unit Test Execution and Coverage

We are using SonarQube Server v2025.3.1 (109879) and is deployed with Docker.

We are running unit tests for our C# projects on a build agent and the tests are running fine and produce either a trx or a xunit log and an opencover file for the coverage. Commands we use are:

dotnet test $env:testPath -c Release --logger 'xunit;LogFileName=TestResults.xml' --collect:'XPlat Code Coverage;Format=opencover' /property:WarningLevel=0 --verbosity quiet

dotnet test $env:testPath -c Release --logger 'trx;LogFileName=TestResults.xml' --collect:'XPlat Code Coverage;Format=opencover' /property:WarningLevel=0 --verbosity quiet

Afterwards when we run the sonarscanner we upload the data as follows, which used to work in the past but stopped working since a few weeks.

dotnet sonarscanner begin /k:$env:projectKey /d:sonar.token=$env:analysisToken /d:sonar.scanner.scanAll=false /d:sonar.host.url=$env:sonarAddress /d:sonar.dotnet.excludeTestProjects=true /d:sonar.cs.opencover.reportsPaths=$env:sources\**\*coverage.opencover.xml /d:sonar.cs.xunit.reportsPaths=$env:sources\**\*Results.xml /v:$projectVersion /d:sonar.scm.disabled=true /d:sonar.qualitygate.wait=true

dotnet sonarscanner begin /k:$env:projectKey /d:sonar.token=$env:analysisToken /d:sonar.scanner.scanAll=false /d:sonar.host.url=$env:sonarAddress /d:sonar.dotnet.excludeTestProjects=true /d:sonar.cs.opencover.reportsPaths=$env:sources\**\*coverage.opencover.xml /d:sonar.cs.vstest.reportsPaths=$env:sources\**\*Results.xml /v:$projectVersion /d:sonar.scm.disabled=true /d:sonar.qualitygate.wait=true

The information is however no longer correctly processed or uploaded to SonarQube and we have lost the information about the unit tests results and coverage.

The issue appeared with 2025.1 version upgrade where we cannot include any unit test result execution bellow is a sample with verbose = true

08:14:20.676 DEBUG: Pattern matcher extracted prefix/absolute path '/agent/_work/1/s/PROJECT/Sources/UnitTests/Email' from the given pattern '/agent/_work/1/s/PROJECT/Sources/UnitTests/Email/**/*TestResults.trx'.
08:14:20.677 DEBUG: Gathering files for wildcardPattern '**/*TestResults.trx'.
08:14:20.687 DEBUG: Pattern matcher returns '1' files.
08:14:20.688 INFO: Parsing the Visual Studio Test Results file '/agent/_work/1/s/PROJECT/Sources/UnitTests/Email/PROJECT.EmailManagement.Tests/TestResults/PROJECT.Service.EmailManagement.Tests.TestResults.trx'. 08:14:20.692 DEBUG: Parsed Visual Studio Unit Test - testId: d6d494be-39dc-26cd-f32d-d84059333c74 outcome: Passed, duration: 0
08:14:20.695 DEBUG: Test method PROJECT.Service.EmailManagement.Tests.PROJECT.Service.EmailManagement.Tests.EmailControllerTests.TestPost cannot be mapped to the test source file. The test will not be included.
08:14:20.696 INFO: Sensor C# Unit Test Results Import [csharpenterprise] (done) | time=22ms

Hi,

Welcome to the community!

What did you upgrade from, please?

 
Thx,
Ann

Thank you!
We upgraded from 10.8.

Hi there,

Would you by any chance change the name of the tests?

We had a user experience a similar problem doing so with NUnit.

We also know there are issues if your tests are generated during build.

Denis

1 Like

Hi @caspar,

Could you post a full, verbose log of this, please? I know you’ve given us a snippet. but it’s helpful to have the whole thing.

Share the Scanner for .NET verbose logs

  • Add /d:"sonar.verbose=true" to the…
    • SonarScanner.MSBuild.exe or dotnet sonarscanner begin command to get more detailed logs
      • For example: SonarScanner.MSBuild.exe begin /k:"MyProject" /d:"sonar.verbose=true"
    • “SonarQubePrepare” or “SonarCloudPrepare” task’s extraProperties argument if you are using Azure DevOps
      • For example:
        - task: SonarCloudPrepare@3
            inputs:
              SonarCloud: 'sonarcloud'
              organization: 'foo'
              scannerMode: 'dotnet'
              projectKey: 'foo_sonar-scanning-someconsoleapp'
              projectName: 'sonar-scanning-someconsoleapp'
              extraProperties: |
                sonar.verbose=true
        
  • The important logs are in the END step (i.e. SonarQubeAnalyze / SonarCloudAnalyze / “Run Code Analysis”)

Share the msbuild detailed logs

MsBuild.exe /t:Rebuild /v:d

or

dotnet build -v:d

 
Thx,
Ann

See the following extensive log file:
log.txt (515.1 KB)

1 Like

Hello @caspar,

thanks a lot for sharing the verbose logs with us.

While I’m investigating the issue based on the info you gave us above could you please tell me if you are maybe customizing your test names?

I’ll come back to you with more info or questions soon.

Thanks!

I have control over updating the test names but noiticed from the logs that it is generating the wrong namespace. For example, the following is found in the logs:

Test method COMPANY.PROJECT.Tests.COMPANY.PROJECT.Tests.ApplicationSettingsTests.Properties_CanBeSet_AndRetrieved cannot be mapped to the test source file. The test will not be included.

The first part marked in bold seems to be a duplication while the test class name is not having this duplication. It looks like it is pre-fixing the assembly name, followed by the namespace, class and method name. Maybe this is a clue of what is going wrong.

What would be useful is it you could share some test code that includes the tests that cannot be mapped and the Test results file generated by

dotnet test $env:testPath -c Release --logger 'trx;LogFileName=TestResults.xml' --collect:'XPlat Code Coverage;Format=opencover' /property:WarningLevel=0 --verbosity quiet

would this be possible?
If not due to privacy reasons maybe you could share a reproducer project with similar test format to the ones that have the issue?

FYI we had this issue before, but with NUnit. The root cause was that the user was overriding the test name via the TestName property for parametrized tests, and that resulted in the test results file to not have the correct full name of these tests - so mapping from test results to actual code was impossible. Let me know if this rings a bell in your case.

If not it would be great to still inspect how the tests results file is encoding the test names to exclude that the problem is there.

thanks!

We have the same issue for all our projects and all unit tests. It is not isolated to one specific project. Here is the code of one testing method:

public class HealthControllerTests
    {
        [Fact]
        public void Index_ReturnsStatusCode200()
        {
            // Arrange
            var controller = new HealthController();

            // Act
            var result = controller.Index();

            // Assert
            var statusCodeResult = Assert.IsType<StatusCodeResult>(result);
            Assert.Equal(200, statusCodeResult.StatusCode);
        }
    }

Hi Caspar,

We’re back on this issue.
Apologies for the delay, we have very limited capacity at the moment.

2 Likes

Hello Caspar!

So I managed to replicate the issue.
I tested with the SQ version that you mentioned in your first post and SQ LTA (2025.3.1).
I have the same issue with both versions.

I have 2 questions:
1 - for which SQ version were your test results working before?
2 - Is is possible that you updated some xunit package meanwhile or dotnet test? If yes could you please give me these versions too?

thanks a lot!

Hello Mary!

Thank you for getting back to me.

We were using SonarQube Server 10.8: the latest version from Sonar | Sonar before and then it was still working as expected.

We used a mix of xunit versions but have used at least version 2.9 and 2.5.3.

Looking forward to hearing from you.

1 Like

Hello @caspar,

I pinned the exact change in our analyzer that is causing this.

Could you please let me know if the issue is resolved in case you remove
/d:sonar.dotnet.excludeTestProjects=true?

Thanks

1 Like

Hi Mary,

Thank you for your continued effort to look into this.

Unfortunately it did not solve the problem.

Hello @caspar,

In my repro the issue was solved - as to map the test results back to the actual tests, the tests themselves need to be included in the analysis.

This requirement is valid since sonar-dotnet 10.4 (which is the change that altered your result after the SQ upgrade).

Could you please check if /d:sonar.dotnet.excludeTestProjects=true is set anywhere else in your analysis (server options - global or project properties) or if your test projects have <SonarQubeExclude>true</SonarQubeExclude>.

If not, could you please send me a reproducer where the results are not properly processed, but the test projects are not excluded from the analysis.

Thanks a lot!

1 Like

Hi @Mary , I managed to make it work now! :smiley:

The following d:sonar.dotnet.excludeTestProjects=true was needed to be removed indeed but found that our pipelines were running the dotnet test command before the sonarscanner begin was called. By moving the dotnet test inside the sonarscanner begin and sonarscenner end block it started to work.

I believe we can close the topic. Thanks so much for your help with finding a solution.

2 Likes

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