Get Improved Code Coverage Results for Your Apex Tests

We improved the force:apex:test:run and force:apex:test:report commands to provide more accurate code coverage results. Previously, running these commands with the codecoverage flag returned results across multiple test runs. Now, the results show only the classes that a test execution entered during the run. And, the coverage calculations factor only these classes.

Where: This change applies to Lightning Experience, Salesforce Classic, and all versions of the Salesforce app in all editions.

How: The code coverage results are now scoped to the tests in a given test run. For example, if you run TestClassA and TestClassB, and TestClassA executes code only in ClassA and TestClassB executes only ClassB. The code coverage result for the first run includes only ClassA, and the second run includes only ClassB.

To enable this new behavior, set the environment variable SFDX_IMPROVED_CODE_COVERAGE to true. When you run the force:apex:test:run and force:apex:test:report CLI commands after enabling the new behavior, you’ll notice changes in your code coverage numbers. The new numbers reflect a more accurate code coverage result.