Frequent questions: what is a unit, how much coverage, how fast, and when to skip.
- What counts as a "unit"?
A single function, method or class exercised in isolation from its collaborators. If the test boots the database, the network or the file system, it is an integration test, not a unit test.
- What coverage should I aim for?
70-80% line, 60-75% branch as a floor, 100% on authentication and payment paths. Do not chase 100% across the codebase; the marginal value drops steeply after the 80% mark.
- How fast should a unit test be?
Under 10 milliseconds each, whole suite under 2 minutes. If the suite is slower, the tests are doing too much; isolate more.
- Can I unit-test a database query?
Yes, if you mock the query builder or test against an in-memory database (SQLite, H2). The unit is the query construction, not the real database.
- Unit tests or TDD?
Unit tests are the artifact; TDD is the practice. TDD writes the test before the code; unit testing writes it after. Both produce the same artifacts, different workflows.