Skip to content

Commit c550bb0

Browse files
EvangelinkCopilotCopilot
authored
perf: eliminate eager testFullName allocation in MSTestTestNodeConverter (#9824)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent cfb0ddc commit c550bb0

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

‎src/Adapter/MSTest.TestAdapter/TestingPlatformAdapter/MSTestTestNodeConverter.cs‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ public static TestNode ToResultTestNode(UnitTestElement element, FrameworkTestRe
8585
private static TestNode CreateBaseTestNode(UnitTestElement element, bool isTrxEnabled, string? displayNameOverride)
8686
{
8787
TestMethod testMethod = element.TestMethod;
88-
string testFullName = $"{testMethod.FullClassName}.{testMethod.Name}";
8988

9089
TestNode testNode = new()
9190
{
9291
Uid = new TestNodeUid(element.GetTestId().ToString()),
93-
DisplayName = displayNameOverride ?? testMethod.DisplayName ?? testFullName,
92+
93+
// TestMethod.DisplayName is always initialized (the constructor sets it to displayName ?? name), so
94+
// displayNameOverride is the only real fallback needed here.
95+
DisplayName = displayNameOverride ?? testMethod.DisplayName,
9496
};
9597

9698
AddCategoriesAndTraits(testNode, element, isTrxEnabled);
@@ -194,8 +196,9 @@ private static void AddTrxResultProperties(TestNode testNode, UnitTestElement el
194196
}
195197

196198
TestMethod testMethod = element.TestMethod;
197-
string testFullName = $"{testMethod.FullClassName}.{testMethod.Name}";
198-
testNode.Properties.Add(new TrxTestDefinitionName(testMethod.DisplayName ?? testFullName));
199+
200+
// TestMethod.DisplayName is always initialized (constructor sets it to displayName ?? name).
201+
testNode.Properties.Add(new TrxTestDefinitionName(testMethod.DisplayName));
199202

200203
TestMethodIdentifierProperty? testMethodIdentifierProperty = testNode.Properties.SingleOrDefault<TestMethodIdentifierProperty>();
201204
if (testMethodIdentifierProperty is not null)
@@ -205,13 +208,15 @@ private static void AddTrxResultProperties(TestNode testNode, UnitTestElement el
205208
? testMethodIdentifierProperty.TypeName
206209
: $"{testMethodIdentifierProperty.Namespace}.{testMethodIdentifierProperty.TypeName}"));
207210
}
208-
else if (TryParseFullyQualifiedType(testFullName, out string? fullyQualifiedType))
211+
else if (!StringEx.IsNullOrEmpty(testMethod.FullClassName))
209212
{
210-
testNode.Properties.Add(new TrxFullyQualifiedTypeNameProperty(fullyQualifiedType));
213+
// FullClassName is the source of truth for the type name, so use it directly instead of re-parsing it out
214+
// of a "FullClassName.Name" string.
215+
testNode.Properties.Add(new TrxFullyQualifiedTypeNameProperty(testMethod.FullClassName));
211216
}
212217
else
213218
{
214-
throw new InvalidOperationException("Unable to parse fully qualified type name from test: " + testFullName);
219+
throw new InvalidOperationException($"The test method '{testMethod.Name}' does not have a fully qualified class name.");
215220
}
216221
}
217222

@@ -291,22 +296,5 @@ private static void AddAttachments(TestNode testNode, FrameworkTestResult result
291296
testNode.Properties.Add(new FileArtifactProperty(new FileInfo(pathToResultFile), Resource.AttachmentSetDisplayName, resultFile));
292297
}
293298
}
294-
295-
private static bool TryParseFullyQualifiedType(string fullyQualifiedName, [NotNullWhen(true)] out string? fullyQualifiedType)
296-
{
297-
fullyQualifiedType = null;
298-
299-
int openBracketIndex = fullyQualifiedName.IndexOf('(');
300-
int lastDotIndexBeforeOpenBracket = openBracketIndex <= 0
301-
? fullyQualifiedName.LastIndexOf('.')
302-
: fullyQualifiedName.LastIndexOf('.', openBracketIndex - 1);
303-
if (lastDotIndexBeforeOpenBracket <= 0)
304-
{
305-
return false;
306-
}
307-
308-
fullyQualifiedType = fullyQualifiedName[..lastDotIndexBeforeOpenBracket];
309-
return true;
310-
}
311299
}
312300
#endif

‎test/UnitTests/MSTestAdapter.UnitTests/MSTestTestNodeConverterTests.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ public void ToResultTestNode_AddsTrxProperties_WhenTrxEnabled()
230230
typeName!.FullyQualifiedTypeName.Should().Be("MyNamespace.MyClass");
231231
}
232232

233+
public void ToResultTestNode_UsesFullClassNameForTrxTypeName_WhenNoManagedMethodName()
234+
{
235+
// Without a managed method name, no TestMethodIdentifierProperty is added, so the TRX fully-qualified type
236+
// name falls back to TestMethod.FullClassName directly (the behavior that replaced TryParseFullyQualifiedType).
237+
UnitTestElement element = CreateElement(managedMethodName: null);
238+
var result = new FrameworkTestResult { Outcome = UnitTestOutcome.Failed, ExceptionMessage = "boom", ExceptionStackTrace = "at X()" };
239+
240+
TestNode node = MSTestTestNodeConverter.ToResultTestNode(element, result, DateTimeOffset.Now, DateTimeOffset.Now, isTrxEnabled: true, new MSTestSettings());
241+
242+
node.Properties.Any<TestMethodIdentifierProperty>().Should().BeFalse();
243+
Testing.Extensions.TrxReport.Abstractions.TrxFullyQualifiedTypeNameProperty? typeName =
244+
node.Properties.SingleOrDefault<Testing.Extensions.TrxReport.Abstractions.TrxFullyQualifiedTypeNameProperty>();
245+
typeName.Should().NotBeNull();
246+
typeName!.FullyQualifiedTypeName.Should().Be("MyNamespace.MyClass");
247+
}
248+
233249
public void ToResultTestNode_DoesNotAddTrxProperties_WhenTrxDisabled()
234250
{
235251
TestNode node = ResultNode(UnitTestOutcome.Passed);

0 commit comments

Comments
 (0)