Sub topic 15
Building a Test Strategy That Stays Useful Over Time
This chapter assumes you can write basic Apex test classes, use the Assert class, and know the 75% deployment rule. It pulls the earlier ideas into a plan you can keep following as a codebase grows.
Treat Coverage as a Floor
Salesforce requires 75% overall coverage to deploy to production, and every trigger must have some coverage. That number is a gate, not a goal. A team that aims at exactly 75% has no margin when someone adds untested code. A team that aims at 100% often writes tests that only run lines.
A better rule is to set a higher target for new code, such as 90% or more, and to require that each test checks a result. Coverage tells you which lines never ran. It cannot tell you whether the ones that ran were verified.
Decide What Each Class Needs
For every class or trigger, ask which of these cases apply, and write a test for each.
| Case | Question it answers | Typical technique |
|---|---|---|
| Happy path | Does it return the right result for valid input? | `Assert.areEqual` on the output |
| Bulk | Does it work with 200 records in one operation? | Insert a list, check the count of changed records |
| Negative | Does it reject bad input in the right way? | `try`/`catch` plus `Assert.fail` |
| User context | Does it behave correctly for a restricted user? | `System.runAs` |
| Callout | Does it handle good and bad responses? | `Test.setMock` |
| Asynchronous | Does the job do its work after it finishes? | `Test.startTest` and `Test.stopTest` |
Not every class needs every row. A pure calculation needs happy and negative tests. A trigger needs bulk tests. A callout class needs both a success mock and a failure mock.
Keep Tests Independent and Readable
Tests last longer when they follow a few habits.
- Create your own data with a factory method or
@TestSetup. Do not use@isTest(SeeAllData=true), because it ties the test to whatever data the org holds. - Give each test one reason to fail. Name it after the behavior, such as
rejectsPercentOutOfRange. - Write a message in every assertion so a failure explains itself.
- Do not depend on test order or on data created by another test method.
Review Tests Like Production Code
A weak test can look fine in a coverage report. Check for these during code review:
- A test method with no assertion.
- An assertion that cannot fail, such as
Assert.isTrue(true). - A
catchblock that swallows an exception without callingAssert.failon the no-exception path. - A test that only checks
result != null.
The next example is the kind of test a review should reject.
@isTest
private class WeakDiscountCalculatorTest {
@isTest
static void coversLinesButChecksNothing() {
DiscountCalculator.apply(100, 10);
}
}It raises coverage, but a bug that returned 100 instead of 90 would still pass.
Make Testing Part of the Workflow
Run the tests that matter at each stage, not only at deployment.
Running only the affected test classes while you work keeps feedback fast. Running the full suite before release catches interactions between classes. When coverage drops after a change, add tests for the new code before merging. Do not wait for the production deployment to fail.
Summary Checklist
- Treat 75% as the minimum and set a higher target for new code.
- Choose test cases by the kind of code: happy, bulk, negative, user, callout, async.
- Build your own data and avoid
SeeAllData. - Reject tests with no meaningful assertion.
- Run tests at every stage, not just at deployment.
Exercise
A teammate's pull request raises a class from 60% to 95% coverage. The new test calls the method with valid input and has no assertions. Name two things you would ask them to add.
Show answer
Ask for assertions that check the returned value or the changed data, so a wrong result would fail the test. Also ask for a negative test for invalid input, using try/catch with Assert.fail so the test fails if no exception is thrown. A bulk test is a good third addition if the code runs from a trigger.
Exercise
Why is aiming for exactly 75% coverage risky, even though it passes the deployment rule?
Show answer
Any later change that adds untested lines can push the org under 75% and block deployment. A 75% target also leaves 25% of the code unverified, and the number says nothing about whether the executed lines were checked by assertions.