Unit test Code coverage always showing 0% even after adding unit tests for newly added code

  • ALM used: Bitbucket
  • CI: Jenkins
  • Scanner command used: - Sonar scanner
  • Language: .NET

We are using sonar scanner and open cover for the unit test coverage and report.
the output xml file for the test results generated successfully with no errors but even after writing the unit test case of newly added code code coverage showing 0.

The path of result xml is configured in sonar scanner begin and open cover step.

What do the logs say about importing code coverage?

Hi Colin,
I have seen some issues in The last analysis has warnings in Analysis status.
Here it is:-

    • The property ‘sonar.login’ is deprecated and will be removed in the future. Please use the ‘sonar.token’ property instead when passing a token.
    • The Data Dictionary is not configured for the PLSQL analyzer, which prevents rule(s) S3641, S3921, S3618, S3651 from raising issues. See PL/SQL | SonarCloud Docs
    • Analysis using MS Build 14 and 15 build tools is deprecated. Please update your pipeline to MS Build 16 or higher.

Out of 3 the first one we are already using sonar.token. only the issue I assume is we are using MS Build 15 build tools and sonar scanner MS build 5.0.

If we change MS Build to 16 and use sonar scanner MS build 5.0 then will it resolve the issue or we need to downgrade the version of sonar scanner MS build to 4.

Hey there.

I think we should focus specifically on code coverage – what do the logs say about importing code coverage?

Hi Colin,
Please find the sonar scanner logs.

scannerlogs.txt (7.8 KB)

We have added verbose statement in begin step and we are using Jenkins pipeline so at the Sonar start step I downloaded the log and attached please let me know if you are referring other logs.

It looks like you’re using a very outdated version of the Scanner for .NET, you should upgrade to v5.13.

And, the logs you shared don’t contain the logs from the end step of the Scanner for .NET, where the information about importing code coverage would be found. Make sure you look for those logs and reference the guide earlier to find details about code coverage logs (this is different than simply providing the logs – make sure you dig into them!)

Latest end log.txt (230.2 KB)
Hi Colin,
Please find the sonarend logs.
We’ve updated MSbuild.exe to 15 and sonarscanner.Msbuild.exe to 5.13 also we are able to generated report and all unit tests case are passed.

Scenario 1:- I have Added 11 lines of code and also written unit test case for that. In this situation the code coverage is showing 0 lines to cover in sonar cloud and in my PR window it is showing -.
Scenario 2:- I have Added 11 lines of code and not wrote unit test case for that. in this situation the code coverage is showing 75% code coverage is required , 11 new lines to cover.

So I assume for first scenario it should show some percentage like 75-80 %.

Hey there.

It doesn’t look like much coverage information is being found.

  08:52:59.087 INFO: Sensor C# Tests Coverage Report Import [csharp]
  08:52:59.088 DEBUG: Analyzing coverage with wildcardPatternFileProvider with base dir 'TestCase\.' and file separator '\'.
  08:52:59.089 DEBUG: Pattern matcher extracted prefix/absolute path 'TestCase\coverage-output\cover.xml' from the given pattern 'TestCase\coverage-output\cover.xml'.
  08:52:59.089 DEBUG: Pattern matcher returns a single file: 'TestCase\coverage-output\cover.xml'.
  08:52:59.089 DEBUG: The current user dir is 'TestCase'.
  08:52:59.089 INFO: Parsing the OpenCover report TestCase\coverage-output\cover.xml
  08:52:59.092 DEBUG: Did not find deterministic source path in 'TestCase\Enterprise\DataAccess\CacheObjectEventArgs.cs'. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.
  08:52:59.093 DEBUG: CoveredFile created: (ID '1', path 'TestCase\Enterprise\DataAccess\CacheObjectEventArgs.cs', NO INDEXED PATH).
 
 08:52:59.102 DEBUG: Skipping the file (ID '2', path 'TestCase\Enterprise\DataAccess\CachingWrapper.cs', NO INDEXED PATH), line '33', visitCount '1' because file is not indexed or does not have the supported language.
  08:52:59.102 DEBUG: Skipping the file (ID '2', path 'TestCase\Enterprise\DataAccess\CachingWrapper.cs', NO INDEXED PATH), line '35', visitCount '1' because file is not indexed or does not have the supported language.
  08:52:59.102 DEBUG: Skipping the file (ID '2', path 'TestCase\Enterprise\DataAccess\CachingWrapper.cs', NO INDEXED PATH), line '37', visitCount '1' because file is not indexed or does not have the supported language.
  08:52:59.102 DEBUG: OpenCover parser: Skipping branch hits for file (ID '2', path 'TestCase\Enterprise\DataAccess\CachingWrapper.cs', NO INDEXED PATH), line '33', offset '5', visitCount '1' because file is not indexed or does not have the supported language.
  08:52:59.102 DEBUG: OpenCover parser: Skipping branch hits for file (ID '2', path 'TestCase\Enterprise\DataAccess\CachingWrapper.cs', NO INDEXED PATH), line '33', offset '5', visitCount '0' because file is not indexed or does not have the supported language.
  
  08:53:18.181 INFO: Adding this code coverage report to the cache for later reuse: TestCase\coverage-output\cover.xml
  08:53:18.181 DEBUG: Analyzing coverage after aggregate found '0' coverage files.
  08:53:18.181 DEBUG: The total number of file count statistics is '0'.
  08:53:18.181 INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=19094ms

If you look at TestCase\coverage-output\cover.xml, do you see coverage for the files changed in this pull request?

It also looks like some lines are randomly missing from these logs. Were these intentional redactions, or an error?

Hi Colin,
yes I can see the newly added code test coverage report in xml. Please find the full log.

Once again it looks like you’re using an old version of the Scanner for MSBuild.

SonarScanner for MSBuild 5.0.3

If you provide logs, please make sure you are using the latest version of the Scanner for .NET (as mentioned earlier, v5.13).

This looks like the Test Execution report you’re providing to sonar.cs.nunit.reportsPaths, not the coverage report.

  sonar.cs.nunit.reportsPaths=_TestCase\\coverage-output\\nunitresult.xml
  sonar.cs.opencover.reportsPaths=_TestCase\\coverage-output\\cover.xml

Are you sure you’re looking at the cover.xml file, not nunitresult.xml?

Coverage.zip (8.2 MB)
Hi Colin,
Please find the updated scanner logs with v5.13 and cover.xml file.
log (1).zip (1.8 MB)

Thanks.

It looks like only a single module is being analyzed by the scanner.

  11:16:57.018 INFO: Indexing files of module 'AAA'
  11:16:57.018 INFO:   Base dir: E:\Build\Project\TestCase\Tools
  11:16:57.018 INFO:   Excluded sources: **/build-wrapper-dump.json
  11:16:57.020 INFO: 119 files indexed

This module doesn’t exist in your cover.xml file.

What does your full pipeline look like? Are you doing a full build and test before the SonarQube analysis? If it’s a scripted pipeline, feel free to share it here.

Yes we are doing these steps
Sonar Scanner Start
Build
Run Tests
Sonar cube end.

echo ‘Build Test Projects and Run the Tests’

echo ‘Build Test Projects and Run the Tests’
bat “"${env.MSBUILD_EXE_VS2019}" ${env.WORKSPACE}/Tools/BuildScripts/XTBuild.proj /t:"9225-BuildTests-RunTests" /p:branch=${env.BRANCH_NAME};buildNumber=${env.BUILD_NUMBER};buildVersionFormat=${env.BUILD_TIMESTAMP_IN_VERSION_FORMAT}”

echo “Creating SonarQube Report…”
bat “"${env.MSBUILD_EXE_VS2019}" XTBuildIntegration.proj /t:"run_opencover" /p:branch=${env.BRANCH_NAME};buildNumber=${env.BUILD_NUMBER};buildVersionFormat=${env.BUILD_TIMESTAMP_IN_VERSION_FORMAT}”

I suggest providing the most detail possible, like the full pipeline.

Hi Colin,
Any update on this.

HI Colin,
Have you gone through the logs?

I have seen the cover.xml files and found this. Please let me know it is valid output
<Module skippedDueTo="Filter" hash="1E-1E-C3-74-C7-70-D5-17-83-E9-55-F5-83-07-CC-75-AC-57-A6-AD"> <ModulePath>Branch\TestCase\References\bin\UnitTests.dll</ModulePath> <ModuleTime>2023-07-20T08:04:39.8503784Z</ModuleTime> <ModuleName>UnitTests</ModuleName> <Classes /> </Module>

Hey there.

This is a free community forum. Sometimes it requires time to look through everything, while managing the rest of the Community. Please do not bump threads daily and familiarize yourself with the FAQ.

I created a topic, when can I expect a response?

This is an open community with people volunteering their free time to provide assistance. We’re eager to contribute to the community, but you are not guaranteed a fast response.

Be patient

  • Wait a few days before bumping a topic that hasn’t received a response.
  • Do not @name mention individuals not involved in the topic.

Contribute as much as you expect to receive

  • Contribute to the community (helping others) as much as you expect to receive help.

Hey again.

There’s a lot I don’t understand about your build. Based on the size of the logs, it’s a huge project, but there are only 148 tests run.

  Test Run Summary
    Overall result: Passed
    Test Count: 148, Passed: 148, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
    Start time: 2023-07-27 15:15:08Z
      End time: 2023-07-27 15:15:44Z
      Duration: 35.958 seconds
  
  Results (nunit3) saved as E:\Build\Dev\\TestCase\nunitresult.xml
  Committing...
  Visited Classes 13 of 4621 (0.28)
  Visited Methods 39 of 61423 (0.06)
  Visited Points 128 of 302525 (0.04)
  Visited Branches 64 of 142924 (0.04)

And very few files indexed

  11:16:57.018 INFO: Indexing files of module 'Development'
  11:16:57.018 INFO:   Base dir: E:\Build\Dev\\TestCase\Tools
  11:16:57.018 INFO:   Excluded sources: **/build-wrapper-dump.json
  11:16:57.020 INFO: 119 files indexed
  11:16:57.020 INFO: 0 files ignored because of inclusion/exclusion patterns
  11:16:57.020 INFO: 0 files ignored because of scm ignore settings

Do you have more context about this overall build? Are you trying to only analyze a small part of it or run tests on a small part of it? Are things being selectively built/tested based on what changed?

Hi Colin,
My issue is very similar to this topic in sonar.sources it is skipping. for Ex:-

11:17:09.478 DEBUG: CoveredFile created: (ID ‘690’, path ‘E:\Build\Dev\TestCase\PatientTrackingPortal\Model\ConsoleViews\Customization\PortalListViewData.cs’, NO INDEXED PATH).

11:17:09.478 DEBUG: Did not find deterministic source path in
‘E:\Build\Dev\TestCase\PatientTrackingPortal\Model\ConsoleViews\Customization\PortalListViewStatistics.cs’. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.

I hope now the things are on the page.

Hey there.

I still need help understanding the following: