You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DotNetCoreCLI测试执行耗时过长的原因及解决方法咨询

解决Azure DevOps单元测试执行耗时过长/命令行卡住问题

问题场景

Azure DevOps中使用DotNetCoreCLI@2任务执行单元测试时出现卡住/耗时过长的情况,当前YAML配置如下:

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '$(solutionUnitTest)'
    testRunTitle: 'Unit Tests'

优化方案

并行执行测试

在任务中添加--parallel参数,利用多核CPU并行运行测试,显著缩短执行时间。修改后的配置:

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '$(solutionUnitTest)'
    testRunTitle: 'Unit Tests'
    arguments: '--configuration $(buildConfiguration) --parallel'

拆分测试任务

若$(solutionUnitTest)包含多个测试项目,拆分任务分别执行,避免单个任务负载过高:

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/ProjectA.UnitTests.csproj'
    testRunTitle: 'ProjectA Unit Tests'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/ProjectB.UnitTests.csproj'
    testRunTitle: 'ProjectB Unit Tests'
    arguments: '--configuration $(buildConfiguration)'

禁用不必要的代码覆盖率

若未需要代码覆盖率数据,移除覆盖率收集参数,减少额外计算开销。若保留覆盖率,指定临时目录避免IO瓶颈:

# 不需要覆盖率的配置
arguments: '--configuration $(buildConfiguration) --no-build'

# 需要覆盖率的优化配置
arguments: '--configuration $(buildConfiguration) --no-build --collect "XPlat Code Coverage" --results-directory $(Agent.TempDirectory)'

定位卡住的测试用例

添加--verbosity detailed参数输出详细日志,定位到具体卡住的测试用例,针对性优化(如移除外部依赖、拆分大测试、模拟数据):

arguments: '--configuration $(buildConfiguration) --verbosity detailed'

提升Agent资源配置

切换至更高规格的托管Agent(如Windows/Linux Large),或使用自托管Agent提供更强的硬件性能,解决资源不足导致的卡顿。

内容的提问来源于stack exchange,提问作者agmcoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 05:25:21