Test Speed and the Fast Test Pattern
What you'll learn: How to keep your unit tests lightning-fast by avoiding slow operations like file I/O, database calls, and network requests.
Why Speed Matters
A good unit test should run in under 100 milliseconds. When you have hundreds or thousands of tests, even small delays add up fast. If each test takes 500ms, your 500-test suite takes over 4 minutes to run. At 50ms each? Just 25 seconds. That difference determines whether developers run tests constantly or avoid them.
The Fast Test Pattern
The key insight: unit tests should only touch in-memory code. The moment you interact with anything outside your process—databases, files, HTTP calls, or even the system clock—you've left "unit" territory and entered "integration."
Operations that kill test speed:
- Database queries (even in-memory databases add 10-100ms)
- File system reads/writes (disk I/O is notoriously slow)
- Network calls (APIs, web services, remote resources)
- Sleep/delay statements (waiting is the opposite of fast)
- Spawning processes (heavy OS operations)
The Solution
Replace slow external dependencies with test doubles (you'll learn these later). Instead of hitting a real database, use a simple in-memory object that behaves like one. Instead of reading a file, pass data directly into your function.
Your unit tests verify logic, not infrastructure. Save the real database for integration tests that run less frequently.
Key Takeaway: Unit tests must be fast (under 100ms) by avoiding all I/O operations—databases, files, networks, and file systems belong in integration tests, not unit tests.