Rayrun
← Back to Discord Forum

teardown gets run like any other test, not AFTER

My teardown gets run like if it were any other test, regardless of my adding the teardown keyword anywhere.

I would like it to be run only after all the tests are complete. What am I missing here?

This thread is trying to answer question "How can I ensure that the teardown function runs only after all tests are complete in Playwright?"

16 replies

Actually, it looks like both dependencies and teardown are not taken into account. If I run npx playwright test --project=chromium then it only runs the chromium projects but not the setup!

Anybody managed to get a teardown project to run AFTER the tests?

refactoreric

Hi, could you show your playwright.config.ts ?

refactoreric

Could you show the source of the global setup and teardown files? Where are you doing the console.logs ?

Doing the console.log to see when they are run, that's the smallest setup/teardown I could think of. But as noted in that report, when hitting an api from setup, tests and teardown, I can see when the backend is getting hit, and it goes setup, teardown, and only then are the tests run.

in github, your teardown is a property of setup

so it runs after setup and before other tests

refactoreric

Hi @dorfsmay , actually in your sample/repro code, I don't see where the problem is. But I did a test locally by starting from zero and for me it worked as expected.

Instead of console.log I wrote log files, just to avoid the risk that console.log might get intercepted by reporters and might output in a different order than the actual execution order.

eveltman@erics-air playwright-proj-deps % cat *.log | sort   
1705691574285 starting setup
1705691575098 starting testA
1705691575099 starting testB
1705691575847 starting teardown

I've attached the combined project files used for this test.

Ah! I was not using test() in my setup/teardown. Adding them solved the issue. Thank you.

Thanks @refactoreric !

So this works well from cli, but if I add --ui it runs both setup and teardown, then launches the ui, and when I click on the run button the test fails because my users have been deleted...

refactoreric

Seems to be as per documentation: https://playwright.dev/docs/test-ui-mode#filtering-tests "If you are using project dependencies make sure to run your setup tests first before running the tests that depend on them. The UI mode will not take into consideration the setup tests and therefore you will have to manually run them first."

Edit: Hmm not exactly as you describe.

It's weird that ui behaves differently than cli, and that's painful.

refactoreric

Maybe best to post a GitHub issue for it, or vote on an existing one.

I found out a feature has been added. All one needs to do is tick the setup and teardown in the filter dropdown before running the tests, and they will then be executed with the correct order.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.