Not Able To Export Unit Test Result to SonarQube

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  • what are you trying to achieve
  • what have you tried so far to achieve this

Hi,

I am using SonarQube Version 8.9.2 (build 46101)
I want to export the test results to the SonarQube portal under my project.

I was able to push the result of the scan including the code coverage to the SonarQube portal but I am not able to push\export the Unit test result of my Angular code.
The build is successful but still the Unit test result are not exported. I still see - above Unit Test like below,

Unit Test Not Seen

I am able to generate the XML file in Generic Test Execution format as mentioned in SonarQube documentation like below,

I am using the below Sonar properties in my Azure DevOps Classic UI pipeline,

sonar.sourceEncoding=UTF-8
sonar.sources=$(Build.SourcesDirectory)/NextGen
sonar.exclusions=**/node_modules/**,**/*.spec.ts,NextGen/src/Tools/**
sonar.tests=$(Build.SourcesDirectory)/NextGen/src
sonar.verbose=true
sonar.test.inclusions=**/*.spec.ts
sonar.ts.tslint.configpath=tslint.json
sonar.typescript.exclusions=**/node_modules/**,**/typings.d.ts,**/main.ts,**/environments/environment*.ts,**/*routing.module.ts
sonar.javascript.lcov.reportPaths=NextGen/coverage/lcov.info
sonar.testExecutionReportPaths=NextGen\src\testresults\unittest\unit-test-result.xml
sonar.buildbreaker.skip=true

In my karma.config file I have below details mentioned.

I have also attached the Run code analysis log in verbose for more details in 2 parts as It was bit in size.

8_Run Code Analysis Part1.txt (2.6 MB)
8_Run Code Analysis Part2.txt (3.1 MB)

Would appreciate if anyone could help me to resolve this last thing pending for me on how to export the unit test result to SonarQube.

Thanks.

Hi @SRP,

Thanks for the detailed information. The logs indicate a bit of what the problem is:

2021-12-22T11:42:01.5086117Z 06:42:01.507 INFO: Parsing F:\Projects\Builds\FisDev_SonarQube\6\s\NextGen\src\testresults\unittest\unit-test-result.xml
2021-12-22T11:42:01.5355427Z 06:42:01.534 INFO: Imported test execution data for 0 files
2021-12-22T11:42:01.5356537Z 06:42:01.534 INFO: Test execution data ignored for 63 unknown files, including:
2021-12-22T11:42:01.5357445Z src/app/activity/activity-tab-bottom-panel-tab-menu/activity-tab-bottom-panel-tab-menu.component.spec.ts
2021-12-22T11:42:01.5358082Z src/app/activity/activity-tab/activity-tab.component.spec.ts
2021-12-22T11:42:01.5358589Z src/app/activity/balance-list-info/balance-list-info.service.spec.ts
2021-12-22T11:42:01.5365166Z src/app/activity/bulk-replace/bulk-replace.component.spec.ts
2021-12-22T11:42:01.5365751Z src/app/activity/case-list/case-list-search/case-list-search.component.spec.ts

The file names in your unit-test-result.xml file need to be relative to the project base directory (sonar.projectBaseDir) which you haven’t specified in the configuration but you can see in the logs is set by the scanner:

2021-12-22T11:37:37.5868304Z 06:37:37.585 INFO: Base dir: F:\Projects\Builds\FisDev_SonarQube\6\s

This is one level above where your code is in the NextGen folder. So you have two basic options from here:

  • Change the filenames in unit-test-result.xml to include NextGen at the front of the path; or
  • Add the sonar.projectBaseDir parameter (documented here) to point to $(Build.SourcesDirectory)/NextGen (and adjust your other parameters like sonar.sources and sonar.javascript.lcov.reportPaths appropriately)

Can you give one of these a try and see if it solves the problem?

Regards,

Cameron.

Thank you Cameron!!

I was able to export the test cases to SonarQube portal by defining the sonar.projectBaseDir property.
However, the number of test cases exported is 755 where as the test cases shown on my azure devops build summary are 3000.

Not sure, why its not exporting the exact number of test cases now. I also updated the sonar properties by removing some files from sonar.typescript.exclusions and sonar.exclusions but it just made a difference of 1 test cases now it shows 756 test cases.

Below are the properties used currently, I’ll try making some changes in them to see its display the correct number of test cases.

If you have any suggestion around this please do share.

Thanks once again.

sonar.sourceEncoding=UTF-8
sonar.projectBaseDir=$(Build.SourcesDirectory)/NextGen
sonar.sources=$(Build.SourcesDirectory)/NextGen
sonar.exclusions=**/node_modules/**,NextGen/src/Tools/**
sonar.tests=$(Build.SourcesDirectory)/NextGen/src
sonar.verbose=true
sonar.test.inclusions=**/*.spec.ts
sonar.typescript.exclusions=**/node_modules/**
sonar.javascript.lcov.reportPaths=$(Build.SourcesDirectory)/NextGen/coverage/lcov.info
sonar.testExecutionReportPaths=$(Build.SourcesDirectory)/NextGen/src/testresults/unittest/unit-test-result.xml
sonar.buildbreaker.skip=true

Hi @SRP,

SonarQube will only import what is contained in the XML file, so it’s worth checking how many tests are in your unit-test-result.xml file. If that number is 756 then the issue is likely to be in the generation of that file by the karma-sonarqube-unit-reporter tool (which is a third party tool not developed or maintained by us at SonarSource)

Regards,

Cameron.