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

Test File Organization and Structure

How to organize test files relative to source code, including mirrored directory structures and naming conventions.

Test File Organization and Structure

What you'll learn: How to arrange test files alongside your source code so they're easy to find, maintain, and run.

Why Organization Matters

Imagine searching for a user manual that's randomly placed somewhere in your house versus one that's always in a specific drawer next to the device. Good test organization means you never waste time hunting for the right test file—and neither does anyone else on your team.

The Mirror Structure Pattern

The most common approach is to mirror your source directory structure in your test folder. If your source code lives in src/, your tests live in test/ or tests/ with the exact same folder hierarchy:

src/
  user/
    authentication.js
  payment/
    processor.js

test/
  user/
    authentication.test.js
  payment/
    processor.test.js

This one-to-one mapping makes it instantly obvious which test file covers which source file.

Naming Conventions

Use clear, consistent suffixes for test files:

  • authentication.test.js or authentication.spec.js
  • Match the source filename exactly, just add the test suffix
  • Some teams prefer authentication_test.js (underscores)

The goal: anyone should instantly recognize "this is a test file."

Why This Works

This structure supports fast feedback. Your editor and test runner can quickly locate tests. When you modify authentication.js, you immediately know to check authentication.test.js. No guessing, no searching through oddly-named folders.

Some frameworks support co-located tests (test files sitting right next to source files), but mirrored structures remain the industry standard—they keep production code separate from test code during deployment.

Key Takeaway: Mirror your source directory structure in your test folder and use consistent naming suffixes so tests are trivially discoverable and maintainable.