Removed the check to verify if traits are supported by reflection method
Taken a dependency on OM 11.0.0
This will not work with VS2012 and lesser versions of OM.
MODIFY
src/NUnitTestAdapter/NUnit.TestAdapter.csproj
src/NUnitTestAdapter/NUnit.TestAdapter.csproj
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net35'">
- <PackageReference Include="Microsoft.VisualStudio.TestPlatform.ObjectModel" Version="0.0.3" PrivateAssets="All" />
+ <PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="11.0.0" PrivateAssets="All" />
<PackageReference Include="Mono.Cecil" Version="0.9.6.4" />
<PackageReference Include="nunit.engine" Version="3.7.0" PrivateAssets="All" />
</ItemGroup>

MODIFY
src/NUnitTestAdapter/TraitsFeature.cs
src/NUnitTestAdapter/TraitsFeature.cs
@@ -32,52 +32,19 @@ namespace NUnit.VisualStudio.TestAdapter
{
public static class TraitsFeature
{
- private static readonly PropertyInfo TraitsProperty;
- private static readonly MethodInfo TraitsCollectionAdd;
- private static readonly PropertyInfo NameProperty;
- private static readonly PropertyInfo ValueProperty;
-
- static TraitsFeature()
- {
- TraitsProperty = typeof(TestCase).GetProperty("Traits");
- if (TraitsProperty != null)
- {
-
- var traitsCollectionType = TraitsProperty.PropertyType;
- TraitsCollectionAdd = traitsCollectionType.GetMethod("Add", new[] { typeof(string), typeof(string) });
-
- var traitType = Type.GetType("Microsoft.VisualStudio.TestPlatform.ObjectModel.Trait,Microsoft.VisualStudio.TestPlatform.ObjectModel");
- if (traitType != null)
- {
- NameProperty = traitType.GetProperty("Name");
- ValueProperty = traitType.GetProperty("Value");
- }
- }
-
- IsSupported = TraitsProperty != null && NameProperty != null && ValueProperty != null;
- }
-
- public static bool IsSupported { get; private set; }
public static void AddTrait(this TestCase testCase, string name, string value)
{
- if (TraitsCollectionAdd != null)
+ if(testCase != null)
{
- object traitsCollection = TraitsProperty.GetValue(testCase, new object[0]);
- TraitsCollectionAdd.Invoke(traitsCollection, new object[] { name, value });
+ testCase.Traits.Add(new Trait(name, value));
}
}
- public static void AddTraitsFromTestNode(this TestCase testCase, XmlNode testNode)
- {
- if (IsSupported)
- AddTraitsFromTestNode(testNode, TraitsProperty.GetValue(testCase, new object[0]));
- }
-
- private static void AddTraitsFromTestNode(XmlNode test, object traitsCollection)
+ public static void AddTraitsFromTestNode(this TestCase testCase, XmlNode test)
{
if (test.ParentNode != null)
- AddTraitsFromTestNode(test.ParentNode, traitsCollection);
+ AddTraitsFromTestNode(testCase, test.ParentNode);
foreach (XmlNode propertyNode in test.SelectNodes("properties/property"))
{
@@ -87,7 +54,7 @@ namespace NUnit.VisualStudio.TestAdapter
// Property names starting with '_' are for internal use only
if (!string.IsNullOrEmpty(propertyName) && propertyName[0] != '_' && !string.IsNullOrEmpty(propertyValue))
{
- TraitsCollectionAdd.Invoke(traitsCollection, new object[] { propertyName, propertyValue });
+ AddTrait(testCase, propertyName, propertyValue);
}
}
}
@@ -96,12 +63,9 @@ namespace NUnit.VisualStudio.TestAdapter
{
var traits = new List<NTrait>();
- if (IsSupported)
+ if (testCase != null && testCase.Traits != null)
{
- var traitsCollection = TraitsProperty.GetValue(testCase, new object[0]) as IEnumerable<object>;
-
- if (traitsCollection != null)
- traits.AddRange(from traitObject in traitsCollection let name = NameProperty.GetValue(traitObject, new object[0]) as string let value = ValueProperty.GetValue(traitObject, new object[0]) as string select new NTrait(name, value));
+ traits.AddRange(from trait in testCase.Traits let name = trait.Name let value = trait.Value select new NTrait(name, value));
}
return traits;

MODIFY
src/NUnitTestAdapterTests/NUnit.TestAdapter.Tests.csproj
src/NUnitTestAdapterTests/NUnit.TestAdapter.Tests.csproj
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
- <PackageReference Include="Microsoft.VisualStudio.TestPlatform.ObjectModel" Version="0.0.3" PrivateAssets="All" />
+ <PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="11.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">

MODIFY
src/NUnitTestAdapterTests/TestConverterTests.cs
src/NUnitTestAdapterTests/TestConverterTests.cs
@@ -104,14 +104,9 @@ namespace NUnit.VisualStudio.TestAdapter.Tests
Assert.That(testCase.LineNumber, Is.EqualTo(FakeTestData.LineNumber));
}
- // Check traits using reflection, since the feature was added
- // in an update to VisualStudio and may not be present.
- if (TraitsFeature.IsSupported)
- {
var traitList = testCase.GetTraits().Select(trait => trait.Name + ":" + trait.Value).ToList();
Assert.That(traitList, Is.EquivalentTo(new[] { "Category:super", "Category:cat1", "Priority:medium" }));
}
}
}
-}

MODIFY
src/NUnitTestAdapterTests/TestTFSBuild.cs
src/NUnitTestAdapterTests/TestTFSBuild.cs
@@ -45,8 +45,6 @@ namespace NUnit.VisualStudio.TestAdapter.Tests
[Test]
public void PropertyValueProviderCategoryWithOneCategory()
{
- CheckTraitsSupported();
-
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
tc.AddTrait("Category", "CI");
var testFilter = new TfsTestFilter(null);
@@ -57,8 +55,6 @@ namespace NUnit.VisualStudio.TestAdapter.Tests
[Test]
public void PropertyValueProviderCategoryWithNoTraits()
{
- CheckTraitsSupported();
-
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
var testFilter = new TfsTestFilter(null);
var obj = TfsTestFilter.PropertyValueProvider(tc, "TestCategory");
@@ -68,8 +64,6 @@ namespace NUnit.VisualStudio.TestAdapter.Tests
[Test]
public void PropertyValueProviderCategoryWithMultipleCategories()
{
- CheckTraitsSupported();
-
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
tc.AddTrait("Category", "CI");
tc.AddTrait("Category", "MyOwn");
@@ -90,11 +84,5 @@ namespace NUnit.VisualStudio.TestAdapter.Tests
var obj = TfsTestFilter.PropertyValueProvider(tc, "Garbage");
Assert.Null(obj);
}
-
- private static void CheckTraitsSupported()
- {
- if (!TraitsFeature.IsSupported)
- Assert.Inconclusive("This version of Visual Studio does not support Traits");
- }
}
}

Removed the check to verify if traits are supported by reflection method

- Taken a dependency on OM 11.0.0
- This will not work with VS2012 and lesser versions of OM.