我在 Azure Devops 中创建了一个简单的管道来运行 Playwright 测试,并显式编码了 2 个测试之一以失败。当这种情况在管道执行期间发生时,管道似乎挂起,我猜测最终会超时(30 分钟后,我取消了)。但是,当我执行成功的测试时,管道会成功退出。下面是我的 yaml 管道。有谁知道为什么会发生这种情况 - 我在这里错过了什么吗?
[编辑]:我将下面的行添加到我的 playwright.config.ts 文件中,任务现在像我预期的那样失败了。默认的 Playwright 配置会打开有关任何失败的 html 报告。您可以在 Playwright 中使用很多记者,所以看看什么适合您 (https://playwright.dev/docs/test-reporters)。
记者:[ ['html', { open: '从不' }] ],
trigger:
- master
- test
- dev
pool:
vmImage: macos-latest
jobs:
- job: Build
displayName: Build stage
steps:
- task: NodeTool@0
inputs:
versionSpec: '16.x'
displayName: 'Install Node.js'
- script: |
npm ci
displayName: 'Install dependencies'
- script: |
npx playwright install --with-deps
displayName: 'Install Playwright'
- script: |
npm run test-unit-smoke
displayName: 'Run unit tests'
原因是您运行测试时虚拟服务器中的“CI”环境变量不正确。如果 CI 目前确实为真,您将永远不会在管道日志中看到“Serving HTML report..”文本。
我在网上读到 CI 提供商默认情况下应该将 CI 设置为 true,但至少在 Azure Pipelines 中我总是必须手动将值设置为 true。
我还没有尝试过 macOS,但对于 Windows,我必须在 YML 文件中运行测试之前添加“set CI=true”。
使用 Windows VM 的 Azure Pipelines 示例:
- task: CmdLine@2
displayName: 'Run all tests with Chrome and Firefox'
inputs:
script: 'set CI=true && npx playwright test ./tests/test.spec.ts --project=chromium --project=firefox'
对于 macOS,您可以在测试前尝试“export CI=true”。
2种方式:
reporter: [['html', { outputFolder: 'my-report', open: 'never' }]],
- script: |
npx playwright test
displayName: 'Run Playwright tests'
continueOnError: true
env:
CI: true
根据 Playwright 文档 和 Playwright 开发人员在 Playwright repo 上的回答,您应该配置一个环境变量
CI
,其值为 true
或 1
- script: |
npm run test-unit-smoke
displayName: 'Run unit tests'
env:
CI: 'true'
请注意以上答案是首选,因为
效果 更多配置,例如CI: 'true'
、workers
和forbidOnly
。retries
或者,您可以在
reporter
中设置 playwright.config
属性,如下所示:
reporter: [ ['html', { open: 'never' }] ],