Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ public static TestNode ToResultTestNode(UnitTestElement element, FrameworkTestRe
private static TestNode CreateBaseTestNode(UnitTestElement element, bool isTrxEnabled, string? displayNameOverride)
{
TestMethod testMethod = element.TestMethod;
string testFullName = $"{testMethod.FullClassName}.{testMethod.Name}";

TestNode testNode = new()
{
Uid = new TestNodeUid(element.GetTestId().ToString()),
DisplayName = displayNameOverride ?? testMethod.DisplayName ?? testFullName,

// TestMethod.DisplayName is always initialized (the constructor sets it to displayName ?? name), so
// displayNameOverride is the only real fallback needed here.
DisplayName = displayNameOverride ?? testMethod.DisplayName,
};

AddCategoriesAndTraits(testNode, element, isTrxEnabled);
Expand Down Expand Up @@ -194,8 +196,9 @@ private static void AddTrxResultProperties(TestNode testNode, UnitTestElement el
}

TestMethod testMethod = element.TestMethod;
string testFullName = $"{testMethod.FullClassName}.{testMethod.Name}";
testNode.Properties.Add(new TrxTestDefinitionName(testMethod.DisplayName ?? testFullName));

// TestMethod.DisplayName is always initialized (constructor sets it to displayName ?? name).
testNode.Properties.Add(new TrxTestDefinitionName(testMethod.DisplayName));

TestMethodIdentifierProperty? testMethodIdentifierProperty = testNode.Properties.SingleOrDefault<TestMethodIdentifierProperty>();
if (testMethodIdentifierProperty is not null)
Expand All @@ -205,13 +208,15 @@ private static void AddTrxResultProperties(TestNode testNode, UnitTestElement el
? testMethodIdentifierProperty.TypeName
: $"{testMethodIdentifierProperty.Namespace}.{testMethodIdentifierProperty.TypeName}"));
}
else if (TryParseFullyQualifiedType(testFullName, out string? fullyQualifiedType))
else if (!StringEx.IsNullOrEmpty(testMethod.FullClassName))
{
testNode.Properties.Add(new TrxFullyQualifiedTypeNameProperty(fullyQualifiedType));
// FullClassName is the source of truth for the type name, so use it directly instead of re-parsing it out
// of a "FullClassName.Name" string.
testNode.Properties.Add(new TrxFullyQualifiedTypeNameProperty(testMethod.FullClassName));
Comment thread
Evangelink marked this conversation as resolved.
}
else
{
throw new InvalidOperationException("Unable to parse fully qualified type name from test: " + testFullName);
throw new InvalidOperationException($"The test method '{testMethod.Name}' does not have a fully qualified class name.");
}
}

Expand Down Expand Up @@ -291,22 +296,5 @@ private static void AddAttachments(TestNode testNode, FrameworkTestResult result
testNode.Properties.Add(new FileArtifactProperty(new FileInfo(pathToResultFile), Resource.AttachmentSetDisplayName, resultFile));
}
}

private static bool TryParseFullyQualifiedType(string fullyQualifiedName, [NotNullWhen(true)] out string? fullyQualifiedType)
{
fullyQualifiedType = null;

int openBracketIndex = fullyQualifiedName.IndexOf('(');
int lastDotIndexBeforeOpenBracket = openBracketIndex <= 0
? fullyQualifiedName.LastIndexOf('.')
: fullyQualifiedName.LastIndexOf('.', openBracketIndex - 1);
if (lastDotIndexBeforeOpenBracket <= 0)
{
return false;
}

fullyQualifiedType = fullyQualifiedName[..lastDotIndexBeforeOpenBracket];
return true;
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ public void ToResultTestNode_AddsTrxProperties_WhenTrxEnabled()
typeName!.FullyQualifiedTypeName.Should().Be("MyNamespace.MyClass");
}

public void ToResultTestNode_UsesFullClassNameForTrxTypeName_WhenNoManagedMethodName()
{
// Without a managed method name, no TestMethodIdentifierProperty is added, so the TRX fully-qualified type
// name falls back to TestMethod.FullClassName directly (the behavior that replaced TryParseFullyQualifiedType).
UnitTestElement element = CreateElement(managedMethodName: null);
var result = new FrameworkTestResult { Outcome = UnitTestOutcome.Failed, ExceptionMessage = "boom", ExceptionStackTrace = "at X()" };

TestNode node = MSTestTestNodeConverter.ToResultTestNode(element, result, DateTimeOffset.Now, DateTimeOffset.Now, isTrxEnabled: true, new MSTestSettings());

node.Properties.Any<TestMethodIdentifierProperty>().Should().BeFalse();
Testing.Extensions.TrxReport.Abstractions.TrxFullyQualifiedTypeNameProperty? typeName =
node.Properties.SingleOrDefault<Testing.Extensions.TrxReport.Abstractions.TrxFullyQualifiedTypeNameProperty>();
typeName.Should().NotBeNull();
typeName!.FullyQualifiedTypeName.Should().Be("MyNamespace.MyClass");
}

public void ToResultTestNode_DoesNotAddTrxProperties_WhenTrxDisabled()
{
TestNode node = ResultNode(UnitTestOutcome.Passed);
Expand Down
Loading