Pytest和Playwright – 在使用类范围的固定装置时使用多个浏览器

huangapple go评论84阅读模式
英文:

Pytest and playwright - multiple browsers when using class-scoped fixtures

问题

我想要运行一个使用多个浏览器的pytest playwright测试 - 例如 pytest --browser firefox --browser webkit

这对于像这样的基于函数的测试有效:

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}/")
    # 等等。

测试会执行两次,每次使用不同的浏览器设置。

然而,我也想要使用基于类的测试,对于这种情况,我在一个基类上使用一个fixture:

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
        # 等等。

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

测试运行了,但只运行了一次 - 多个浏览器设置被忽略了。

我错过了什么 - 在这样的设置中有办法让它多次运行吗?

英文:

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 ?

答案1

得分: 1

你正在尝试将 pytest 的 fixture 与继承 unittest.TestCase 的类混合使用。但这不能直接完成。请参阅 https://stackoverflow.com/a/18187731/7058266 了解详细信息。

然而,有一些解决方法。例如,在 https://stackoverflow.com/a/52062473/7058266 中,你可以看到使用 parameterized Python 库(https://pypi.org/project/parameterized/)来实现相同目标的方法。这可能是你想要打开多个浏览器的方式。结合使用 pytest-xdist,这样你就可以使用 pytest -n NUM_PROCESSES 同时运行多个 pytest 测试。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月15日 20:08:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定