迁移至.NET Core 7后Visual Studio中MSTest测试发现失败:TestRunDirectoryLabel缺失
问题:.NET Core 7迁移后MSTest无法发现单元测试(机器特定问题)
问题背景
近期将C#项目从.NET Framework 4.8迁移至.NET Core 7后,MSTest无法发现任何单元测试。切换回旧框架分支时测试显示正常,且几周前新分支还能正常运行测试,代码库未做相关变更。其他开发者可正常运行测试,因此判定为机器特定问题。
错误信息
测试发现阶段的报错如下:
Building Test Projects Starting test discovery for requested test run ========== Starting test discovery ========== [MSTest][Discovery][C:\sln\project\bin\Debug\project.dll] Failed to discover tests from assembly C:\sln\project\bin\Debug\project.dll. Reason:Field not found: 'Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestRunDirectoryLabel'. No test is available in C:\sln\project\bin\Debug\project.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. ========== Test discovery finished: 0 Tests found in 993.5 ms ==========
相关.runsettings文件
项目使用的.runsettings配置如下:
<?xml version="1.0" encoding="utf-8"?> <RunSettings> <!-- Configurations that affect the Test Framework --> <RunConfiguration> <!-- Path relative to solution directory --> <ResultsDirectory>\.\TestResults</ResultsDirectory> <!-- [x86] | x64 - You can also change it from menu Test, Test Settings, Default Processor Architecture --> <TargetPlatform>x64</TargetPlatform> <!-- Framework35 | [Framework40] | Framework45 --> <!--<TargetFrameworkVersion>Framework40</TargetFrameworkVersion>--> </RunConfiguration> <!-- Configurations for data collectors --> <DataCollectionRunSettings> <DataCollectors> <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken="[redacted]"> <Configuration> <CodeCoverage> <ModulePaths> <Exclude> <ModulePath>.*CPPUnitTestFramework.*</ModulePath> </Exclude> </ModulePaths> </CodeCoverage> </Configuration> </DataCollector> </DataCollectors> </DataCollectionRunSettings> <!-- Adapter Specific sections --> <!-- MSTest adapter --> <MSTest> <!-- required for app.config (which has gcverylargeobjects) to run --> <!--<ForcedLegacyMode>true</ForcedLegacyMode>--> <MapInconclusiveToFailed>True</MapInconclusiveToFailed> <CaptureTraceOutput>false</CaptureTraceOutput> <DeleteDeploymentDirectoryAfterTestRunIsComplete>false</DeleteDeploymentDirectoryAfterTestRunIsComplete> <DeploymentEnabled>true</DeploymentEnabled> </MSTest> </RunSettings>
已尝试的解决方式
- 删除
.vs、bin、obj目录后重新构建项目 - 安装所有MSTest相关的NuGet包
- 修改
.runsettings文件的各项配置 - 更新.NET Core 7和.NET Framework 4.8到最新版本
- 使用
dotnet test命令通过CLI运行测试
以上方法均未解决问题,请问该如何修复?
内容的提问来源于stack exchange,提问作者Brambo
相关产品推荐
相关产品推荐

