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

如何通过Playwright命令按精确名称(完全匹配)执行测试

如何精确匹配Playwright测试名称执行指定测试

Playwright的-g/--grep参数支持正则表达式,要精确匹配名称恰好为test的测试,你可以使用锚点正则,把匹配范围限定在字符串的开头和结尾。

执行命令如下:

npx playwright test -g "^test$"
  • ^ 表示匹配字符串的起始位置
  • $ 表示匹配字符串的结束位置
  • 这样只会执行标题完全等于test的测试,不会匹配test2、3test这类仅包含test但名称不完全一致的测试

翻译后的代码示例

test("test", async ({}, testInfo) => console.log(`名称: ${testInfo.title}`));
test("test2", async ({}, testInfo) => console.log(`名称: ${testInfo.title}`));
test("3test", async ({}, testInfo) => console.log(`名称: ${testInfo.title}`));

内容的提问来源于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.26 12:01:05