Write assertions from the requirement
For a refund calculation, establish the expected amount from the policy before reading the implementation. Assert that exact amount for representative inputs. A test that computes its expectation with the same formula as production code can reproduce the same defect and pass while both versions remain wrong.
Make failures explain the condition and expected outcome. Prefer an exact status or collection comparison over a broad truthiness check. Several assertions can belong together when they describe one coherent result, but unrelated behaviors are easier to diagnose when they have their own focused test cases.
Use small fixtures and deliberate doubles
Create only the data needed for the rule. A massive shared fixture can hide the input that matters and couple unrelated tests. Give changed state a clear lifetime, and restore globals, environment variables, or fake clocks so the execution order does not affect later checks in the suite.
Use a fake clock to stabilize time-based rules and a small stub for unavailable external data. Avoid verifying every internal method call unless the interaction itself is the contract. Excessive call expectations often prevent safe refactoring without improving evidence about the user's observable business outcome.
Verify that tests challenge the code
Add boundary cases for equality, empty collections, missing values, and invalid states where the contract defines them. Do not add every theoretical combination automatically. Use the risk and known failure modes to select cases that distinguish a correct implementation from plausible mistakes in the behavior being tested.
Replay a known regression or introduce a bounded local defect to check whether the relevant assertion catches it. Treat a surviving defect as a gap to investigate. Keep the suite fast and reliable, and remove redundant checks only after confirming that remaining tests still cover the important behavior.
Working sheet
| Weak pattern | Better choice | Benefit |
|---|---|---|
| Result is truthy | Exact expected value | Detects wrong content |
| Live current time | Controlled clock | Stable boundary check |
| Large shared fixture | Minimal case data | Clear relevant inputs |
| Internal call count | Observable result | Safer refactoring |
Automate unit tests in CI
Run the same test command locally and in CI. Keep input fixtures, locale and random seeds explicit when they can affect the result. A test that needs a live external service belongs in a different layer.
Make failures visible and stable before making them required. Preserve the failing output and commit identity so a later green rerun does not erase the evidence.
Questions about unit testing best practices that improve failure detection
Must each test contain one assertion?
No. Keep assertions focused on one coherent behavior and make each failure clear.
Should every dependency be mocked?
No. Control dependencies outside the unit while preserving enough real behavior to test the intended rule.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
