Sub topic 15
Troubleshooting common test-related failures
This chapter assumes you know the four test levels, the 75% coverage rule, and how sf project deploy start sets a level. Here you learn to read a failed deployment and find the cause.
Start with the failure type
A test-related deployment failure falls into one of three groups. Check which one you have before you change any code.
Failing tests
Any test failure fails the whole deployment, and nothing is saved. Open the failure details in the CLI output or the Deployment Status page. Each entry gives the class, the method, the message, and a stack trace.
Common causes:
- Missing data. The test uses
@IsTest(SeeAllData=true)or assumes records that exist in your sandbox but not in production. Create every record the test needs inside the test. - Validation rules and required fields. A rule or required field added in the same deployment, or already in the target org, makes the test's inserts fail. Update the test data to satisfy it.
- Row locks. Errors such as
UNABLE_TO_LOCK_ROWcome from tests running in parallel and touching the same records. Rerun the failing class alone to confirm. - Tests you did not write.
RunAllTestsInOrgalso runs managed package tests. A failure there is not your code. UseRunLocalTestsinstead.
To read a job after the fact, use its job ID:
sf project deploy report --job-id <deploy-job-id> --target-org my-prodCoverage failures
Coverage messages name a percentage, so compare it with the rule for your level.
| Level | What must reach 75% |
|---|---|
| RunLocalTests | Overall coverage across the org's Apex |
| RunAllTestsInOrg | Overall coverage across the org's Apex |
| RunSpecifiedTests | Each class and trigger in the deployment, from the named tests |
The last row catches many people. With RunSpecifiedTests, a class can fail because the tests you named do not cover it, even if the org as a whole sits at 90%. Add the tests that cover that class to --tests.
Fixes for low coverage:
- Add assertions and calls that execute the uncovered lines, not only the happy path.
- Check that the test class is in the deployment or already in the target org.
- Make sure the test class is not marked
@IsTeston a method that is never reached because of an early return.
Level and quick-deploy errors
Production rejects NoTestRun when the package contains Apex. Switch to RunLocalTests or RunSpecifiedTests.
A quick deploy fails when no passing validation exists. The validation must have run tests, must have succeeded, and must be recent. The window is 10 days. Run the validation again if the job has expired or the target org changed in the meantime.
sf project deploy validate --source-dir force-app --target-org my-prod --test-level RunLocalTests
sf project deploy quick --job-id <validated-deploy-job-id> --target-org my-prodYou can also check that a validation job is still usable by running sf project deploy report on it.
Exercise
A production deployment with RunSpecifiedTests --tests AccountServiceTest fails with a coverage error for ContactService, although the org's overall coverage is 88%. Why, and what do you change?
Show answer
With RunSpecifiedTests, each class and trigger in the deployment must reach 75% from the named tests alone. AccountServiceTest does not cover ContactService enough. Add the test class that covers ContactService to the repeated --tests flag, or write one, then validate again.