发布于 2026-01-06 1 阅读
0

使用新的 --only-changed 选项快速迭代

使用新的 --only-changed 选项快速迭代

Playwright v1.46版本新增了一项便捷的本地开发功能。
通过指定该--only-changed选项,Playwright 会检查尚未提交的更改并运行所有受影响的测试文件。

如果你以前使用过 Jest,这与 Jest 的--onlyChanged标志非常相似。

让我们来看看实际效果。如果您更喜欢视频演示,请观看发布视频中的演示。在这个例子中,我们有一个没有未提交更改的存储库:

// utils.ts
export const inputPlaceholder = 'What needs to be done?'
export const todoItemTestID = 'todo-item'
export const todo = 'buy milk'
Enter fullscreen mode Exit fullscreen mode
// test.spec.ts
import { test, expect } from '@playwright/test'
import { inputPlaceholder, todo, todoItemTestID } from './utils.ts'

test('adding a todo', async ({ page }) => {
    await page.goto('https://demo.playwright.dev/todomvc')

    const inputField = page.getByPlaceholder(inputPlaceholder)
    await inputField.fill(todo);
    await inputField.press('Enter');

    await expect(inputField).toBeEmpty();
    await expect(page.getByTestId(todoItemTestID)).toHaveText(todo);
})
Enter fullscreen mode Exit fullscreen mode

如果我们运行一个测试--only-changed,会发现没有运行任何测试:

$ npx playwright test --only-changed
Error: No tests found
Enter fullscreen mode Exit fullscreen mode

现在我们来修改其中一个文件,但暂时不要提交:

// utils.ts
export const inputPlaceholder = 'What needs to be done?'
export const todoItemTestID = 'todo-item'
- export const todo = 'buy milk'
+ export const todo = 'make cappucino'
Enter fullscreen mode Exit fullscreen mode
❯ npx playwright test

Running 1 test using 1 worker

  ✓  1 [chromium] › test.spec.ts:4:5 › adding a todo (426ms)

  1 passed (837ms)
Enter fullscreen mode Exit fullscreen mode

Playwright 检测到了文件内容的更改utils.ts,因此执行了所有依赖于该文件的测试文件utils.ts。太棒了!

默认情况下,Playwright 会与指定版本进行比较HEAD以确定已更改的文件。
但您也可以指定不同的 Git 版本进行比较:--only-changed=main在特性分支上使用此选项会执行受特性分支更改影响的所有测试,并执行指定提交与指定提交--only-changed=<commit sha>之间的所有更改。HEAD

我该如何使用它?

--only-changed是帮助你快速迭代的又一利器。以下五个建议可以帮助你将其融入到工作流程中:

1. 本地开发:用于--only-changed=main在进行更改时快速运行受影响的测试。这在开发某个功能并希望查看更改的影响时尤其有用。

2. pre-commit hook :在pre-commit hook--only-changed运行可以防止失败的测试被提交。以下是一个可以使用的 hook 示例:

npx playwright test --only-changed
Enter fullscreen mode Exit fullscreen mode

3. pre-push hook:顾名思义,pre-push hook在推送代码之前运行。这可以防止失败的测试触发代价高昂的 CI 运行。以下是一个示例实现:

while read local_ref local_sha remote_ref remote_sha
do
    # deleting branch, nothing to check
    if [ "$local_sha" = '0000000000000000000000000000000000000000' ]; then continue; fi

    echo "Running Playwright for HEAD...$remote_sha"
    npx playwright test --only-changed="$remote_sha"
done
Enter fullscreen mode Exit fullscreen mode

4. 加快持续集成 (CI) 中的反馈:在完整运行之前,使用此方法--only-changed可以更快地获取新失败的反馈。这在大型测试套件中尤其有用,因为运行所有测试可能需要很长时间。以下是 GitHub Actions 中的实现示例:

...
    - name: Install Playwright Browsers
      run: npx playwright install --with-deps
+   - name: Run changed Playwright tests
+     run: npx playwright test --only-changed=${{ github.base_ref }}
    - name: Run Playwright tests
      run: npx playwright test
...
Enter fullscreen mode Exit fullscreen mode

5. 额外建议:如果您对 Playwright 感兴趣--only-changed,您可能也会对 Playwright 的Watch 模式感兴趣,该模式可在 UI 和 VS Code 扩展中使用。

就是这样!请告诉我们您的想法以及您的使用情况--only-changed。您可以在这里的评论区、社区 DiscordLinkedIn上留言!祝您度过美好的一天。

文章来源:https://dev.to/playwright/iterate-quickly-using-the-new-only-changed-option-55m2