Reputation: 27509
When adding a SQL Server Unit Test item to a Unit Test Project in visual studio 2017 I get the following error:
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.
The project seems to build fine afterwards, but all sql unit tests fail with the following error:
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'..
[I've already solved this and will add my fix as an answer below, I'm creating this to document the solution for others that might find it helpful. I'll still be interested if anyone else has experienced this and can shed some light on the cause]
Upvotes: 3
Views: 1030
Reputation: 27509
This seems to be something to do with mismatched versions of the testing tools in the packages that are referenced automatically.
This is what I did to fix it:
Remove the references to Microsoft.VisualStudio.TestPlatform.TestFramework
and Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions
from the project.
Then add a new reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework
. You'll find it under the Assemblies -> Extensions list in the reference dialog. You might find there are two copies, both listed as version 10.1.0.0. In which case you should check the file version of each and you'll find that one is 14.0.23107.0 and one is 15.0.26228.0 It's the 15.x one that you want.
After changing these references everything worked fine for me.
Upvotes: 7