Playwright teardown ONLY if all specs have passed

46 Views Asked by At

How do I run teardown in Playwright JS only if all my specs have passed?

As you can see from my setup, I turned off the parallel run and I am organising my tests in a list via test.list.js. That runs all fine and actually the teardown run every time after all my tests run BUT the problem is that teardown runs irrespective of test passing or failing. I would like it to exclude the teardown if there any is failure found.

 projects: [
    {
      name: 'setup',
      testMatch: 'test.list.js',
      teardown: 'myTeardown'
    },
    {
      name: 'myTeardown',
      testMatch: /destroy\.all\.js/,
      use: {
        ...devices['Desktop Chrome'],
        headless: false
    },
    },
    {
      name: 'chromium',
      use: {
         ...devices['Desktop Chrome'],
         headless: false,
         dependencies: ['setup']
     },
    },
1

There are 1 best solutions below

4
AutomationAndy On

EDIT: THIS WILL NOT WORK AS THE TEST RESULTS ARE NOT AVAILABLE TO TESTINFO UNTIL AFTER TEARDOWN HAS COMPLETED. LOOKING FOR SOLUTION :)

You should be able to get that from the testinfo fixture in project dependency teardown I believe:

// tests/dependencies/setup.ts

import { test as setup} from '@playwright/test';

setup('basic test setup', async ({}) => {
  // do something
});
// tests/dependencies/teardown.ts

import { test as teardown } from '@playwright/test';

teardown('basic test teardown', async ({}, testInfo) => {
  if (testInfo.errors.length === 0) {
    // teardown code here
  }
});
// playwright.config.ts

projects: [
    {
      name: "setup",
      testMatch: "tests/depedencies/setup.ts",
      teardown: "teardown",
    },
    {
      name: "teardown",
      testMatch: "tests/depedencies/teardown.ts",
    },
    {
      name: "chromium",
      use: { ...devices["Desktop Chrome"] },
      dependencies: ["setup"],
    },
  ],