No code-coverage reported

  • ALM used : Azure DevOps
  • CI system used : Azure DevOps
  • All of this is run inside of docker, I’ve tried both locally and through Azure DevOps Pipelines
  • Scanner command :
dotnet sonarscanner begin \
      /k:"<projectName>-opf" \
      /o:"<orgName>" \
      /d:sonar.token="${DOCKER_SONAR_TOKEN}" \
      /d:sonar.cs.opencover.reportsPaths=./**/coverage.xml \
      /d:sonar.branch.name="${GIT_BRANCH}" \
      /d:sonar.projectBaseDir="/src/<projectName>" \
      /d:sonar.verbose=true \
  • .NET C#
  • I’m unable to get any code-coverage to show up in any of my scans. I’ve tried so many different things I don’t remember them all.
#Start with sonar begin as shown, this is from my Dockerfile

RUN dotnet build "<projectName>.csproj" -c Release -o /app/build

RUN dotnet test /Test<projectName>/Test.<projectName>.csproj --collect:"XPlat Code 
Coverage"

FROM build AS publish
RUN dotnet publish "<projectName>.csproj" -c Release -o /app/publish

RUN dotnet sonarscanner end /d:sonar.token="${DOCKER_SONAR_TOKEN}"

This is running on a branch where there is no new code, I just want to get the test-coverage working before merging into main.

When looking at the step where the tests are run, I see that the tests get executed, and a report is generated, however this is not reflected in SonarCloud

I’m pretty sure there is a simple solution to this, but I need help seeing it! Thank you!

Hey there!

What do the logs say about importing code coverage?

I’ll make sure to include the docs for .NET Test Coverage just in case. :slight_smile:

Hi, thanks for your reply!

image

I’m not entirely sure what I am looking for, but here is what I’ve found.

I have a feeling it might be this:


Which means I need to get a better overview of the folder structure I think

I’ve come one step further, but I need some help figuring out what to do next.

#30 21.90 10:32:40.487 INFO: Sensor C# File Caching Sensor [csharpenterprise] (done) | time=25ms
#30 21.90 10:32:40.488 INFO: Sensor C# Tests Coverage Report Import [csharpenterprise]
#30 21.90 10:32:40.488 DEBUG: Analyzing coverage with wildcardPatternFileProvider with base dir '/src/XXX/.' and file separator '/'.
#30 21.90 10:32:40.489 DEBUG: Pattern matcher extracted prefix/absolute path '/src/XXX/./coverage.xml' from the given pattern 'coverage.xml'.
#30 21.91 10:32:40.490 DEBUG: Pattern matcher returns a single file: '/src/XXX/./coverage.xml'.
#30 21.91 10:32:40.491 DEBUG: The current user dir is '/src/XXX'.
#30 21.91 10:32:40.491 INFO: Parsing the OpenCover report /src/XXX/./coverage.xml
#30 21.91 10:32:40.496 DEBUG: The file '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/NoAllocEnumerateExtensions.cs' is not indexed or does not have the supported language. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.
#30 21.91 10:32:40.497 DEBUG: CoveredFile created: (ID '1', path '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/NoAllocEnumerateExtensions.cs', NO INDEXED PATH).
#30 21.91 10:32:40.498 DEBUG: The file '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/SimplePool.cs' is not indexed or does not have the supported language. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.
#30 21.91 10:32:40.498 DEBUG: CoveredFile created: (ID '2', path '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/SimplePool.cs', NO INDEXED PATH).
#30 21.91 10:32:40.499 DEBUG: The file '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/StringBuilderPool.cs' is not indexed or does not have the supported language. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.
#30 21.92 10:32:40.500 DEBUG: CoveredFile created: (ID '3', path '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/StringBuilderPool.cs', NO INDEXED PATH).
#30 21.92 10:32:40.501 DEBUG: The file '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/HashCodeCombiner.cs' is not indexed or does not have the supported language. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.
#30 21.92 10:32:40.501 DEBUG: CoveredFile created: (ID '4', path '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/HashCodeCombiner.cs', NO INDEXED PATH).
#30 21.92 10:32:40.501 DEBUG: The file '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/CompatibilityCacheKey.cs' is not indexed or does not have the supported language. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.
#30 21.92 10:32:40.501 DEBUG: CoveredFile created: (ID '5', path '/_/src/Microsoft.TestPlatform.ObjectModel/Nuget.Frameworks/CompatibilityCacheKey.cs', NO INDEXED PATH).

And hundreds more of these entries.

I saw some other thread of there being an issue of code coverage not showing when there were 0 lines of code in a PR, but this isn’t a PR scan. It does however not have any code changes in it. Any help would be greatly apprecciated!

It looks like whatever coverage file is being picked up includes entires for, inexpclicably, the source code of vstest!

Have you confirmed that this coverage file actually contains entries pertaining to your source code?

You are right, how can this be?? When running the tests in the dockerfile it lists successful tests from our unit test project. I can’t figure out how to troubleshoot this further

Here is an output from the pipeline run:

A report is generated, and the redacted text is my project name, so I see that it does pick up some files from the project

@Colin Is there a way to run a sonar scan on a short lived branch but still get a FULL scan of the entire repo? I’m finding it hard to actually see any change from run to run, and with the “There is not enough lines to compute coverage” I cannot really test if my setup is correct without merging to main, which I want to avoid if possible.

@Stine Sounds like you want to run a long-lived branch analysis

(docs on Adjusting the long-lived branch name pattern of your project)

This will execute a “full” analysis without requiring you to merge to main.

1 Like

So after making our branch a long-lived branch we were able to get the code-coverage percentage to actually change.. So that’s great.

It seemed that the test-libraries included in our scan was from packages that we use, so those are fine to cover as well.

I think a lot of my problems were about not being able to see that percentage change, thinking that something was wrong. Had I merged to main and ran a full branch scan, I would have gottem my result almost right away. It would be nice knowing a bit more/having better documentation on the things I’ve learned here about how the different types of scans work. (PR scan, vs long-living-branch scan, vs short lived branch scan)

@Stine if I distill one additional takeaway here, I think it would be best if we showed the estimated coverage here even when there are no new lines to compute coverage on

I’ll flag that for our team

1 Like

YES! I think a lot of the threads here would not exist if that was a feature! Excellent suggestion!

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