自建.NET Pipeline运行缓慢(测试通过后挂起)及测试结果无法显示仪表盘问题求助
Hey there! Let's tackle your two pipeline issues step by step—first the mysterious 12-minute hang after tests pass, then getting your test results to show up in the dashboard.
From your description, the core business logic only takes ~4 minutes, but the pipeline stalls for 12 minutes post-tests. Let's break down potential culprits in your YAML and environment:
Possible Causes & Fixes
Lingering processes on self-hosted agent
Since you're using a self-hosted pool, leftover processes (likedotnetor test runners) often stick around after the test task finishes, forcing the pipeline to wait for them to exit. Log into your agent machine, check for running processes tied to your test project, and terminate them manually to see if the hang time drops. You can also add a post-test cleanup step to kill these processes explicitly.Inefficient test task configuration
Your originalDotNetCoreCLI@2test task hascontinueOnError: true, but if the test runner doesn't exit cleanly, it can block the pipeline. Try modifying the task to skip rebuilding (since you already ran a full build earlier) with the--no-buildflag:- task: DotNetCoreCLI@2 displayName: Run tests continueOnError: true inputs: command: test projects: '$(TestProjects)' publishRunAttachments: false arguments: '--configuration $(buildConfiguration) --verbosity normal --no-build'Overly broad build scope
Yourdotnet buildtask targets**/*.csproj, which includes test projects. Building test projects is unnecessary (the test task can handle it, or you can skip building them entirely with--no-build). Narrow down the build to exclude test projects:- task: DotNetCoreCLI@2 displayName: 'dotnet build' inputs: command: 'build' projects: | **/*.csproj !**/*Test*.csproj # Exclude test projects configuration: $(buildConfiguration) arguments: '--configuration $(buildConfiguration)'Agent post-job cleanup delays
Self-hosted agents often run cleanup tasks after a pipeline finishes (like deleting the working directory). If your workspace is large, this can take minutes. Check your agent settings to disable automatic cleanup (or adjust retention policies) to speed things up.
script: dotnet test When you run dotnet test via a script task, Azure DevOps doesn't automatically pick up the test results—you need to explicitly publish them using the PublishTestResults@2 task. Here's how to fix it:
Add this task right after your script test step:
- script: dotnet test --logger trx --framework netcoreapp3.1 --configuration $(buildConfiguration) displayName: Run tests via script - task: PublishTestResults@2 displayName: Publish test results to dashboard inputs: testResultsFormat: 'VSTest' # Matches the trx logger format testResultsFiles: '**/*.trx' # Find all trx files in the workspace failTaskOnFailedTests: false # Optional: Set to true if you want the pipeline to fail on test failures
This will upload the TRX test results file to Azure DevOps, and they'll appear in the pipeline dashboard under the "Tests" tab.
内容的提问来源于stack exchange,提问作者Trombone Man

