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

如何在同一Playwright测试文件中混合串行与并行执行测试?

同一Playwright测试文件实现部分并行、部分串行执行

完全可以实现这种混合执行模式,你只需要用test.describe将不同执行要求的测试用例分组,再为每个分组单独配置执行模式即可。

由于你已经在playwright.config中设置了fullyParallel: true,默认所有测试用例会以并行模式运行,但你可以通过test.describe.configure()为特定分组覆盖全局配置:

示例代码

// a、b 保持并行执行(全局默认配置,无需额外设置)
test("a", async ({}) => console.log("a"));
test("b", async ({}) => console.log("b"));

// 为c、d所在的分组配置串行模式
test.describe.configure({ mode: 'serial' });
test.describe('串行测试集合', () => {
  test("c", async ({}) => console.log("c"));
  test("d", async ({}) => console.log("d"));
});

如果想要更清晰地划分执行逻辑,也可以给a、b显式配置并行分组:

// 显式配置a、b为并行分组
test.describe.configure({ mode: 'parallel' });
test.describe('并行测试集合', () => {
  test("a", async ({}) => console.log("a"));
  test("b", async ({}) => console.log("b"));
});

// 配置c、d为串行分组
test.describe.configure({ mode: 'serial' });
test.describe('串行测试集合', () => {
  test("c", async ({}) => console.log("c"));
  test("d", async ({}) => console.log("d"));
});

关键说明

  • test.describe.configure()的配置仅对当前后续的test.describe块生效,不会影响其他分组或全局配置
  • 串行模式下,同一分组内的测试用例会按编写顺序依次执行,必须等待前一个测试完成后才会启动下一个
  • 并行模式下的测试用例则会同时启动,互不阻塞

内容的提问来源于stack exchange,提问作者I.sh.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 06:41:07