3

当在Visual Studio 2017年将SQL服务器单元测试项目的单元测试项目,我得到以下错误:错误的单元测试项目2017年

The reference "Microsoft.VisualStudio.QualityTools.UnitTestFramework, 
Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, 
processorArchitecture=MSIL" could not be added to the project. 
This wizard will continue to run, but the resulting project may not build properly. 

enter image description here

该项目似乎构建细算账,但所有SQL单元测试失败,出现以下错误:

Test Name: SqlTest1 
Test FullName: UnitTestProject1.SqlServerUnitTest1.SqlTest1 
Test Source: c:\[path]\SqlServerUnitTest1.cs : line 34 
Test Outcome: Failed 
Test Duration: 0:00:00.0278356 

Result StackTrace: 
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) 
    at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) 
    at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) 
    at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) 
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) 

Result Message: 
    Unable to set TestContext property for the class 
    UnitTestProject1.SqlServerUnitTest1. 
    Error: System.ArgumentException: Object of type 
    'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation' 
    cannot be converted to type 'Microsoft.VisualStudio.TestTools.UnitTesting.TestContext'.. 

[我已经解决了这个问题,并将下面的答案添加到我的解决方案中,我将其创建为记录其他人可能会觉得有用的解决方案。如果其他任何人遇到过这种情况,我仍会感兴趣,并且可以从中找出原因]

回答

6

这似乎是与自动引用的软件包中测试工具的不匹配版本有关。

这是我做过什么来解决这个问题:

  1. 从项目中删除引用到Microsoft.VisualStudio.TestPlatform.TestFrameworkMicrosoft.VisualStudio.TestPlatform.TestFramework.Extensions

  2. 然后添加一个新的参考Microsoft.VisualStudio.QualityTools.UnitTestFramework。您可以在参考对话框的Assemblies - > Extensions列表下找到它。您可能会发现有两个副本,均列为10.1.0.0版。在这种情况下,你应该检查每个文件的版本,你会发现一个是14.0.23107.0,另一个是15.0.26228.0它是你想要的15.x。

更改这些引用后,一切工作正常。