从netcoreapp3.1升级到net6.0后单元测试无法运行
问题描述
我有一个包含两个项目的解决方案:
- 主项目:基于netcoreapp3.1的ASP应用,已升级至net6.0
- 单元测试项目:同样从netcoreapp3.1升级至net6.0
升级后执行清理重建,构建无报错且显示成功,但出现以下问题:
- 右键单元测试项目时,“运行测试”选项呈灰色
- 在Test Explorer中运行测试,卡顿一段时间后提示未运行任何测试
错误输出信息
Aborting test discovery because a test run was requested. Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Failed to negotiate protocol, waiting for response timed out after 90 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout. at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TestRequestSender.CheckVersionWithTestHost() at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler) ========== Test discovery aborted: 0 Tests found in 1.5 min ========== Building Test Projects Starting test discovery for requested test run ========== Starting test discovery ========== Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Failed to negotiate protocol, waiting for response timed out after 90 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout. at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TestRequestSender.CheckVersionWithTestHost() at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler) Executing all tests in project: Products.Dashboard.UnitTests ========== Test discovery aborted: 0 Tests found in 1.5 min ========== ========== Starting test run ========== ========== Test run finished: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ========== Resuming previously aborted test discovery ========== Starting test discovery ==========
单元测试项目文件内容
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <ProjectGuid>{af563d15-3097-4430-89e5-6e80e6152dfa}</ProjectGuid> <IsPackable>false</IsPackable> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PlatformTarget>x64</PlatformTarget> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.CodeCoverage" Version="16.10.0" /> <PackageReference Include="Moq" Version="4.18.3" /> <PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0" /> <PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> <ItemGroup> <ProjectReference Include="..\..\src\MainProject.csproj" /> </ItemGroup> </Project>
解决方法
尝试以下步骤逐一排查:
1. 更新测试相关NuGet包
当前xunit主包与runner版本不匹配(2.4.2 vs 2.4.1),且版本偏旧。更新到适配.NET 6的最新稳定版,确保主包和runner版本一致:
- 将
xunit版本更新至2.5.3(或最新稳定版) - 将
xunit.runner.visualstudio版本更新至2.5.3 - 同步更新
Microsoft.CodeCoverage到适配.NET 6的版本(如17.7.2)
更新后的PackageReference示例:
<PackageReference Include="Microsoft.CodeCoverage" Version="17.7.2" /> <PackageReference Include="xunit" Version="2.5.3" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference>
2. 统一平台目标
确保解决方案内所有项目平台目标一致:
- 检查主项目的
PlatformTarget设置,确保和测试项目同为x64 - 打开生成 > 配置管理器,将所有项目的平台统一设置为x64,避免AnyCPU与x64混合导致的兼容性问题
3. 清理测试缓存
- 删除解决方案目录下的
.vs、bin、obj文件夹 - 执行清理解决方案,再重新生成解决方案
- 重启Visual Studio后尝试运行测试
4. 延长测试连接超时
按照错误提示设置环境变量延长超时:
- 右键单元测试项目 > 属性 > 调试 > 环境变量,添加
VSTEST_CONNECTION_TIMEOUT=180(单位:秒) - 或在系统环境变量中添加该配置,重启Visual Studio生效
5. 验证测试项目配置
- 确认项目文件顶部SDK设置为
<Project Sdk="Microsoft.NET.Sdk"> - 删除项目文件中残留的netcoreapp3.1相关配置项
内容的提问来源于stack exchange,提问作者Andy
相关产品推荐
相关产品推荐

