What are test anti-patterns and how do you avoid them?
Answer
Common test anti-patterns that reduce test value: (1) Testing implementation details: tests break when you refactor without changing behavior. Fix: test outcomes/behavior, not internal calls. (2) Over-mocking: mock everything including internal collaborators — tests pass but real integration is broken. Fix: mock only external dependencies (DBs, APIs, filesystems). (3) The "Liar": test passes but doesn't actually test anything (assertion is always true, or testing the mock itself). Fix: run code coverage + mutation testing. (4) Tests too large: each test asserts too many things — hard to identify failures. Fix: one concept per test. (5) Shared mutable state: static fields, global variables shared between tests. Fix: initialize in SetUp, use DI. (6) Slow tests: tests that call real services, sleep, or do heavy I/O. Fix: mock I/O, use in-memory alternatives. (7) Non-deterministic tests: use real time, random data, external services. Fix: inject clock, seed random, mock external. (8) Excessive test setup: 50 lines of Arrange before Act. Fix: test data builders (builder pattern), object mother pattern. (9) Commented-out tests: silently skipping failing tests. Fix: fix or delete them.