跳到主要內容

命令列

簡介

以下是命令列中最常用的選項。

  • 執行所有測試

    npx playwright test
  • 執行單個測試檔案

    npx playwright test tests/todo-page.spec.ts
  • 執行一組測試檔案

    npx playwright test tests/todo-page/ tests/landing-page/
  • 執行檔名中包含 my-specmy-spec-2 的檔案

    npx playwright test my-spec my-spec-2
  • 執行 my-spec.ts 第 42 行的測試

    npx playwright test my-spec.ts:42
  • 執行具有標題的測試

    npx playwright test -g "add a todo item"
  • 在有 head 瀏覽器中執行測試

    npx playwright test --headed
  • 針對特定專案執行所有測試

    npx playwright test --project=chromium
  • 停用平行化

    npx playwright test --workers=1
  • 選擇報告器

    npx playwright test --reporter=dot
  • 使用 Playwright Inspector 在偵錯模式下執行

    npx playwright test --debug
  • 在互動式 UI 模式下執行測試,並具有內建的 watch 模式 (預覽)

    npx playwright test --ui
  • 尋求協助

    npx playwright test --help

參考

完整的 Playwright Test 選項集可在設定檔中找到。以下選項可以傳遞到命令列,並優先於設定檔

選項描述
非選項參數每個參數都被視為與完整測試檔案路徑匹配的正規表示式。只有來自符合模式的檔案的測試才會被執行。特殊符號如 $* 應該使用 \ 進行跳脫。在許多 shell/終端機中,您可能需要引用參數。
-c <file>--config <file>設定檔,或包含可選 "playwright.config.{m,c}?{js,ts}" 的測試目錄。預設為目前目錄中的 playwright.config.tsplaywright.config.js
--debug使用 Playwright Inspector 執行測試。是 PWDEBUG=1 環境變數和 --timeout=0 --max-failures=1 --headed --workers=1 選項的快捷方式。
--fail-on-flaky-tests如果任何測試被標記為不穩定則失敗 (預設值:false)。
--forbid-only如果呼叫了 test.only 則失敗 (預設值:false)。在 CI 上很有用。
--fully-parallel並行執行所有測試 (預設值:false)。
--global-timeout <timeout>此測試套件可以運行的最長時間,以毫秒為單位 (預設值:無限制)。
-g <grep>--grep <grep>僅執行與此正規表示式匹配的測試 (預設值:".*")。
-gv <grep>--grep-invert <grep>僅執行與此正規表示式不匹配的測試。
--headed在有 head 瀏覽器中執行測試 (預設值:headless)。
--ignore-snapshots忽略螢幕截圖和快照期望。
--last-failed僅重新執行失敗的測試。
--list收集所有測試並報告它們,但不執行。
--max-failures <N>-x在前 N 次失敗後停止。傳遞 -x 會在第一次失敗後停止。
--no-deps不執行專案依賴項。
--output <dir>輸出artifacts的資料夾 (預設值:"test-results")。
--only-changed [ref]僅執行在 'HEAD' 和 'ref' 之間已變更的測試檔案。預設為執行所有未提交的變更。僅支援 Git。
--pass-with-no-tests即使沒有找到測試,也使測試運行成功。
--project <project-name...>僅從指定的專案清單中執行測試,支援 '*' 萬用字元 (預設值:執行所有專案)。
--quiet抑制 stdio。
--repeat-each <N>將每個測試執行 N 次 (預設值:1)。
--reporter <reporter>要使用的報告器,以逗號分隔,可以是 "dot"、"line"、"list" 或其他 (預設值:"list")。您也可以傳遞自訂報告器檔案的路徑。
--retries <retries>不穩定測試的最大重試次數,零表示不重試 (預設值:不重試)。
--shard <shard>分片測試並僅執行選定的分片,以 "current/all" 的形式指定,從 1 開始,例如 "3/5"。
--timeout <timeout>以毫秒為單位指定測試逾時閾值,零表示無限制 (預設值:30 秒)。
--trace <mode>強制追蹤模式,可以是 "on"、"off"、"on-first-retry"、"on-all-retries"、"retain-on-failure"、"retain-on-first-failure"。
--tsconfig <path>適用於所有匯入檔案的單個 tsconfig 的路徑 (預設值:為每個匯入的檔案單獨查找 tsconfig)。
--ui在互動式 UI 模式下執行測試。
--ui-host <host>用於在 UI 上提供的 Host;指定此選項會在瀏覽器標籤頁中開啟 UI。
--ui-port <port>用於在 UI 上提供的 Port,0 表示任何可用 port;指定此選項會在瀏覽器標籤頁中開啟 UI。
-u--update-snapshots [mode]使用實際結果更新快照。可能的值為 "all"、"changed"、"missing" 和 "none"。不傳遞預設為 "missing";不帶值傳遞預設為 "changed"。
-j <workers>--workers <workers>並行 worker 的數量或邏輯 CPU 核心的百分比,使用 1 在單個 worker 中執行 (預設值:50%)。
-x在第一次失敗後停止。