我试图使用C:\Program (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe.运行测试用例
但一些测试在异常System.IO.FileLoadException中失败:无法加载文件或程序集'Newtonsoft.Json、Version=11.0.0.0、Culture=neutral、PublicKeyToken=30ad4fe6b2a6aeed‘或其依赖项之一。所定位的程序集的清单定义与程序集引用不匹配。( HRESULT: 0x80131040除外)堆栈跟踪: at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule模块(RuntimeModule模块,Int32 typeToken,IntPtr* typeInstArgs,Int32 typeInstCount,IntPtr* methodInstArgs,Int32 methodInstCount,ObjectHandleOnStack type)在System.Reflection.RuntimeModule.ResolveType(Int32,en21#,en23#)
在visual studio ide中正在传递相同的测试用例(使用vstest和IDE的相同版本)。
我的项目是使用newtonsoft 12.0.3。
我无法理解为什么测试会通过IDE而在控制台上失败。我检查了我的项目,发现使用的是相同版本的newtonsoft,即12.0.3。
发布于 2022-08-28 06:54:58
在深入研究之后,我发现在Visual中运行测试和通过控制台运行测试之间的区别。使用MSTest运行测试,vstest是通过控制台运行测试的实用工具。两者都有不同的配置。我发现MsTest有以下内容
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="4.5.0.0-12.0.0.2" newVersion="12.0.0.2"/>
<codeBase version="12.0.0.2" href="PrivateAssemblies\Newtonsoft.Json.dll"/>
</dependentAssembly>在testhost.exe.config中添加了上述配置,从而解决了问题
https://stackoverflow.com/questions/73501532
复制相似问题