Playwright how to disable global teardown if any of the tests fail?

24 Views Asked by At

I am following this guide to accomplish a global teardown. So far so good and my teardown code runs every time whenever the test run is finished. The problem is, that teardown gets triggered every single time. I need it to be disabled if a failure/error is identified during the initial e2e test execution.

I am also using the test list to control the sequence in which my e2e tests are running because parallel runs are not for us. I know that tests should be built in isolation but it just does not work for us that way.

My test list: (test.list.js) @root

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

import usecase1 from './e2e/usecase-1.spec';

import usecase2 from './e2e/usecase-2.spec';

import usecase3 from './e2e/usecase-3.spec';

import usecase4 from './e2e/usecase-4.spec';


test.describe(usecase1);

test.describe(usecase2);

test.describe(usecase3);

test.describe(usecase4);

My global teardown file: (teardown.js) @root

import { chromium } from '@playwright/test';
async function globalTeardown()
{
const browser = await chromium.launch();
const page = await browser.newPage();
//some code doing the teardown
}
export default globalTeardown()

My playwright.config.js:

export default defineConfig({

testMatch: 'test.list.js',

globalTeardown: require.resolve('./teardown.js'),

fullyParallel: false,

projects: [

{

name: 'chromium',

use: {

...devices['Desktop Chrome'],

headless: false

}

},

.......

1

There are 1 best solutions below

1
unickq On

You can't access test results in GlobalTeardown.

GlobalTeardown is just a function executed after the test is finished - it knows nothing about the test execution context. You can get only Playwright config data there.

You may create Custom Reporter, which will create a file with failures that GlobalTeardown will read later and decide whether to do the next