David Pongo
David Pongo

Reputation: 125

Playwright teardown ONLY if all specs have passed

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']
     },
    },

Upvotes: 0

Views: 458

Answers (1)

AutomationAndy
AutomationAndy

Reputation: 1012

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"],
    },
  ],

Upvotes: 0

Related Questions