如何在Azure Pipeline的Playwright YAML文件中按指定顺序运行测试
在Azure Pipeline中按指定顺序执行Playwright测试
要解决Playwright测试无序执行的问题,核心是使用Playwright官方测试命令指定执行顺序——你之前误用了jest命令(Playwright默认不依赖jest运行),导致无法按预期执行。
修正方案:指定测试文件顺序执行
你可以在一个脚本步骤中按顺序列出需要执行的测试文件,Playwright会严格按照给出的顺序运行:
替换原有YAML中的Run Playwright tests步骤,改为:
- script: npx playwright test ./Digor.Web/playwright-tests/createTraveler.spec.ts ./Digor.Web/playwright-tests/createSingleTrip.spec.ts displayName: 'Run Playwright tests in order'
如果需要分开执行两个测试(比如单独查看每个测试的执行日志),也可以拆分为两个独立步骤,但要确保每个步骤的测试结果能被后续任务正确合并:
- script: npx playwright test ./Digor.Web/playwright-tests/createTraveler.spec.ts --reporter=junit displayName: 'Run createTraveler.spec.ts' - script: npx playwright test ./Digor.Web/playwright-tests/createSingleTrip.spec.ts --reporter=junit displayName: 'Run createSingleTrip.spec.ts'
注意:拆分步骤时,需确保Playwright配置中JUnit报告的输出路径支持追加,或修改每个步骤的报告输出文件名,避免覆盖。
完整修正后的YAML配置
trigger: - main pool: vmImage: ubuntu-latest steps: - task: NodeTool@0 inputs: versionSpec: '18' displayName: 'Install Node.js' - script: npm ci displayName: 'npm ci' - script: npx playwright install --with-deps displayName: 'Install Playwright browsers' # 替换为按顺序执行指定测试的步骤 - script: npx playwright test ./Digor.Web/playwright-tests/createTraveler.spec.ts ./Digor.Web/playwright-tests/createSingleTrip.spec.ts displayName: 'Run Playwright tests in order' - task: PublishTestResults@2 displayName: 'Publish test results' inputs: searchFolder: 'test-results' testResultsFormat: 'JUnit' testResultsFiles: 'e2e-junit-results.xml' mergeTestResults: true failTaskOnFailedTests: true testRunTitle: 'My End-To-End Tests' condition: succeededOrFailed() - task: PublishPipelineArtifact@1 inputs: targetPath: playwright-report artifact: playwright-report publishLocation: 'pipeline' condition: succeededOrFailed()
关键说明
- 为什么
jest命令无效?:Playwright默认使用自身的测试运行器,而非jest。除非手动配置了jest作为Playwright的测试运行器,否则jest命令无法识别Playwright的测试文件和API。 - 路径正确性:确保测试文件的路径与仓库实际结构一致,路径错误会导致测试无法被找到。
内容的提问来源于stack exchange,提问作者Oscar Keirouz
相关产品推荐
相关产品推荐

