日本語

How to Tag and Filter Playwright Tests with –grep

​​Playwright​E2E Testing,TypeScript

If you tag Playwright tests with values like @smoke, you can run only those tests with npx playwright test --grep @smoke. This lets you run a smoke test that quickly checks the main screens before a deploy, or run E2E tests with the slow ones excluded, without splitting the test files.

The tag option for adding tags was introduced in Playwright 1.42 (1.42 release notes). Here I ran the tests in a test environment to check how to add tags, how to select them with --grep, and the points that are easy to trip over when selecting.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, and @playwright/test 1.63.0. The tests were written in TypeScript, and I used only headless Chromium with a single worker. I did not try the commands in Windows PowerShell or Command Prompt, or running in CI services such as GitHub Actions.

Three ways to add tags to tests

For testing, I prepared one HTML page with a product list and a login form, and wrote seven tests across two test files. tests/cart.spec.ts is as follows.

ts
import { test, expect } from '@playwright/test';
import { readFileSync } from 'node:fs';

test.beforeEach(async ({ page }) => {
  await page.setContent(readFileSync('site/shop.html', 'utf8'));
});

test('商品が2つ並ぶ', { tag: '@smoke' }, async ({ page }) => {
  await expect(page.getByRole('listitem')).toHaveCount(2);
});

test('合計金額が450円になる', { tag: ['@smoke', '@regression'] }, async ({ page }) => {
  await expect(page.locator('#total')).toHaveText('合計 450円');
});

test('クーポンの入力欄がある', { tag: '@regression' }, async ({ page }) => {
  await expect(page.getByLabel('クーポン')).toBeVisible();
});

test('商品を100個並べても購入ボタンが見える @slow', async ({ page }) => {
  await page.evaluate(() => {
    const ul = document.querySelector('ul')!;
    for (let i = 0; i < 100; i++) ul.insertAdjacentHTML('beforeend', `<li>商品${i}</li>`);
  });
  await expect(page.getByRole('button', { name: '購入する' })).toBeVisible();
});

You add a tag by passing { tag: '@smoke' } after the test name. To add several, use an array. As in the fourth test, writing @slow inside the name also works as a tag. As the official documentation on tagging tests explains, in both forms the tag starts with @.

In tests/login.spec.ts, I added a tag to test.describe. The import at the top and the beforeEach are the same as in cart.spec.ts, so I have left them out.

ts
test.describe('ログイン', { tag: '@login' }, () => {
  test('メールアドレスの入力欄がある', { tag: '@smoke' }, async ({ page }) => {
    await expect(page.getByLabel('メールアドレス')).toBeVisible();
  });

  test('空のまま押すとエラーが出る', { tag: '@regression' }, async ({ page }) => {
    await page.getByRole('button', { name: 'ログイン' }).click();
    await expect(page.locator('#err')).toHaveText('メールアドレスを入力してください');
  });
});

test('パスワードを忘れた場合のリンクがある', { tag: '@smoke-ui' }, async ({ page }) => {
  await expect(page.getByRole('link', { name: 'パスワードを忘れた場合' })).toBeVisible();
});

When I ran everything with --reporter=list, the tags appeared at the end of each test's line. The @login tag set on describe appears before each test's own tags on the two tests inside it.

text
$ npx playwright test --reporter=list
Running 7 tests using 1 worker
  ✓  1 [chromium] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ @smoke (114ms)
  ✓  2 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる @smoke @regression (87ms)
  ✓  3 [chromium] › tests/cart.spec.ts:16:1 › クーポンの入力欄がある @regression (81ms)
  ✓  4 [chromium] › tests/cart.spec.ts:20:1 › 商品を100個並べても購入ボタンが見える @slow (85ms)
  ✓  5 [chromium] › tests/login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある @login @smoke (76ms)
  ✓  6 [chromium] › tests/login.spec.ts:13:3 › ログイン › 空のまま押すとエラーが出る @login @regression (120ms)
  ✓  7 [chromium] › tests/login.spec.ts:19:1 › パスワードを忘れた場合のリンクがある @smoke-ui (73ms)
  7 passed (1.2s)

On the other hand, when I listed the tests with --list, the tags added with the tag option were not shown. Only @slow, which was written in the name, was displayed.

text
$ npx playwright test --list
Listing tests:
  [chromium] › cart.spec.ts:8:1 › 商品が2つ並ぶ
  [chromium] › cart.spec.ts:12:1 › 合計金額が450円になる
  [chromium] › cart.spec.ts:16:1 › クーポンの入力欄がある
  [chromium] › cart.spec.ts:20:1 › 商品を100個並べても購入ボタンが見える @slow
  [chromium] › login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある
  [chromium] › login.spec.ts:13:3 › ログイン › 空のまま押すとエラーが出る
  [chromium] › login.spec.ts:19:1 › パスワードを忘れた場合のリンクがある
Total: 7 tests in 2 files

Selecting tests by tag with –grep

When I passed @login to --grep (short form -g), only the two tests that had the tag set on describe ran.

text
$ npx playwright test --grep @login --reporter=list
Running 2 tests using 1 worker
  ✓  1 [chromium] › tests/login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある @login @smoke (88ms)
  ✓  2 [chromium] › tests/login.spec.ts:13:3 › ログイン › 空のまま押すとエラーが出る @login @regression (111ms)
  2 passed (669ms)

Conversely, to run tests while excluding a certain tag, use --grep-invert (short form -G). With --grep-invert @slow, the six tests other than the one whose name includes @slow ran.

text
$ npx playwright test --grep-invert @slow --reporter=list
Running 6 tests using 1 worker
  ...(6つのテストの行。@slow のテストはない)
  6 passed (1.1s)

--grep and --grep-invert can be used together. With --grep @regression --grep-invert @login, the two cart tests tagged @regression ran, and the two login tests were excluded.

text
$ npx playwright test --grep @regression --grep-invert @login --reporter=list
Running 2 tests using 1 worker
  ✓  1 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる @smoke @regression (110ms)
  ✓  2 [chromium] › tests/cart.spec.ts:16:1 › クーポンの入力欄がある @regression (69ms)
  2 passed (748ms)

Combining tags with "or" and "and"

The string passed to --grep is treated as a regular expression (official documentation on command-line options). So when combining multiple tags, you write them as regular expressions too.

To select tests that have either of several tags, join them with |. Since | is the pipe symbol in the shell, wrap the argument in quotes. In the following run, three @smoke tests and one @slow test ran. (The meaning of ( |$) is explained in the next section.)

text
$ npx playwright test --grep '@smoke( |$)|@slow' --reporter=list
Running 4 tests using 1 worker
  ✓  1 [chromium] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ @smoke (106ms)
  ✓  2 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる @smoke @regression (72ms)
  ✓  3 [chromium] › tests/cart.spec.ts:20:1 › 商品を100個並べても購入ボタンが見える @slow (72ms)
  ✓  4 [chromium] › tests/login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある @login @smoke (76ms)
  4 passed (957ms)

To select only tests that have both tags, use the lookahead form ((?=.*…)) described in the official documentation on tagging tests. Only one test has both @login and @regression, and only that test ran.

text
$ npx playwright test --grep '(?=.*@login)(?=.*@regression)' --reporter=list
Running 1 test using 1 worker
  ✓  1 [chromium] › tests/login.spec.ts:13:3 › ログイン › 空のまま押すとエラーが出る @login @regression (152ms)
  1 passed (689ms)

Selecting @smoke also includes @smoke-ui

When I ran --grep @smoke, the three @smoke tests ran, plus the test tagged @smoke-ui, for a total of four.

text
$ npx playwright test --grep @smoke --reporter=list
Running 4 tests using 1 worker
  ✓  1 [chromium] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ @smoke (100ms)
  ✓  2 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる @smoke @regression (66ms)
  ✓  3 [chromium] › tests/login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある @login @smoke (62ms)
  ✓  4 [chromium] › tests/login.spec.ts:19:1 › パスワードを忘れた場合のリンクがある @smoke-ui (69ms)
  4 passed (841ms)

As explained in the description of testConfig.grep, the --grep regular expression is not matched against each tag in the list one by one. It is matched against a single string that joins the project name, file name, describe names, test name, and tags with spaces. @smoke also matches the start of @smoke-ui in that string, so that test gets selected too.

If you want to match up to the end of a tag, write ( |$) after the tag to say that a space or the end of the string must follow. With --grep '@smoke( |$)', the @smoke-ui test was excluded, leaving three tests.

text
$ npx playwright test --grep '@smoke( |$)' --reporter=list
Running 3 tests using 1 worker
  ✓  1 [chromium] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ @smoke (89ms)
  ✓  2 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる @smoke @regression (71ms)
  ✓  3 [chromium] › tests/login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある @login @smoke (75ms)
  3 passed (785ms)

For the same reason, --grep also matches text other than tags. --grep cart selected the four tests whose file name is cart.spec.ts, and --grep ログイン selected the two tests whose describe name is ログイン (Login). Tags start with @, so when I specified a tag with the @ prefix, such as @login, it never matched a file name or describe name in these tests.

When no test matched (--grep @nothing), the output was Error: No tests found and the exit code was 1.

You can also filter by tag in the HTML report

In the HTML report generated with --reporter=html, each test name was shown with badges for the tags, displayed without the @ (smoke, regression, and so on), alongside a badge for the project name (chromium). Tags set with the tag option and the @slow written in the name both appeared as badges in the same way.

Playwright HTML report. Seven tests from cart.spec.ts and login.spec.ts are listed, and next to each test name are a chromium badge for the project and badges for the tags smoke, regression, login, slow, and smoke-ui
In the HTML report, tags are shown as badges next to the test name

When I typed @smoke into the search box at the top left of the report, the display changed to "Filtered: 3", and only the three @smoke tests remained. Unlike --grep @smoke, the @smoke-ui test was not kept.

HTML report with @smoke entered in the search box. It shows Filtered: 3, and only three tests remain: the two-products test, the total-450-yen test, and the Login test that checks the email address field is shown
Entering @smoke in the search box keeps only the three tests with the smoke badge

Splitting projects by tag in the config file

Instead of writing --grep on every command, you can also set grep and grepInvert in projects in the config file (testProject.grep description). In the following config, the smoke project runs only the @smoke tests, and the full project runs the tests other than @slow.

ts
// configs/projects.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: '../tests',
  workers: 1,
  use: { browserName: 'chromium' },
  projects: [
    { name: 'smoke', grep: /@smoke( |$)/ },
    { name: 'full', grepInvert: /@slow/ },
  ],
});

With --list, three tests fell into smoke and six into full. Adding --project smoke ran only the three tests.

text
$ npx playwright test -c configs/projects.config.ts --project smoke --reporter=list
Running 3 tests using 1 worker
  ✓  1 [smoke] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ @smoke (100ms)
  ✓  2 [smoke] › tests/cart.spec.ts:12:1 › 合計金額が450円になる @smoke @regression (74ms)
  ✓  3 [smoke] › tests/login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある @login @smoke (68ms)
  3 passed (707ms)

When I added --grep @login on top of this, only one test ran: the one that matched the project's grep and also matched @login. The --grep on the command line did not replace the project's grep; both filters applied.

text
$ npx playwright test -c configs/projects.config.ts --project smoke --grep @login --reporter=list
Running 1 test using 1 worker
  ✓  1 [smoke] › tests/login.spec.ts:9:3 › ログイン › メールアドレスの入力欄がある @login @smoke (91ms)
  1 passed (568ms)
Warning

When I wrote grepInvert: /@slow/ at the top level of the config file and ran --grep @slow, the output was Error: No tests found and the exit code was 1. Adding --grep-invert @nothing gave the same result, so the grepInvert in the config was not overridden by the command-line option.

Error when the @ is missing

When a string without @ was passed to the tag option, an error was raised while loading the tests, and no tests ran.

text
$ npx playwright test -c configs/bad.config.ts --reporter=list
Error: Tag must start with '@'
   at bad.spec.ts:3
  1 | import { test } from '@playwright/test';
  2 |
> 3 | test('タグに@を付け忘れた', { tag: 'smoke' }, async () => {});
    | ^
  4 |
    at /path/to/app/tests-bad/bad.spec.ts:3:1
Error: No tests found

I replaced the path in the output with a placeholder value (/path/to/app). The error shows the file and line where the tag was written. Write the string passed to tag starting with @, as in '@smoke' in cart.spec.ts above.

​Posted by 86