单元测试通过“dotnet test"但从 Visual Studio 2017 测试资源管理器运行时失败 [英] Unit tests pass with "dotnet test" but fail when run from Visual Studio 2017 Test Explorer

查看:17
本文介绍了单元测试通过“dotnet test"但从 Visual Studio 2017 测试资源管理器运行时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My XUnit 2.2 tests all pass when run from the command line with dotnet test. When running them from Visual Studio 2017's Test Explorer some of the tests fail with Assembly Binding errors.

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

My test project targets .Net 4.6.2 and references an ASP.Net Core 1.1 application also targeting .Net 4.6.2. The unit tests were working fine in VS 2015 prior to upgrading to the .NET Core 1.1 release tooling and Visual Studio 2017.

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

The workaround was to create an app.config file for the test project and add all of the needed binding redirects to it via trial and error. I'm not sure why I have to do this, though.

似乎在使用 dotnet test 时,运行时使用较新的程序集版本而没有抱怨.从 Visual Studio 运行时,重定向是必需的.

It appears that when using dotnet test the runtime uses the newer assembly versions without complaint. When running from Visual Studio, the redirects are necessary.

为什么这两种运行测试的方法在程序集绑定行为上存在差异?

Why is there a difference in the assembly binding behavior between these two methods of running the tests?

示例程序集绑定错误:

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>

推荐答案

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

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

The workaround is to add the following two items to the test project's csproj file:

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

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

This eliminates the need for manually adding binding redirects.

这篇关于单元测试通过“dotnet test"但从 Visual Studio 2017 测试资源管理器运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆