单元测试通过 "dotnet test" 但在 运行 时失败 Visual Studio 2017 Test Explorer

Unit tests pass with "dotnet test" but fail when run from Visual Studio 2017 Test Explorer

我的 XUnit 2.2 测试全部通过 运行 从命令行 dotnet test。当 运行 从 Visual Studio 2017 年的测试资源管理器中调用它们时,一些测试因程序集绑定错误而失败。

我的测试项目以 .Net 4.6.2 为目标并引用一个 ASP.Net Core 1.1 应用程序也以 .Net 4.6.2 为目标。在升级到 .NET Core 1.1 发布工具和 Visual Studio 2017 之前,单元测试在 VS 2015 中运行良好。

解决方法是为测试项目创建一个 app.config 文件,并通过反复试验向其添加所有需要的绑定重定向。不过,我不确定为什么我必须这样做。

似乎在使用 dotnet test 时 运行 时间使用较新的程序集版本而没有抱怨。当 运行ning 来自 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 STUDIO17\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 STUDIO17\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>

原来这个问题正在 https://github.com/Microsoft/vstest/issues/428

被跟踪

解决方法是将以下两项添加到测试项目的 csproj 文件中:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>      
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

这消除了手动添加绑定重定向的需要。