1

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"

1
  • 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, 2020 at 15:24

1 Answer 1

5

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.

4
  • 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? Jun 10, 2020 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 ? Jun 10, 2020 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, 2020 at 13:03
  • Ok, Thank you!! @sfdcfox Jun 11, 2020 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .