Yves Dorfsman
Yves Dorfsman

Reputation: 3045

Playwright teardown project is run before the tests

Adding global setup and teardown projects following the documentation exactly, the teardown project is always run before the actual tests. I tried to add the "teardown" key to the tests in addition to the setup, and it still runs before the tests.

Has anybody been successful with their teardown project running after the spec tests when following the example at https://playwright.dev/docs/test-global-setup-teardown#teardown-example ?

playwright.config.ts is the one generated by npx playwright init, with this modification where I add a global setup and teardown:

  projects: [
    {
      name: 'setup',
      testMatch: /global\.setup\.ts/,
      teardown: 'teardown',
    },  
    {
      name: 'teardown',
      testMatch: /global\.teardown\.ts/,
    },
    { 
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
      dependencies: ['setup'],
    },

    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
      dependencies: ['setup'],
    },    

tests/global.setup.ts

console.log("H E L L O !")

global.teardown.ts is the same file but it say "BYE".

When running, teardown is run immediately after setup, before the actual tests:

$ npx playwright test --project=firefox
H E L L O !
B Y E

Running 2 tests using 2 workers
  2 passed (5.3s)

If I remove the dependencies, neither setup nor teardown are run, as expected.

Upvotes: 1

Views: 1003

Answers (1)

hardkoded
hardkoded

Reputation: 21597

Your setup and teardown scripts need to look like a normal playwright test file:

global.setup.ts

import { test } from "@playwright/test";

test("Print hello", async () => {
  console.log("H E L L O !");
});

global.teardown.ts

import { test } from "@playwright/test";

test("Print bye", async () => {
  console.log("B Y E !");
});

Upvotes: 1

Related Questions