在Visual Studio 2017中向单元测试项目添加SQL Server单元测试项时出现错误。

3
在 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个回答

7
这似乎与自动引用的包中测试工具的版本不匹配有关。
这是我解决它的方法:
  1. 从项目中删除对 Microsoft.VisualStudio.TestPlatform.TestFrameworkMicrosoft.VisualStudio.TestPlatform.TestFramework.Extensions 的引用。
  2. 然后添加一个新的引用到 Microsoft.VisualStudio.QualityTools.UnitTestFramework。你会在引用对话框的程序集 -> 扩展列表下找到它。您可能会发现有两个副本,都列为版本10.1.0.0。在这种情况下,您应该检查每个文件的文件版本,您会发现一个是14.0.23107.0,另一个是15.0.26228.0。你需要选择15.x版本的。
更改这些引用后,一切都可以正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接