1

I want to run a pytest playwright test with multiple browsers - e.g. pytest --browser firefox --browser webkit

This works for function-based tests like this one:

import pytest
from playwright.sync_api import Page

@pytest.mark.playwright
def test_liveserver_with_playwright(page: Page, live_server):
    page.goto(f"{live_server.url}/")
    # etc.

The test is executed twice, once per browser setting.

However, I also want to use class-based tests, for which I use a fixture on a base class:

import pytest
import unittest
from playwright.sync_api import Page

@pytest.mark.playwright
class PlaywrightTestBase(unittest.TestCase):
    @pytest.fixture(autouse=True)
    def playwright_liveserver_init(self, page: Page, live_server):
        self.page = page
        # etc.

class FrontpageTest(PlaywrightTestBase):
    def test_one_thing(self):
        self.page.goto("...")
        # etc

The test runs, but only once - the multiple browser settings are ignored.

What am I missing - is there a way to get the multiple runs in such a setup as well ?

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
mfit
  • 807
  • 13
  • 28

1 Answers1

1

You're attempting to mix pytest fixtures with classes that inherit unittest.TestCase. But that can't be done directly. See https://stackoverflow.com/a/18187731/7058266 for details.

However, there are workarounds. For example, in https://stackoverflow.com/a/52062473/7058266 you see a way of using the parameterized Python library (https://pypi.org/project/parameterized/) for accomplishing the same goal. That's probably how you want to open multiple browsers. Combine that with pytest-xdist so that you can run multiple pytest tests at the same time with pytest -n NUM_PROCESSES.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • 1
    Thanks for answering. I now see that my use case is explicitly not supported, as stated in the source of [playwright-pytest on github](https://github.com/microsoft/playwright-pytest/blob/main/pytest_playwright/pytest_playwright.py) – mfit Jun 28 '23 at 10:49