Skip to content

Instantly share code, notes, and snippets.

@qduc
Created July 31, 2025 02:57
Show Gist options
  • Save qduc/eb5f174e46614f4e4605929703c5c511 to your computer and use it in GitHub Desktop.
Save qduc/eb5f174e46614f4e4605929703c5c511 to your computer and use it in GitHub Desktop.

Unit Test Guidelines for Senior Developers

Test Behavior, Not Implementation
Focus on what the code does for its users, not how it does it. Private methods and call sequences matter only when they're part of the contract you're guaranteeing.

One Clear Failure Reason
Each test should fail for exactly one conceptual reason. If you need conditionals in your test, you're probably testing multiple scenarios—split them or move up to integration level.

Strategic Test Doubles
Mock what's slow, flaky, or expensive. Everything else should use real objects when practical. Over-mocking makes tests brittle; under-mocking makes them slow.

Performance Matters
Unit tests should run in milliseconds, not seconds. If your test suite takes more than a few seconds to run, developers will skip it. Design for fast feedback loops.

Test Data as Code
Treat test data setup with the same care as production code. Use builders, factories, or fixtures that make the test's intent obvious while hiding irrelevant details.

Coverage vs Risk
High coverage on critical paths beats 100% coverage on everything. A uncovered getter is fine; an uncovered error handler might not be.

Design Feedback Loop
If internal refactoring breaks many tests, you're coupled to implementation. If bugs slip through, you're missing edge cases. Let test pain guide better design.

Time and State Control
Isolate anything that depends on current time, random values, or global state. Flaky tests destroy team confidence faster than no tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment