Salesforce APEX Test Coverage Report Not Working

  • ALM used: Salesforce/Azure DevOps
  • CI system used: Azure DevOps
  • Languages of the repository: Apex

Hi, I’ve configured my Azure DevOps Pipeline to upload test-result-coverage.json to SonarCloud but I’m not able to see its results or even mention of it in the logs that it was uploaded. Please help.
Below are my configurations.

This generates the test-result-coverage.json
sfdx force:apex:test:run -c -r json -l RunLocalTests -w 60 -d ./testoutput -u $Alias

SonarCloud Task for the Report

  • task: SonarCloudPrepare@1
    inputs:
    SonarCloud: ‘SonarCloudADO’
    organization: ‘xxxxx’
    scannerMode: ‘CLI’
    configMode: ‘file’
    extraProperties: |
    sonar.apex.coverage.reportPath=$(Agent.TempDirectory)\testoutput\test-result-codecoverage.json
    sonar.verbose=true

  • task: SonarCloudAnalyze@1

sonar-project.properties

sonar.projectKey=mykey
sonar.projectName=myproj
sonar.projectVersion=1.0

sonar.modules=force-app

force-app.sonar.projectName=myproj
force-app.sonar.sources=main/default
force-app.sonar.language=apex
force-app.sonar.exclusions=**/.xml, **/__.cls, **/.html

1 Like

Hi @alexkoz and welcome to the community !

Some questions :

  • Is that a Salesforce DX Project ? Seeing the command i guess so, but just to be sure.
  • Do you see your analyzed code correctly on SonarCloud UI ?
  • Have you check that the coverage file is at that place and contains relevant data inside it ?

Thanks.
Mickaël

1 Like

Hi @alexkoz Please check the directory you have mentioned, if you are generating the code coverage at runtime then the report will be generated at location $(Build.SourcesDirectory)\testoutput\test-result-codecoverage.json

1 Like

Hi @mickaelcaro Could you please confirm, code-coverage is possible to generate only with SFDX project and not with normal salesfoce?

I confirm.

1 Like

Guys, thanks for your reply. Aanvekar was right about the file location.
I confirmed and the results are now showing. However, the overall results between SonarCloud and Salesforce are a bit off. SonarCloud is reporting overall coverage at 7.8%, while Salesforce is showing 9%. It’s close enough but wondering what the difference is?

1 Like

Great @alexkoz SonarQube analysis the report and displays the aggregate result on UI.
Probably there are few other files which are getting counted as part of Code-Coverage, you can make use of “sonar.coverage.exclusions” to exclude those files.

Thanks
Abhishek

1 Like

@alexkoz when are you generating the code coverage report?

Using this above command we can generate only when the code is in ORG, is there any option to generate the code coverage before deploying the code? because I would like to add an Quality gate on code-coverage(CC) like if CC is less than 75% I want the build to fail or no deployment happens.

Is there are way to generate the code coverage before deployment ?

Also @mickaelcaro @janos, please add to it and let me know if there is any possibility.

Thanks,
Abhishek