跳至主要内容

命令行

简介

以下是命令行中最常用的选项。

  • 运行所有测试

    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"
  • 在带界面的浏览器中运行测试

    npx playwright test --headed
  • 针对特定项目运行所有测试

    npx playwright test --project=chromium
  • 禁用并行化

    npx playwright test --workers=1
  • 选择一个 reporter

    npx playwright test --reporter=dot
  • 在调试模式下运行 Playwright Inspector

    npx playwright test --debug
  • 在交互式UI模式下运行测试,内置观察模式(预览)

    npx playwright test --ui
  • 请求帮助

    npx playwright test --help

参考

Playwright Test的完整选项集可在配置文件中找到。以下选项可以通过命令行传递,并优先于配置文件:

OptionDescription
Non-option argumentsEach argument is treated as a regular expression matched against the full test file path. Only tests from files matching the pattern will be executed. Special symbols like $ or * should be escaped with \. In many shells/terminals you may need to quote the arguments.
-c <file> or --config <file>Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}". Defaults to playwright.config.ts or playwright.config.js in the current directory.
--debugRun tests with Playwright Inspector. Shortcut for PWDEBUG=1 environment variable and --timeout=0 --max-failures=1 --headed --workers=1 options.
--fail-on-flaky-testsFail if any test is flagged as flaky (default: false).
--forbid-onlyFail if test.only is called (default: false). Useful on CI.
--fully-parallelRun all tests in parallel (default: false).
--global-timeout <timeout>Maximum time this test suite can run in milliseconds (default: unlimited).
-g <grep> or --grep <grep>Only run tests matching this regular expression (default: ".*").
-gv <grep> or --grep-invert <grep>Only run tests that do not match this regular expression.
--headedRun tests in headed browsers (default: headless).
--ignore-snapshotsIgnore screenshot and snapshot expectations.
--last-failedOnly re-run the failures.
--listCollect all the tests and report them, but do not run.
--max-failures <N> or -xStop after the first N failures. Passing -x stops after the first failure.
--no-depsDo not run project dependencies.
--output <dir>Folder for output artifacts (default: "test-results").
--only-changed [ref]Only run test files that have been changed between 'HEAD' and 'ref'. Defaults to running all uncommitted changes. Only supports Git.
--pass-with-no-testsMakes test run succeed even if no tests were found.
--project <project-name...>Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects).
--quietSuppress stdio.
--repeat-each <N>Run each test N times (default: 1).
--reporter <reporter>Reporter to use, comma-separated, can be "dot", "line", "list", or others (default: "list"). You can also pass a path to a custom reporter file.
--retries <retries>Maximum retry count for flaky tests, zero for no retries (default: no retries).
--shard <shard>Shard tests and execute only the selected shard, specified in the form "current/all", 1-based, e.g., "3/5".
--timeout <timeout>Specify test timeout threshold in milliseconds, zero for unlimited (default: 30 seconds).
--trace <mode>Force tracing mode, can be "on", "off", "on-first-retry", "on-all-retries", "retain-on-failure", "retain-on-first-failure".
--tsconfig <path>Path to a single tsconfig applicable to all imported files (default: look up tsconfig for each imported file separately).
--uiRun tests in interactive UI mode.
--ui-host <host>Host to serve UI on; specifying this option opens UI in a browser tab.
--ui-port <port>Port to serve UI on, 0 for any free port; specifying this option opens UI in a browser tab.
-u or --update-snapshots [mode]Update snapshots with actual results. Possible values are "all", "changed", "missing", and "none". Running tests without the flag defaults to "missing"; running tests with the flag but without a value defaults to "changed".
--update-source-method [mode]Update snapshots with actual results. Possible values are "patch" (default), "3way" and "overwrite". "Patch" creates a unified diff file that can be used to update the source code later. "3way" generates merge conflict markers in source code. "Overwrite" overwrites the source code with the new snapshot values.
-j <workers> or --workers <workers>Number of concurrent workers or percentage of logical CPU cores, use 1 to run in a single worker (default: 50%).
-xStop after the first failure.