Dotnet test coverage shows as 0%

Hello,

We are using : SonarQube Developer Version : 9.4.0.54424
We have noticed that coverage report for all the repositories shows as 0.0%.
We are not sure how we can resolve this, can any one help us on this.
Please advise if this feature is supported in SonarQube Developer Version : 9.4.0.54424 or not.

Thanks,
Anil Kumar

Hey there.

What do the logs say?

:warning: Make sure you upgrade to SonarQube v9.9 LTS soon, not only to benefit from our Best LTS Ever™, but because soon we will systematically ask users to upgrade when they ask questions about earlier versions of SonarQube, which are now considered unsupported. :smiley:

Thank you Colin for your reply.

Below is the github actions workflow that we have added.

    - name: SonarQube Begin
  
    run: |
     
     #dotnet add package coverlet.collector --version 3.2.0
	 
     dotnet tool install --global coverlet.console
     
     dotnet tool install --global dotnet-sonarscanner
            
     dotnet sonarscanner begin /k:"***************"
	 
     /d:sonar.login=${{ secrets.SONAR_TOKEN }}
	 
     /d:$GITHUB_WORKSPACE/SonarQube.Analysis.xml
	 
     /d:sonar.verbose=true
	 
     /d:sonar.cs.vscoveragexml.reportsPaths=./tst/Infrastructure.Tests/TestResults/coverage.xml
	 
     /d:sonar.verbose=true
  
     dotnet build --no-incremental /d:sonar.verbose=true

     coverlet .\tst\Infrastructure.Tests\bin\Debug\net6.0\Infrastructure.Tests.dll
	 
     --target "dotnet" --targetagrs "test --no-build" -f=opencover -o="coverage.xml"
	 
     /d:sonar.verbose=true

  - name: Build
    run: dotnet build      

  - name: SonarQube end
    run: dotnet sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}'

I am seeing the below error message when I run the github actions workflow:

Preparing working directories…

14:19:55.635 Updating build integration targets…

14:19:55.736 Failed to request and parse ‘http://localhost:9000/api/server/version’: Connection refused (localhost:9000)

Unhandled exception. System.Net.Http.HttpRequestException: Connection refused (localhost:9000)

—> System.Net.Sockets.SocketException (111): Connection refused

at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)

at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)

at System.Net.Sockets.Socket.g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)

at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)

— End of inner exception stack trace —

We are not sure why we are seeing error : Failed to request and parse ‘http://localhost:9000/api/server/version’: Connection refused (localhost:9000) only for this specific workflow.

Our " SonarQube.Analysis.xml" is defined as below :

<?xml version="1.0" encoding="utf-8" ?>
<SonarQubeAnalysisProperties  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">

  <Property Name="sonar.host.url">https://***************/</Property>
  
  <Property Name="/d:sonar.cs.opencover.reportsPaths=">./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml</Property> 
  
</SonarQubeAnalysisProperties>

Could you please help us to resolve the issue.

Thanks,
Anil Kumar

It looks like your SonarQube.Analysis.xml file isnt doing anything, and that’s expected if you’re running in a GitHub Actions environment and installing the scanner fresh each time. As a result, there’s no value being passed to sonar.host.url.

You would either need to pass /s:<custom.analysis.xml> to your dotnet sonarscanner begin command, or just do something like this (no SonarQube.Analysis.xml file)

dotnet sonarscanner begin /k:"***************" /d:sonar.host.url="https://***************/"

ok, thnak you, let me change and check accordingly.

Hi Colin,

I am seeing the below error when I am running the github actions workflow for Sonar:

15:50:31.476 Writing Roslyn analyzer additional file to

/home/runner/work/********/*******/.sonarqube/conf/vbnet/SonarLint.xml…

15:50:31.48 Processing analysis cache

15:50:31.48 Incremental PR analysis is available starting with SonarQube 9.9 or later.

15:50:31.52 Pre-processing succeeded.

/home/runner/work/_temp/ee5356df-6ed7-4aa8-9ae7-6e4d947bcd5e.sh: line 11:

/d:sonar.cs.vscoveragexml.reportsPaths=./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml: No such file or directory

Error: Process completed with exit code 127.

GitHub actions workflow is defines as below :

- name: SonarQube Begin
        run: |
         
         #dotnet add package coverlet.collector --version 3.2.0
		 
         dotnet tool install --global coverlet.console
         
         dotnet tool install --global dotnet-sonarscanner
                
         
         dotnet sonarscanner begin /k:"**********" /d:sonar.login=${{ secrets.SONAR_TOKEN }} /s:$GITHUB_WORKSPACE/SonarQube.Analysis.xml /d:sonar.verbose=true
         
         /d:sonar.cs.vscoveragexml.reportsPaths=./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml /d:sonar.verbose=true
      
         dotnet build --no-incremental /d:sonar.verbose=true

         coverlet .\tst\Infrastructure.Tests\bin\Debug\net6.0\Infrastructure.Tests.dll 
         
         --target "dotnet" --targetagrs "test --no-build" -f=opencover -o="coverage.xml" /d:sonar.verbose=true

        - name: Build
        run: dotnet build

        - name: SonarQube end
        run: dotnet sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 

I have the test resulsts file uploaded to github ( location) at : ./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml

SonarQube.Analysis.xml file shows as below :

<?xml version="1.0" encoding="utf-8" ?>
<SonarQubeAnalysisProperties  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">

  <Property Name="sonar.host.url">***************/</Property>
 
  <Property Name="sonar.cs.vscoveragexml.reportsPaths">./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml</Property>
  
</SonarQubeAnalysisProperties>


Could you please let me know how we can resolve the issue.

Thanks,
Anil Kumar

The most important thing is to make sure the file actually exists where you think it is. What happens if you run a command to print out the content of * ./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml in your build pipeline?

Hi Colin,

Thank you for your reply. I am able to print the content of the file in my github actions workflow : cat ./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml

Please let me know in case if you want me to share the complete file, I will share the file accordingly.

I am seeing the same error message :

/home/runner/work/_temp/b366b478-4e8d-4aed-b0cf-fb986e910fca.sh: line 11:

/d:sonar.cs.vscoveragexml.reportsPaths=./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml: No such file or directory

102
Error: Process completed with exit code 127.

Please let me know in case if there is any solution on this issue.

Regards,
Anil Kumar V

Hi Colin,

please let me know if we have to add any steps in yaml to run .NET test coverage.

Thanks in advance,
Anil Kumar

Hello everyone,

Can any one help us with the issue that we are expereincing on sonar. We are not able to resolve the issue since a long time now.

Thanks in advance,
Anil Kumar.

Hey there.

Looing again at your post:

I think the issue might just be that you’ve put /d:sonar.cs.vscoveragexml.reportsPaths.... on a seperate line from the rest of the command. The scanner isn’t failing to find the file, GitHub actions is trying to execute that one line and can’t for obvious reasons.

Try moving it up to the command. Also take note that if you’re importing an opencover report, you may want to be using sonar.cs.opencover.reportsPaths instead.

Hi Colin, thank you for your reply, we have tried the respective changes however the issue remain same.

- name: SonarQube Begin
        run: |
         
        #dotnet add package coverlet.collector --version 3.2.0
  
  dotnet tool install --global coverlet.console
  
  dotnet tool install --global dotnet-sonarscanner
          
  dotnet sonarscanner begin /k:"***********" /d:sonar.login=*** /s:$GITHUB_WORKSPACE/SonarQube.Analysis.xml /d:sonar.cs.opencover.reportsPaths=./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml /d:sonar.verbose=true      
        
  dotnet build --no-incremental  
  
  coverlet .\tst\Infrastructure.Tests\bin\Debug\net6.0\Infrastructure.Tests.dll
  
  --target "dotnet" --targetagrs "test --no-build" -f=opencover -o="coverage.xml" /d:sonar.verbose=true

SonarQube.Analysis.xml file is defined as below:

<?xml version="1.0" encoding="utf-8" ?>
<SonarQubeAnalysisProperties  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">

  <Property Name="sonar.host.url">*************/</Property>
 
  <Property Name="sonar.cs.vscoveragexml.reportsPaths">./tst/Infrastructure.Tests/TestResults/coverage.opencover.xml</Property>
  
</SonarQubeAnalysisProperties>

Error Message displayed on github actions :

127 Warning(s)
0 Error(s)

Time Elapsed 00:00:33.52
Target must be specified.
Cross platform .NET Core code coverage tool 3.2.0.0

Usage: coverlet [options] <<ASSEMBLY|DIRECTORY>>

Arguments:
<ASSEMBLY|DIRECTORY> Path to the test assembly or application directory.

Options:
-h|–help Show help information
-v|–version Show version information
-t|–target Path to the test runner application.
-a|–targetargs Arguments to be passed to the test runner.
-o|–output Output of the generated coverage report
-v|–verbosity Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.
-f|–format Format of the generated coverage report.
–threshold Exits with error if the coverage % is below value.
–threshold-type Coverage type to apply the threshold to.
–threshold-stat Coverage statistic used to enforce the threshold value.
–exclude Filter expressions to exclude specific modules and types.
–include Filter expressions to include only specific modules and types.
–exclude-by-file Glob patterns specifying source files to exclude.
–include-directory Include directories containing additional assemblies to be instrumented.
–exclude-by-attribute Attributes to exclude from code coverage.
–include-test-assembly Specifies whether to report code coverage of the test assembly.
–single-hit Specifies whether to limit code coverage hit reporting to a single hit for each location
–skipautoprops Neither track nor record auto-implemented properties.
–merge-with Path to existing coverage result to merge.
–use-source-link Specifies whether to use SourceLink URIs in place of file system paths.
–does-not-return-attribute Attributes that mark methods that do not return.
–instrument-modules-without-local-sources Specifies whether modules should be instrumented even if the sources from the PDBs can’t be found locally.

Error: Process completed with exit code 102.

That looks like an error with the coverlet tool, not SonarQube, which just consumes the report it produces.

Thank you Colin for your reply, we are having issues with sonar and we are not able to resolve the issues, looks like we have to look out for some other tool for code coverage.