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。

更改這些引用後,一切工作正常。