DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

When should you use test.retry versus test.failFast in Playwright's test runner, and what are the performance and reliab

🔥 Playwright Test Flakiness: Retry or Fail Fast?
You're debugging a CI pipeline riddled with intermittent Playwright test failures. Should you re-run them or stop the build immediately?

📌 Problem Statement
Flaky tests plague modern CI/CD. These unpredictable failures waste time, resources, and erode team confidence. Playwright's test runner offers test.retry and test.failFast to manage this, but choosing the right strategy is crucial for efficiency and reliability.

💡 Solution & Code Walkthrough
Playwright provides two powerful mechanisms to control test execution flow based on failure:

test.retry: Automatically re-executes a failing test up to N times.
test.failFast: Aborts the entire suite (or specific project) after the first failure.

When to prefer test.retry

Why it helps:
Intermittent network glitches: Transient API or CDN issues often pass on a second attempt.
Non-deterministic UI animations: UI elements occasionally fail to load in time, a retry can fix this.
External service rate-limits: A short delay and retry can bypass temporary 429 errors.
Trade-offs:
• Increases total suite runtime, potentially wasting CI minutes.
• Can mask genuine, underlying bugs if overused.
• Adds extra load on CI agents for re-runs.
Best Practice: Set a modest global retry (e.g., retries: 1). Use per-test overrides sparingly for known flaky tests. Keep the count low (< 2).

When to prefer test.failFast

Why it helps:
Critical regression: If login or core functionality fails, continuing the suite generates a flood of dependent failures.
Resource-constrained CI: Stops wasteful allocation of containers or VMs for tests that are guaranteed to fail.
Immediate Feedback: Essential for fast dev cycles when core functionality breaks.
Trade-offs:
• An early abort may hide secondary issues that could have been discovered later in the suite.
Best Practice: Apply to critical test projects (e.g., critical-paths) where any failure signifies a major blocker.

Playwright Configuration Example

// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  // Globally enable retries for all tests on CI
  retries: process.env.CI ? 1 : 0, 

  projects: [
    {
      name: 'critical-paths',
      testMatch: /critical\.spec\.ts/,
      failFast: true, // Fail this project immediately on first error
    },
    {
      name: 'e2e',
      testMatch: /e2e\.spec\.ts/,
      // No failFast for general E2E, allow other tests to run
    },
  ],
});
Enter fullscreen mode Exit fullscreen mode

🔑 Key Takeaways
test.retry is for transient, often external, flakiness that might pass on re-execution.
test.failFast is for critical failures indicating a major blocker, saving valuable CI resources.
• Thoughtfully balance these strategies to maintain a reliable, efficient CI pipeline without masking real bugs.

❓ Quick Summary Q&A
Q: When should I use test.retry?
A: For transient issues like network glitches or UI timing problems that might resolve themselves.

Q: When should I use test.failFast?
A: For critical path failures (e.g., login broken) or when CI resources are limited, to get immediate feedback.

TAGS: playwright, testing, automation, sdet, ci/cd, web testing

────────────────────────────────────────
Ready to master Playwright and ace your interviews?
Download our app for expert guides, interactive courses, and practice questions:
────────────────────────────────────────

📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20260915

🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20260915&mt=8

────────────────────────────────────────
────────────────────────────────────────

Top comments (0)