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

指定csproj运行dotnet test失败求助(本地Azure DevOps Server)

问题描述

我们部署了本地Azure DevOps Server,自上次微软补丁更新后出现以下异常:

  • 通过Azure DevOps的DotNetCoreCLI@2任务执行dotnet test命令失败
  • 直接在服务器上指定csproj文件运行dotnet test同样失败
  • 但在同一文件夹下不指定csproj文件运行dotnet test则测试成功
  • 通过Visual Studio工具命令行运行该命令可正常执行

相关YAML配置:

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet test '
  inputs:
    command: test
    projects: |
     **/*Test*.csproj
    arguments: '-c  Release'
    testRunTitle: 'Test'
    enabled: false
    continueOnError: true
可能的解决方案

1. 排查.NET SDK版本冲突

微软补丁可能自动更新了服务器上的.NET SDK版本,导致指定csproj运行时使用的SDK与项目目标框架不兼容:

  • 执行dotnet --list-sdks查看服务器已安装的SDK版本
  • 在项目根目录的Directory.Build.props中明确指定SDK版本:
    <Project>
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework> <!-- 替换为你的项目目标框架 -->
        <RollForward>LatestMinor</RollForward>
      </PropertyGroup>
    </Project>
    

2. 调整Azure DevOps任务配置

修改YAML参数,尝试指定工作目录或优化项目匹配规则:

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet test'
  inputs:
    command: test
    projects: '**/*Test*.csproj'
    arguments: '--configuration Release --no-restore'
    testRunTitle: 'Test'
    workingDirectory: '$(Build.SourcesDirectory)/你的测试项目根目录'

3. 清理缓存与构建输出

补丁更新可能导致缓存损坏,执行以下操作:

  • 在服务器上运行dotnet nuget locals all --clear清理NuGet缓存
  • 在Azure DevOps任务中添加前置清理步骤:
  • task: CmdLine@2
    displayName: 'Clean build artifacts'
    inputs:
    script: |
    dotnet clean --configuration Release
    rm -rf $(Build.ArtifactStagingDirectory)/*
## 4. 对齐VS与服务器的.NET环境
Visual Studio命令行能正常运行,说明VS使用的.NET环境与服务器默认环境存在差异:
- 在Azure DevOps任务中指定与VS一致的SDK版本:
  ```yaml
- task: UseDotNet@2
  displayName: 'Use .NET SDK 6.x'
  inputs:
    packageType: 'sdk'
    version: '6.x' # 替换为VS使用的具体SDK版本
    installationPath: $(Agent.ToolsDirectory)/dotnet

内容的提问来源于stack exchange,提问作者D.Sanchez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 17:05:18