I am executing code coverage through SFDX cli, below is the command:

sfdx force:apex:test:run --targetusername $username --resultformat json --codecoverage --testlevel RunLocalTests

I then upload the report in sonarqube.

I see 2 results on console:

1) testruncoverage --> It is 28%. 2) orgwidecoverage --> It is 34%.

enter image description here

"testruncoverage --> It is 28%" is the one that gets posted on SonarQube.

What is the difference between those 2? How do I make testRunCoverage and orgWideCoverage as same? How do I show orgWideCoverage on sonarqube?

Should I be running it as "--testlevel RunAllTestsInOrg"

  • RunLocalTests will run all tests in an org except managed packages. If you change to RunAllTestsInOrg that will include the managed packages. See the sfdx CLI reference. – nbrown Jun 9 '20 at 15:24

The Test Run Coverage displays the lines of code that were covered during the current test run. The Org Wide Coverage includes all the lines of code that were covered in any previously run unit test. This number may be higher if some unit tests were deleted from the org or no longer cover certain functions. You may want to clear the test coverage from Setup > Apex Test Execution > View Test History > Clear Test Data, then run your tests again. The numbers should then match.

  • Thanks for your quick response @sfdcfox, I cleared test data(history). Unfortunately it still gives me different numbers, Below is my script: sfdx force:apex:test:run --targetusername $username --resultformat json --codecoverage --testlevel RunAllTestsInOrg Do I need to make any changes? – Abhishek Anvekar Jun 10 '20 at 10:33
  • Should I consider the code coverage of managed package or no? The ORG wide coverage which I see on Salesforce UI does that contains managed package coverage as well ? – Abhishek Anvekar Jun 10 '20 at 11:01
  • @AbhishekAnvekar Yes, I believe so, though the docs don't seem to call this out specifically. You should be using the RunLocalTests run level, not RunAllTestsInOrg. The managed package coverage doesn't affect if a deployment will succeed. – sfdcfox Jun 10 '20 at 13:03
  • Ok, Thank you!! @sfdcfox – Abhishek Anvekar Jun 11 '20 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.