How to set Playwright test directory

Playwright projects often separate end-to-end specs from unit tests, helper files, and generated fixtures. Setting a dedicated test directory tells Playwright Test where to collect spec files so routine runs do not sweep unrelated folders.

The testDir setting lives in playwright.config.ts or playwright.config.js. Playwright scans that directory recursively, applies testMatch inside it, and reports listed files relative to the configured directory.

Use one directory string for testDir, such as ./e2e. If a project must collect several separate folders, keep a shared parent as testDir and narrow discovery with testMatch instead of trying to list multiple directories.

Steps to set Playwright test directory:

  1. Open a terminal at the Playwright project root.
  2. Create the directory for collected specs.
    $ mkdir -p e2e
  3. Move an existing spec into the new directory.
    $ mv tests/home.spec.ts e2e/home.spec.ts

    Update relative imports, fixture paths, and snapshot paths after moving test files. A spec that imports helpers with ../ paths may fail even when Playwright discovers it.

  4. Set testDir in the Playwright configuration file.
    import { defineConfig } from '@playwright/test';
    
    export default defineConfig({
      testDir: './e2e',
    });

    Relative testDir values resolve from the directory that contains the Playwright configuration file.

  5. List discovered tests from the configured directory.
    $ npx playwright test --list
    Listing tests:
      home.spec.ts:3:5 › home page shows ready state
    Total: 1 test in 1 file

    The listed path is relative to ./e2e because testDir is set there. If the total is 0 tests, check the directory path and the file suffix used by testMatch.

  6. Run the suite from the configured directory.
    $ npx playwright test
    
    Running 1 test using 1 worker
    ·
      1 passed (716ms)