将SQL Server单元测试项添加到Visual Studio 2017中的单元测试项目时出错

时间:2017-05-07 19:39:42

标签: sql-server unit-testing mstest visual-studio-2017 sql-server-unit-testing

在Visual Studio 2017中向单元测试项目添加SQL Server单元测试项时,我收到以下错误:

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'..

[我已经解决了这个问题,我将在下面添加我的解决方案作为答案,我正在创建此解决方案,以便为其他人提供有用的解决方案。如果其他人经历过这种情况并且能够解释原因,我仍然会感兴趣]

1 个答案:

答案 0 :(得分:7)

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

这就是我为解决这个问题而采取的措施:

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

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

  3. 更改这些参考文献后,一切正常。