Skip to main content

Pytest 插件参考

Playwright 提供了一个 Pytest 插件来编写端到端测试。要开始使用它,请参阅 入门指南

用法

要运行您的测试,请使用 Pytest CLI。

pytest --browser webkit --headed

如果您想自动添加 CLI 参数而无需指定它们,可以使用 pytest.ini 文件:

CLI 参数

  • --headed: 以有头模式运行测试(默认:无头)。
  • --browser: 在不同的浏览器 chromiumfirefoxwebkit 中运行测试。可以多次指定(默认:chromium)。
  • --browser-channel 要使用的 浏览器通道
  • --slowmo 使用慢动作 (slow mo) 运行测试。
  • --device 要模拟的 设备
  • --output 测试产生的工件的目录(默认:test-results)。
  • --tracing 是否为每个测试记录 traceonoffretain-on-failure(默认:off)。
  • --video 是否为每个测试录制视频。onoffretain-on-failure(默认:off)。
  • --screenshot 是否在每个测试后自动捕获截图。onoffonly-on-failure(默认:off)。

Fixtures

此插件配置了 Playwright 特定的 pytest fixtures。要使用这些 fixtures,请使用 fixture 名称作为测试函数的参数。

def test_my_app_is_working(fixture_name):
# 使用 fixture_name 测试
# ...

函数作用域:这些 fixtures 在测试函数中请求时创建,并在测试结束时销毁。

会话作用域:这些 fixtures 在测试函数中请求时创建,并在所有测试结束时销毁。

  • playwright: Playwright 实例。
  • browser_type: 当前浏览器的 BrowserType 实例。
  • browser: Playwright 启动的 Browser 实例。
  • browser_name: 字符串形式的浏览器名称。
  • browser_channel: 字符串形式的浏览器通道。
  • is_chromium, is_webkit, is_firefox: 相应浏览器类型的布尔值。

自定义 Fixture 选项:对于 browsercontext fixtures,使用以下 fixtures 定义自定义启动选项。

并行:同时运行多个测试

如果您的测试在具有许多 CPU 的机器上运行,您可以使用 pytest-xdist 同时运行多个测试,从而加快测试套件的整体执行时间:

# 安装依赖
pip install pytest-xdist
# 使用 --numprocesses 标志
pytest --numprocesses auto

根据硬件和测试的性质,您可以将 numprocesses 设置为从 2 到机器上的 CPU 数量。如果设置得太高,您可能会注意到意外的行为。

有关 pytest 选项的一般信息,请参阅 运行测试

示例

配置 Mypy 类型以实现自动补全

# test_my_application.py
from playwright.sync_api import Page

def test_visit_admin_dashboard(page: Page):
page.goto("/admin")
# ...

配置慢动作 (slow mo)

使用 --slowmo 参数运行带有慢动作的测试。

pytest --slowmo 100

按浏览器跳过测试

# test_my_application.py
import pytest

@pytest.mark.skip_browser("firefox")
def test_visit_example(page):
page.goto("https://example.com")
# ...

在特定浏览器上运行

# conftest.py
import pytest

@pytest.mark.only_browser("chromium")
def test_visit_example(page):
page.goto("https://example.com")
# ...

使用自定义浏览器通道(如 Google Chrome 或 Microsoft Edge)运行

pytest --browser-channel chrome
# test_my_application.py
def test_example(page):
page.goto("https://example.com")

配置 base-url

使用 base-url 参数启动 Pytest。为此使用了 pytest-base-url 插件,它允许您从配置、CLI 参数或作为 fixture 设置基本 url。

pytest --base-url http://localhost:8080
# test_my_application.py
def test_visit_example(page):
page.goto("/admin")
# -> 将结果为 http://localhost:8080/admin

忽略 HTTPS 错误

# conftest.py
import pytest

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
return {
**browser_context_args,
"ignore_https_errors": True
}

使用自定义视口大小

# conftest.py
import pytest

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
return {
**browser_context_args,
"viewport": {
"width": 1920,
"height": 1080,
}
}

设备模拟

# conftest.py
import pytest

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright):
iphone_11 = playwright.devices['iPhone 11 Pro']
return {
**browser_context_args,
**iphone_11,
}

或者通过 CLI --device="iPhone 11 Pro"

持久上下文

# conftest.py
import pytest
from playwright.sync_api import BrowserType
from typing import Dict

@pytest.fixture(scope="session")
def context(
browser_type: BrowserType,
browser_type_launch_args: Dict,
browser_context_args: Dict
):
context = browser_type.launch_persistent_context("./foobar", **{
**browser_type_launch_args,
**browser_context_args,
"locale": "de-DE",
})
yield context
context.close()

使用它时,测试中的所有页面都是从持久上下文创建的。

unittest.TestCase 一起使用

请参阅以下示例,了解如何将其与 unittest.TestCase 一起使用。这有一个限制,即只能指定一个浏览器,并且指定多个浏览器时不会生成多个浏览器的矩阵。

import pytest
import unittest

from playwright.sync_api import Page


class MyTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def setup(self, page: Page):
self.page = page

def test_foobar(self):
self.page.goto("https://microsoft.com")
self.page.locator("#foobar").click()
assert self.page.evaluate("1 + 1") == 2

调试

与 pdb 一起使用

在您的测试代码中使用 breakpoint() 语句暂停执行并获取 pdb REPL。

def test_bing_is_working(page):
page.goto("https://bing.com")
breakpoint()
# ...

部署到 CI

请参阅 CI 提供商指南 以将您的测试部署到 CI/CD。