@canyonjs/playwright 的 Python 实现,为 Playwright Python 提供 Istanbul 覆盖率收集能力。
pip install canyonjs-playwright或从源码安装(开发模式):
pip install -e .在 conftest.py 中添加一行即可自动启用,所有使用 page 的测试都会自动收集覆盖率:
# conftest.py
pytest_plugins = ["canyonjs_playwright.pytest_plugin"]测试用例无需修改,直接使用 page:
def test_example(page):
page.goto("https://example.com")
assert "Example" in page.title()# conftest.py
import os
os.environ["CANYON_OUTPUT_DIR"] = ".canyon_output"
pytest_plugins = ["canyonjs_playwright.pytest_plugin"]# conftest.py
from canyonjs_playwright import create_coverage_context_fixture
coverage_context = create_coverage_context_fixture(output_dir=".canyon_output")# test_example.py
def test_example(page, coverage_context):
page.goto("https://example.com")python3 -m venv venv
source venv/bin/activate # macOS/Linux
# Windows: venv\Scripts\activatepython -m pip install -r requirements.txtpython -m playwright install chromiumpython -m pytest| 参数 | 说明 |
|---|---|
--headed |
有界面模式运行(显示浏览器窗口) |
-v |
详细输出 |
--tracing=retain-on-failure |
失败时保留 trace 便于调试 |
canyonjs-playwright-python/
├── src/
│ └── canyonjs_playwright/ # 包源码
│ ├── __init__.py
│ ├── fixtures.py
│ └── pytest_plugin.py
├── tests/
│ ├── conftest.py
│ ├── test_baidu.py
│ └── test_canyon.py
├── pyproject.toml
├── requirements.txt
├── pytest.ini
└── .github/workflows/
└── playwright.yml
覆盖率数据写入 coverage-{timestamp}.json,默认目录为 .canyon_output。
注意:仅当被测页面有 Istanbul 注入的
window.__coverage__时才会生成覆盖率文件。
- Python >= 3.8
- playwright >= 1.40.0
- pytest-playwright >= 0.4.0
推送到 GitHub 后,GitHub Actions 会自动运行测试。失败时可在 Actions 页面下载 trace 文件,在 trace.playwright.dev 查看。