命令行
简介
以下是命令行中最常用的选项。
-
运行所有测试
npx playwright test
-
运行单个测试文件
npx playwright test tests/todo-page.spec.ts
-
运行一组测试文件
npx playwright test tests/todo-page/ tests/landing-page/
-
运行文件名中包含
my-spec
或my-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
-
选择一个报告器
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.ts 或 playwright.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 | 在有头浏览器中运行测试 (默认值: 无头)。 |
--ignore-snapshots | 忽略屏幕截图和快照期望。 |
--last-failed | 仅重新运行失败的测试。 |
--list | 收集所有测试并报告它们,但不运行。 |
--max-failures <N> 或 -x | 在前 N 次失败后停止。传递 -x 会在第一次失败后停止。 |
--no-deps | 不运行项目依赖项。 |
--output <dir> | 输出工件的文件夹 (默认值: "test-results")。 |
--only-changed [ref] | 仅运行在 'HEAD' 和 'ref' 之间已更改的测试文件。默认为运行所有未提交的更改。仅支持 Git。 |
--pass-with-no-tests | 即使未找到任何测试,也使测试运行成功。 |
--project <project-name...> | 仅从指定的项目列表运行测试,支持 '*' 通配符 (默认值: 运行所有项目)。 |
--quiet | 抑制标准输出。 |
--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 服务的端口,0 表示任何空闲端口;指定此选项将在浏览器选项卡中打开 UI。 |
-u 或 --update-snapshots [mode] | 使用实际结果更新快照。可能的值为 "all"、"changed"、"missing" 和 "none"。不传递默认为 "missing";不带值传递默认为 "changed"。 |
-j <workers> 或 --workers <workers> | 并发工作进程数或逻辑 CPU 核心的百分比,使用 1 在单个工作进程中运行 (默认值: 50%)。 |
-x | 在第一次失败后停止。 |