Azure Pipeline执行Playwright测试报错:未找到测试用例(本地正常)
本地VS Code运行Playwright测试完全正常,但在Azure Pipeline中执行时出现“No tests found”错误。测试用例存放在tests文件夹,Playwright配置里testDir已设置为'./tests'。
相关YAML配置
trigger: - none variables: - group: 'Common' - group: 'Optimizely' pool: name: VMSS-Linux-Pool resources: repositories: - repository: PlaywrightFramework type: git name: PlaywrightFramework jobs: - job: Build timeoutInMinutes: 0 steps: - checkout: self displayName: 'Checkout SAGov' - checkout: PlaywrightFramework - script: | mv MavenSyso/* . rm -rf MavenSyso ls -l displayName: 'Prepare folder structure' - task: NodeTool@0 inputs: versionSpec: '18.x' displayName: 'Install Node.js' - script: | npm install sudo npx playwright install-deps npx playwright install npm install -g allure-commandline --save-dev displayName: 'Install software' - script: | sudo apt-get update && sudo apt-get install -y jq buildID=$(Build.BuildId) jq --arg buildID "$buildID" --arg emailPass "$(EmailPassword)" '.buildID = $buildID | .executionENV = "pipeline" | .emailPassword = $emailPass' automation.config.json > temp.json && mv temp.json automation.config.json displayName: 'Update Automation config' - script: | export CI=true npx playwright test --project chrome env: SALESFORCE_UAT_BASEURL: $(SALESFORCE_UAT_BASEURL) SALESFORCE_API_USERNAME: $(SALESFORCE_API_USERNAME) SALESFORCE_API_PASSWORD: $(SALESFORCE_API_PASSWORD) SALESFORCE_API_CLIENTID: $(SALESFORCE_API_CLIENTSECRET) OPTIMIZELY_USERNAME: $(Optimizely_Username) OPTIMIZELY_PASSWORD: $(Optimizely_Password) displayName: 'Run Playwright Tests' - task: AzureCLI@2 displayName: 'Download Allure History from Blob Storage' condition: succeededOrFailed() inputs: azureSubscription: 'Execution' scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | az storage blob download-batch --account-name $(StorageAccountName) --source 'reports' --pattern 'Maven_Regression/AutomationReport/history/*.*' --destination '$(Pipeline.Workspace)/s/allure-results' - script: | mv "$(Pipeline.Workspace)/s/allure-results/Maven/AutomationReport/history" "$(Pipeline.Workspace)/s/allure-results" rm -rf "$(Pipeline.Workspace)/s/allure-results/MavenSyso_Regression" condition: succeededOrFailed() displayName: 'Move Allure History Folder and Delete MavenSyso_Regression' - script: | allure generate allure-results --clean -o allure-report/AutomationReport condition: succeededOrFailed() displayName: 'Generate Allure Report' - task: PublishPipelineArtifact@1 displayName: 'Attach Allure Report' condition: succeededOrFailed() inputs: targetPath: '$(Pipeline.Workspace)/s/allure-report' artifactName: 'Allure Report' - task: PublishPipelineArtifact@1 displayName: 'Attach Allure Results' condition: succeededOrFailed() inputs: targetPath: '$(Pipeline.Workspace)/s/allure-results' artifactName: 'allure-results' - task: AzureCLI@2 displayName: 'Upload Allure reports' condition: succeededOrFailed() inputs: azureSubscription: 'Maven-NonProd' scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | az storage blob delete-batch --account-name "$(StorageAccountName)" --source 'reports' --pattern 'Maven_Regression/*' az storage blob upload-batch --account-name $(StorageAccountName) -d reports/Maven -s "$(Pipeline.Workspace)/s/allure-report"
错误日志
Generating script.
Script contents: shell
npx playwright test --project=chrome --grep='@DBVerification'
========================== Starting Command Output ===========================
"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "C:\a_temp\705d70ac-de66-419f-9475-f2622d15d2cc.cmd""
globalVariables.startTime2024-07-24 07:53:56
Error: No tests foundTeardown finished
##[error]Cmd.exe exited with code '1'.
Finishing: Run Playwright Tests
排查与解决方法
1. 验证工作目录与文件结构
在执行Playwright测试的步骤前,添加打印目录结构的脚本,确认tests文件夹是否存在于当前工作目录:
script: | pwd ls -R displayName: 'Check current directory and file structure'
注意:双仓库checkout后,代码默认存放在$(Pipeline.Workspace)/s/仓库名子目录下,需确认Prepare folder structure步骤是否将tests文件夹移动到了正确位置。
2. 显式指定测试目录
即使配置了testDir,流水线中可能因工作目录变化导致路径失效,执行测试时直接指定测试目录:
npx playwright test ./tests --project chrome
3. 检查测试文件命名规范
Playwright默认识别以下命名的测试文件:
*.test.ts/*.test.js*.spec.ts/*.spec.js
确认测试文件符合规则,且未被.gitignore排除导致未被checkout到流水线。
4. 核对grep标签匹配
错误日志中的执行命令带有--grep='@DBVerification',但YAML配置中未包含该参数,需确认:
- 测试用例中是否存在带
@DBVerification标签的测试 - 若不需要过滤,移除该参数;若需要,确保标签拼写正确
5. 指定Playwright配置文件路径
确保配置文件在当前工作目录,或执行测试时显式指定:
npx playwright test --project chrome --config ./playwright.config.ts
6. 切换到正确的工作目录
双仓库checkout后,需切换到主仓库目录执行测试,修改测试步骤:
script: | cd $(Pipeline.Workspace)/s/SAGov export CI=true npx playwright test --project chrome
内容的提问来源于stack exchange,提问作者Ketan Pardeshi

