Course contentsShow
← Software Engineering
Lesson 441 of 1,93313. Unit Testing in DepthFree lesson

Fast Feedback: Keeping Tests Quick

Write unit tests that execute in milliseconds by avoiding I/O, databases, and external dependencies.

Fast Feedback: Keeping Tests Quick

What you'll learn: How to write unit tests that run in milliseconds, giving you instant feedback on code changes.

Why Speed Matters

Imagine hitting "save" and waiting 30 seconds to know if your code works. You'd lose focus, check your phone, and break your flow. Fast tests—running in milliseconds—keep you in the zone. The goal: sub-second test suites that you run constantly without thinking twice.

The Core Principle

Unit tests should execute blazingly fast by avoiding anything that touches the outside world. Here's what slows tests down:

  • File I/O: Reading or writing files
  • Database calls: Even in-memory databases add overhead
  • Network requests: APIs, external services, anything across a network
  • External processes: Shelling out to other programs
  • Sleep/wait statements: Artificial delays

How to Stay Fast

Your unit test should only interact with objects in memory. Test pure logic: functions that take inputs and return outputs without side effects. If your code needs a database, test the logic around the database without actually connecting.

Think of it like testing a calculator app. You don't need to test if the screen pixels light up—that's slow. Test if add(2, 3) returns 5. That's instant.

// Fast: pure logic, no dependencies
function calculateDiscount(price, percentage) {
  return price * (percentage / 100);
}

// Test runs in < 1ms
assert(calculateDiscount(100, 10) === 10);

When you follow the Testing Pyramid, unit tests form the base precisely because they're fast enough to run hundreds of times per hour without breaking your rhythm.

Key Takeaway: Unit tests should run in milliseconds by testing pure logic in memory—no I/O, no databases, no network calls. Fast tests give you instant feedback and encourage running them constantly.