在Azure DevOps运行单元测试时提示找不到MyProject.UnitTests.dll
解决CI环境下.NET单元测试找不到测试DLL的问题
错误日志
The test source file "D:\a\_work\3\s\MyProject.UnitTests\bin\Release\net6.0\MyProject.UnitTests.dll" provided was not found. ##[error]Error: The process 'C:\hostedtoolcache\windows\dotnet\dotnet.exe' failed with exit code 1 ##[warning]No test result files were found. ##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build
相关YAML配置
steps: - task: DotNetCoreCLI@2 displayName: "Run unit tests" inputs: command: test projects: "**/*MyProject.UnitTests/*.csproj" arguments: --configuration $(projectBuildConfiguration) --no-build --no-restore feedsToUse: 'config' nugetConfigPath: 'nuget.config'
问题背景
本地机器以Release配置运行测试完全正常,但CI环境中提示找不到测试DLL文件。
解决方案
检查构建任务是否前置执行
测试任务使用了--no-build参数,要求测试前必须完成对应配置的构建。确认CI流水线中,测试任务之前是否执行了dotnet build任务,且构建配置与$(projectBuildConfiguration)一致(即Release)。若缺少构建步骤,添加如下任务到测试任务之前:- task: DotNetCoreCLI@2 displayName: "Build test project" inputs: command: build projects: "**/*MyProject.UnitTests/*.csproj" arguments: --configuration $(projectBuildConfiguration) --no-restore验证项目路径匹配正确性
CI环境目录结构可能与本地不同,可在测试任务前添加文件列表任务,确认路径是否能匹配到测试项目:- task: CmdLine@2 displayName: "List test project files" inputs: script: | dir /s /b "*MyProject.UnitTests*.csproj"移除不必要参数(可选)
若CI流水线未提前构建,可暂时移除--no-build参数,让dotnet test自动完成构建,但会增加执行时间。处理NuGet兼容性警告
针对日志中的NuGet兼容性提示,确保CI使用的NuGet版本≥5.8,或改用dotnet restore命令替代传统NuGet任务进行包还原。
内容的提问来源于stack exchange,提问作者MChak
相关产品推荐
相关产品推荐

