Skip to content

Commit 317f982

Browse files
[rel/3.9] VSTestBridge: Handle TestPropertyAttributes.Trait instead of special casing specific properties (#5648)
Co-authored-by: Youssef1313 <[email protected]>
1 parent aea689c commit 317f982

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/ObjectModelConverters.cs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ internal static class ObjectModelConverters
3434
valueType: typeof(string),
3535
owner: typeof(TestCase));
3636

37-
private static readonly TestProperty TestCategoryProperty = TestProperty.Register(
38-
id: "MSTestDiscoverer.TestCategory",
39-
label: "TestCategory",
40-
valueType: typeof(string[]),
41-
owner: typeof(TestCase));
42-
43-
private static readonly TestProperty TraitsProperty = TestProperty.Register(
44-
id: "TestObject.Traits",
45-
label: "Traits",
46-
valueType: typeof(KeyValuePair<string, string>[]),
47-
owner: typeof(TestObject));
48-
4937
/// <summary>
5038
/// Converts a VSTest <see cref="TestCase"/> to a Microsoft Testing Platform <see cref="TestNode"/>.
5139
/// </summary>
@@ -82,27 +70,33 @@ public static TestNode ToTestNode(this TestCase testCase, bool isTrxEnabled, INa
8270

8371
private static void CopyCategoryAndTraits(TestObject testCaseOrResult, TestNode testNode, bool isTrxEnabled)
8472
{
85-
// TPv2 is doing some special handling for MSTest... we should probably do the same.
86-
// See https://github.com/microsoft/vstest/blob/main/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs#L66-L70
87-
if (testCaseOrResult.GetPropertyValue<string[]>(TestCategoryProperty, defaultValue: null) is string[] mstestCategories)
73+
foreach (KeyValuePair<TestProperty, object?> property in testCaseOrResult.GetProperties())
8874
{
89-
if (isTrxEnabled)
75+
#pragma warning disable CS0618 // Type or member is obsolete
76+
if ((property.Key.Attributes & TestPropertyAttributes.Trait) == 0)
77+
#pragma warning restore CS0618 // Type or member is obsolete
9078
{
91-
testNode.Properties.Add(new TrxCategoriesProperty(mstestCategories));
79+
continue;
9280
}
9381

94-
foreach (string category in mstestCategories)
82+
if (property.Value is string[] categories)
9583
{
96-
testNode.Properties.Add(new TestMetadataProperty(category, string.Empty));
97-
}
98-
}
84+
if (isTrxEnabled)
85+
{
86+
testNode.Properties.Add(new TrxCategoriesProperty(categories));
87+
}
9988

100-
if (testCaseOrResult.GetPropertyValue<KeyValuePair<string, string>[]>(TraitsProperty, defaultValue: null) is KeyValuePair<string, string>[] traits &&
101-
traits.Length > 0)
102-
{
103-
foreach (KeyValuePair<string, string> trait in traits)
89+
foreach (string category in categories)
90+
{
91+
testNode.Properties.Add(new TestMetadataProperty(category, string.Empty));
92+
}
93+
}
94+
else if (property.Value is KeyValuePair<string, string>[] traits)
10495
{
105-
testNode.Properties.Add(new TestMetadataProperty(trait.Key, trait.Value));
96+
foreach (KeyValuePair<string, string> trait in traits)
97+
{
98+
testNode.Properties.Add(new TestMetadataProperty(trait.Key, trait.Value));
99+
}
106100
}
107101
}
108102
}

test/UnitTests/Microsoft.Testing.Extensions.VSTestBridge.UnitTests/ObjectModel/ObjectModelConvertersTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public void ToTestNode_WhenTestResultOutcomeIsFailed_TestNodePropertiesContainFa
7676
public void ToTestNode_WhenTestResultHasMSTestDiscovererTestCategoryTestProperty_TestNodePropertiesContainTheCategoryInTraits()
7777
{
7878
TestResult testResult = new(new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs"));
79-
var testCategoryProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "Label", typeof(string[]), TestPropertyAttributes.None, typeof(TestCase));
79+
#pragma warning disable CS0618 // Type or member is obsolete
80+
var testCategoryProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "Label", typeof(string[]), TestPropertyAttributes.Trait, typeof(TestCase));
81+
#pragma warning restore CS0618 // Type or member is obsolete
8082
testResult.SetPropertyValue<string[]>(testCategoryProperty, ["category1"]);
8183

8284
var testNode = testResult.ToTestNode(false, new NamedFeatureCapabilityWithVSTestProvider(), new ServerModeCommandLineOptions(), ClientInfo);
@@ -91,7 +93,9 @@ public void ToTestNode_WhenTestResultHasMSTestDiscovererTestCategoryTestProperty
9193
public void ToTestNode_WhenTestResultHasMSTestDiscovererTestCategoryTestPropertyWithTrxEnabled_TestNodePropertiesContainTrxCategoriesProperty()
9294
{
9395
TestResult testResult = new(new TestCase("assembly.class.SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs"));
94-
var testCategoryProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "Label", typeof(string[]), TestPropertyAttributes.None, typeof(TestCase));
96+
#pragma warning disable CS0618 // Type or member is obsolete
97+
var testCategoryProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "Label", typeof(string[]), TestPropertyAttributes.Trait, typeof(TestCase));
98+
#pragma warning restore CS0618 // Type or member is obsolete
9599
testResult.SetPropertyValue<string[]>(testCategoryProperty, ["category1"]);
96100

97101
var testNode = testResult.ToTestNode(true, new NamedFeatureCapabilityWithVSTestProvider(), new ServerModeCommandLineOptions(), ClientInfo);

0 commit comments

Comments
 (0)