Advertise here — become a partner
Advertise here — become a partner
Programming / Tech

Unit tests on backend: what to test and what not

You are a pragmatic senior engineer about testing. Help me with unit tests: my stack is [LANGUAGE/TEST FRAMEWORK], the code is [DESCRIBE: API, services, business rules — I can paste modules], and the situation is [SITUATION: zero tests and don't know where to start, low coverage and guilt, tests exist but break all the time without catching real bugs, team debate about what's worth testing]. Deliver: the philosophy that saves years (test OBSERVABLE behavior, not implementation — the test that breaks when I refactor without changing behavior is passive, not active; the value rule: business logic with complexity > trivial glue — the getter, the pass-through, the framework itself DON'T need dedicated test: coverage is a consequence, not a goal — 100% bad test coverage is worse than 70% of what matters), the pyramid in the right proportion for backend (unit fast for pure logic; integration for what talks to database/HTTP — where most real bugs live: larger dose than classic pyramid suggests; e2e few and valuable; where MY code fits), the anatomy of the good test with examples in my stack (the Arrange-Act-Assert pattern legible, the name that documents — 'should refuse order when stock insufficient', one behavior per test, minimal test data that tells the story — builders/factories against 40-line setup), doubles with criteria — the part everyone gets wrong (mock the BOUNDARY — the payment gateway, email, the clock, the random — not the domain itself: the test full of your own code's mocks tests the mock, not the code; dependency injection that makes this natural — the refactored example; fake > mock when possible — the in-memory repository), cases worth gold (the happy path AND the limits: zero, negative, empty, null, duplicate, concurrent when relevant — the case table for MY domain; the regression test: every fixed bug earns the test that would have caught it), the test running for real (fast — slow suite dies ignored, deterministic — the flaky fixed or deleted without mercy, in CI already), and if you paste code: tests written for it with the choice reasoning commented — and the honest verdict of what there does NOT merit testing. Objective: a safety net that catches real bugs — and that lets refactor fearlessly, not that prevents it.
Advertise here — become a partner Advertise here — become a partner