What is the behavior of beforeAll() hook in Playwright Test when a test case fails and workers are set to 1?


Understanding beforeAll() Behavior with Failed Test Cases

Yes, beforeAll() re-runs after a test case failure when workers are set to 1. @playwright/test runs tests in worker processes, which are independent OS processes orchestrated by the test runner. When tests pass, they run in order within the same worker process. However, if a test fails, Playwright Test discards the entire worker process along with the browser and starts a new one. Testing continues in the new worker process starting with the next test.

If you enable retries and a test fails, a second worker process starts by retrying the failed test and continues from there. This means that the beforeAll() hook runs again in the new worker process after a failed test case.

This scheme works well for independent tests and ensures that failing tests can't affect healthy ones. You can use test.afterAll() to teardown any resources set up in beforeAll().

In summary, when a test case fails with workers set to 1 and retries enabled, beforeAll() re-runs in the new worker process after discarding the previous one along with its browser instance.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

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.