【问题标题】:Unit tests pass with "dotnet test" but fail when run from Visual Studio 2017 Test Explorer单元测试通过“dotnet test”通过,但从 Visual Studio 2017 测试资源管理器运行时失败
【发布时间】:2017-07-29 11:23:35
【问题描述】:

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

我的测试项目以 .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>

【问题讨论】:

  • 你能分享一个重现项目吗?看起来您也没有使用 xunit net core 测试项目的新测试模板。你能试试用那个吗

标签: asp.net-core xunit.net visual-studio-2017


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2018-05-03
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多