Sub topic 12
What happens when a test fails
This chapter assumes you can read Apex tests, know the four test levels, and have run sf project deploy start. It covers what the platform does when a test fails during a deployment, and how to find the cause.
Deployments are all or nothing
A metadata deployment to an org is transactional. If any component fails to deploy, or any test that the deployment runs fails, the platform rolls back the whole deployment. No part of the change set is saved, including components that deployed cleanly.
The same applies to coverage. If the coverage requirement for the chosen test level is not met, the deployment fails and rolls back, even when every test passed.
What counts as a failure
Three kinds of problems stop a deployment at the test stage:
- A failing test method. An assertion fails or an unhandled exception is thrown.
- Insufficient coverage. The tests pass, but the covered lines fall below the required percentage for the level you chose.
- A test that cannot run. For example, a name passed to
--testsdoes not match a test class.
With NoTestRun, no tests run, so none of these can occur. Compile errors in the Apex code itself can still fail the deployment.
Reading the failure output
The CLI prints a table of failed tests with the class name, method name, error message and stack trace. Read the message first, then follow the stack trace to the line. If you need the output again, ask for the report by job ID:
sf project deploy report --job-id <deploy-job-id> --target-org my-prodCoverage failures list the classes or triggers that fall short, with their percentages. In an RunSpecifiedTests deployment, each class and trigger in the deployment must reach 75% from the tests you named. In RunLocalTests and RunAllTestsInOrg, the check is across the org.
Why a test can pass locally and fail on deploy
Most failures in this category come from a few causes:
- Dependence on org data. The test uses
SeeAllData=trueor assumes records exist. The target org has different data. A test that creates its own data, as inAccountServiceTest, avoids this. - Different configuration. Validation rules, required fields, flows or triggers in the target org reject records the test inserts.
- Test-order or limit effects.
RunLocalTestsruns every test in the org. A test unrelated to your change can fail, and that failure blocks your release. - Missing dependencies. A test needs a field, record type or permission that is not part of the deployment.
Partial deployment options
The CLI has an --ignore-errors flag that keeps successful components even when others fail. It does not apply to production deployments. Production always rolls back on error. Use the flag only in a sandbox for debugging, and expect the org to end up in a mixed state.
A practical loop
- Deploy, or validate, with the level your target requires.
- Read the failed test table and the stack trace.
- Reproduce locally, either by running the test in a scratch org or sandbox, or with
sf apex run test. - Fix the test or the code, then deploy again.
Validation is the safest place to find these failures. A validation run saves nothing, so a failure costs only the test time. The next chapter covers validation and quick deploy.
Exercise
You deploy to production with RunSpecifiedTests and name one test class. All its tests pass, but the deployment still fails and rolls back. What is the most likely cause, and what happens to the components that deployed without errors?
Show answer
The most likely cause is insufficient coverage. With RunSpecifiedTests, each class and trigger in the deployment needs at least 75% coverage from the named tests, and a class that falls short fails the deployment. The components that deployed without errors are rolled back too, because the whole deployment is transactional.