从命令行运行dotnet test
时,我的XUnit 2.2测试全部通过。从Visual Studio 2017的Test Explorer运行它们时,某些测试因Assembly Binding错误而失败。
我的测试项目针对.Net 4.6.2并引用了一个同样针对.Net 4.6.2的ASP.Net Core 1.1应用程序。在升级到.NET Core 1.1发布工具和Visual Studio 2017之前,单元测试在VS 2015中运行良好。
解决方法是为测试项目创建一个app.config
文件,并通过反复试验将所有需要的绑定重定向添加到其中。我不确定为什么我必须这样做。
似乎在使用dotnet test
时,运行时使用较新的程序集版本而没有抱怨。从Visual Studio运行时,重定向是必要的。
为什么这两种运行测试的方法之间的程序集绑定行为存在差异?
示例程序集绑定错误:
The operation failed.
Bind result: hr = 0x80131040. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\PROFESSIONAL\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
(Fully-specified)
LOG: Appbase = file:///C:/xx/xx/xx.Tests/bin/Debug/net462
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = C:\Users\xxxxx\AppData\Local\Temp\a1ec4d3c-04ff-4fa0-9e56-129e799dd870
LOG: AppName = a1ec4d3c-04ff-4fa0-9e56-129e799dd870
Calling assembly : Serilog.Settings.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\PROFESSIONAL\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/xx/xx/xx.Tests/bin/Debug/net462/Microsoft.Extensions.Configuration.Abstractions.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\xx\xx\xx.Tests\bin\Debug\net462\Microsoft.Extensions.Configuration.Abstractions.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Microsoft.Extensions.Configuration.Abstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
重定向需要解决它:
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" culture="neutral" publicKeyToken="adb9793829ddae60" />
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.1.1.0" />"
</dependentAssembly>
答案 0 :(得分:1)
事实证明,此问题正在https://github.com/Microsoft/vstest/issues/428
进行跟踪解决方法是将以下两项添加到测试项目的csproj文件中:
LinearLayout
这消除了手动添加绑定重定向的需要。