Home > Mobile >  vstest fails to load newtonsoft
vstest fails to load newtonsoft

Time:08-29

I am trying to run the test cases using C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe.

But some test are failing with exception System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Stack Trace: at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)

The same test cases are passing in visual studio ide (same version of vstest and IDE is used)

My project is using newtonsoft 12.0.3.

I am not able to understand why test are passing in IDE and failing in console. I checked my project and found that same version of newtonsoft is being used which is 12.0.3.

CodePudding user response:

After investigating further I found the difference between running the test in Visual studio IDE and via console. Visual Studio IDE uses MSTest to run the test and vstest is the utility to run the test via console. Both have different configurations. I found that MsTest has following

<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>

Added the above in testhost.exe.config in vstest configuration and this resolved the problem

  • Related