Check apex tests org wide coverage while simulating Salesforce deployments to sandboxes
BOSS: We have tested everything, now let’s deploy to production, the client is waiting !
DEV: Hey boss… sorry I tried but I can’t deploy, the code coverage is 72% and the colleague who did the development is not available
BOSS: Do whatever is necessary, but we need to deploy in production !
DEV: Ok, I’ll add a thousand empty lines with
i=0;
so we can deploy
Who here never experienced such situation ?
And how many times such useless code has been refactored ? (SPOILERS: Zero…)
The problem
Salesforce does not enforce the 75% of code coverage when deploying in sandboxes.
So when you are deploying to dev, UAT or preprod a code that does not reaches those mandatory 75%, you do not see any error… and you’re not even obliged to run the test classes.
So you can deploy code with failing apex tests classes at worse, but also with incomplete code coverage.
And the day you want to go live in production, you are stuck and forced to generate technical debts to deploy.
The solution
Each sfdx deployment to any org must be previously simulated:
- With all local Apex Test cases run
- With control of the minimum code coverage percentage
Salesforce DX provides a method to simulate deployments while running test cases:
sfdx force:source:deploy -x manifest/package.xml --wait 60 --ignorewarnings --testlevel RunLocalTests --postdestructivechanges manifest/destructiveChanges.xml --checkonly --verbose --coverageformatters json-summary
But this command can not fail in case the minimum code coverage is reached, and Salesforce has not planned to support it in a near future (cf Github issue comment)
The sfdx-hardis command sfdx force:source:deploy
had already been wrapped within sfdx hardis:source:deploy
, to help solving deployment errors, as explained in this article.
We added an option — checkcoverage, that will parse the code coverage results then control that the minimum code coverage percentage is reached, else fail with an explicit message.
sfdx hardis:source:deploy -x manifest/package.xml --wait 60 --ignorewarnings --testlevel RunLocalTests --postdestructivechanges ./manifest/destructiveChanges.xml --checkonly --coverageformatters json-summary --verbose --checkcoverage
You find this command too long ? Use our shorter command that does the same :) (very useful in Salesforce CI/CD pipelines !)
sfdx hardis:project:deploy:sources:dx --check
You don’t like command lines ? Use VsCode SFDX Hardis menu Simulate SFDX Deployment
Bonus: Customize minimum org code coverage
Default checked percentage is 75%, but you can have higher expectations, by overriding its value, by:
- Defining property apexTestsMinCoverageOrgWide in config file config/.sfdx-hardis.yml
- Defining environment variable APEX_TESTS_MIN_COVERAGE_ORG_WIDE (can be done in your CI variables)