简介:随着Playwright测试用例集合在团队内增长,如何高效灵活管理逐渐变得重要,tag和grep功能可以有效解决这个问题。
比较常见的问题例如:
- 如何重新快速过滤失败的case,重新执行带有特殊标签
tag的用例 - 对测试集和测试套件进行功能分组feature
- 如何在
html report中展示某些特定的tag
grep介绍
grep是什么
grep是一强大的类Unix命令行使用程序,用于搜索纯文本数据集以查找与正则表达式匹配的行,是一种允许您使用正则表达式高效搜索文本的工具。
grep如何定义
在Playwright中,用--grep 或者-g定义
举个例子
用例定义如下:
import { test } from '@playwright/test';
test('customer can create an appointment', async ({ page }) => {
...
});
test('customer can see all appointments', async ({ page }) => {
...
});
test('customer can create an order', async ({ page }) => {
...
});
测试执行时候,使用grep指定cases:
# Run tests matching "customer" (all of them)
npx playwright test --grep "customer"
# Run tests matching "appointment" (the first two)
npx playwright test --grep "appointment"
# Run tests matching "order" (the last one)
npx playwright test --grep "order"
# Run tests matching "create" (the first and last ones)
npx playwright test --grep "create"
tag介绍
tag是什么
在Playwright,在测试描述中使用tag语法。
tag如何定义
在测试用例中定义tag,只需要在描述中加入@标签内容,其中标签内容可根据需求定义,比如
test('user can login @smoke @login', async ({ page }) => {
// Test implementation goes here
});
上面的例子中,定义了smoke、login这样2个tag
![图片[1]-Playwright实战-运用grep和tag更好管理测试集-365博客](https://qiniu.dianchuang365.cn/2025/02/20250205025008315.png)
@tag用例执行
Playwright命令行
--grep 指定匹配tag
# Run tests with the @smoke tag
npx playwright test --grep "@smoke"
--grep-invert 排除匹配tag
# Run tests with the @login tag, excluding those with the @smoke tag
npx playwright test --grep "@login" --grep-invert "@smoke"
OR组合
# Run tests with either the @smoke or @login tag (logical OR)
npx playwright test --grep "@smoke|@login"
AND组合
# Run tests with both the @smoke and @login tags (logical AND)
npx playwright test --grep "(?=.*@smoke)(?=.*@login)"
test.describe中使用
@tag除了在test()中外,也可以用于test.describe,例如:
import { test } from '@playwright/test';
test.describe('group, {tag: '@report'}, () => {
test('reporter header', async ({ page }) => {
// ...
});
test('full report', async ({ page }) => {
// ...
});
});
1. test.describe定义一组测试例, @report是指定的tag
2. npx playwright test --grep "@report"执行
结论
• @tag定义tag,--grep指定用例,可以灵活高效标记和筛选测试用例;
• --grep、--grep-invert、|等组合运用可以更加灵活选对特定用例;
• 采用清晰一致的tag定义,可以更好管理测试用例,比如
@smoke,标记冒烟测试@regression,标记回归测试@slow,标记用例执行时间@ci,标记CI pipeline执行用例
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END









暂无评论内容