Sub topic 3
Where tests fit in a metadata deployment
This chapter assumes you can read basic Apex and the @IsTest annotation. It also assumes you know the difference between a sandbox and production, and that you have run sf commands before. Here you will see where Apex tests run during a deployment and why they can block it.
A deployment is one all-or-nothing transaction
When you run sf project deploy start, the CLI packages your metadata and sends it to the target org. The org then processes the whole package as a single unit. If any component fails, the org rolls back the entire deployment, so you never end up with half of a change set applied.
Apex tests add a second check on top of the metadata check. The org first saves your components, then runs tests, then decides whether to keep the result. A deployment can therefore fail even though every component is valid, because a test failed or coverage was too low.
Why tests gate a deployment
Salesforce runs on shared infrastructure, so it wants evidence that new Apex does not break existing behavior. Tests provide that evidence. Two things are checked:
- Test results. Every test that runs must pass.
- Code coverage. The percentage of Apex lines that tests executed must meet the required threshold. The next chapter covers how coverage is measured.
A test class only counts toward coverage when it runs as part of the deployment. A passing test in your repository that is not part of the run does nothing for that deployment.
What decides whether tests run
Three inputs control the test step:
| Input | Effect |
|---|---|
| Target org type | Production enforces tests for Apex deployments. Sandboxes and scratch orgs are more relaxed. |
| Contents of the package | A package with no Apex classes or triggers has nothing to test. |
| The test level you choose | Selects which tests run, from none to every test in the org. |
The test level is the part you control. The four levels and their defaults are covered in later chapters. For now, the point is that the level is a setting on the deployment, not a property of your code.
A deployment with tests
This command deploys a source folder and asks the org to run all locally defined tests before committing:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsIf the tests pass and coverage is met, the changes commit. If not, the command reports the failures and the org stays as it was.
Common errors at this stage
- Assuming a valid component means a successful deployment. The component check and the test check are separate. Read both sections of the deploy output.
- Expecting a partial deploy. One failing test rolls back every component, including ones unrelated to the test.
- Relying on tests that create no data. Apex tests do not see existing org records by default, so a test that depends on them fails in a different org. Create the data inside the test, as
AccountServiceTestdoes in the example later in this tutorial. - Testing only in a sandbox. A sandbox deployment may skip tests, so problems first appear when you deploy to production.
Where this leads
Next you will see the four test levels side by side. Each one is a different answer to the question in the diagram: which tests run at the "Run Apex tests" step, and how strict the pass check is.
Exercise
A deployment contains 20 valid components and one Apex class whose test fails. What is the state of the org afterward, and why?
Show answer
The org is unchanged. The deployment is a single transaction, so the failed test causes a rollback of all 21 components, even though 20 of them were valid.