You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Playwright实现测试并行与串行混合执行的配置问题

解决方案

要实现你想要的混合执行模式,需要调整配置中两个项目的worker分配,并确保串行项目内的测试严格按顺序执行,具体修改如下:

1. 调整项目的worker配置

在串行项目中明确指定workers: 1,强制该项目仅使用1个worker;并行项目指定workers: 2(对应总worker数3的分配)。修改后的配置代码:

export default defineConfig({
  testDir: './tests',
  testMatch: /.*Tests\.ts/,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 1 : 0,
  reporter: 'html',
  use: {
    viewport: {
      width: 1920,
      height: 1080
    },
  },
  // 总worker数保持3,由两个项目分配使用
  workers: 3,

  projects: [
    {
      name: 'Google Chrome - Parallel',
      testDir: "./tests/ParallelTests",
      fullyParallel: true,
      // 给并行测试分配2个worker
      workers: 2,
    },
    {
      name: 'Google Chrome - Serial',
      testDir: "./tests/SerialTests",
      fullyParallel: false,
      // 强制串行测试仅用1个worker,确保测试按顺序执行
      workers: 1,
    },
  ],
});

2. 确保SerialTests内的测试执行顺序

仅靠配置无法严格保证serial1Tests先于serial2Tests执行,需要在测试代码中用test.describe.serial包裹整个测试套件,明确串行执行顺序:

比如在serial1Tests.ts中:

test.describe.serial('Serial Test Suite 1', () => {
  test('serial test 1-1', async ({ page }) => {
    // 测试逻辑
  });

  test('serial test 1-2', async ({ page }) => {
    // 测试逻辑
  });
});

在serial2Tests.ts中:

test.describe.serial('Serial Test Suite 2', () => {
  test('serial test 2-1', async ({ page }) => {
    // 测试逻辑
  });
});

这样Playwright会先执行完serial1Tests的所有测试,再开始serial2Tests的测试,完全符合你要的串行要求。

内容的提问来源于stack exchange,提问作者half-proger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 18:57:15