Skip to content
Open
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 @@ -17,15 +17,18 @@ namespace Microsoft.Testing.Extensions.TrxReport.Abstractions;
/// <see cref="ErrorTestNodeStateProperty"/>, <see cref="TimeoutTestNodeStateProperty"/>, or
/// <see cref="SkippedTestNodeStateProperty"/> are included in the report.
/// Properties supplied only on discovery or in-progress updates are not carried forward to the final update.
/// The final state is the only required property; every other property is optional.
/// </para>
/// <para>
/// To create a test definition, the reporter requires either <see cref="TrxFullyQualifiedTypeNameProperty"/>
/// or <see cref="TestMethodIdentifierProperty"/> on the final node.
/// To create a test definition, the reporter uses <see cref="TrxFullyQualifiedTypeNameProperty"/> and
/// <see cref="TestMethodIdentifierProperty"/> from the final node. Providing at least one of them is recommended
/// so that the test is grouped under its real class.
/// <see cref="TrxFullyQualifiedTypeNameProperty"/> supplies the TRX class name when present; otherwise,
/// the namespace and type name from <see cref="TestMethodIdentifierProperty"/> are used.
/// When neither property is present, the class name is the <c>UnknownClass</c> placeholder because the TRX schema
/// requires a class name.
/// The method identifier supplies the method name when available; otherwise, <see cref="TestNode.DisplayName"/> is used.
/// Source-file information is not required, and the reporter does not look up whether the class or method exists.
/// Omitting both identity properties causes report generation to fail.
/// </para>
/// <para>The following properties are optional:</para>
/// <list type="bullet">
Expand Down Expand Up @@ -88,7 +91,9 @@ namespace Microsoft.Testing.Extensions.TrxReport.Abstractions;
/// <para>
/// Provide one final state and at most one instance of each identity or optional property above,
/// except <see cref="TestMetadataProperty"/> and <see cref="FileArtifactProperty"/>, which can have multiple instances.
/// <see cref="StandardOutputProperty"/> and <see cref="TestFileLocationProperty"/> are not consumed by the TRX reporter.
/// Providing more than one instance of a consumed singleton property makes report generation fail.
/// Unconsumed properties such as <see cref="StandardOutputProperty"/> and
/// <see cref="TestFileLocationProperty"/> are ignored.
/// </para>
/// </remarks>
/// <seealso href="https://learn.microsoft.com/dotnet/core/testing/microsoft-testing-platform-test-reports#visual-studio-test-reports-trx">TRX report extension documentation</seealso>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ dotnet add package Microsoft.Testing.Extensions.TrxReport.Abstractions

Reference this package from a testing-platform extension that needs to contribute TRX-specific properties or inspect `ITrxReportCapability`. Test projects that only need to generate a TRX report should use `Microsoft.Testing.Extensions.TrxReport` instead.

## Required and optional properties

A test framework that supports TRX reporting adds properties to its final test-node updates. The only required property is the final state:

- **Required**: one final state, such as `PassedTestNodeStateProperty` or `FailedTestNodeStateProperty`. Updates without a final state are not reported.
- **Recommended**: `TrxFullyQualifiedTypeNameProperty` or `TestMethodIdentifierProperty`, so tests are grouped under their real class. Without either, the class name is the `UnknownClass` placeholder and the method name is the test node's display name.
- **Optional**: `TimingProperty`, `TrxTestDefinitionName`, `TrxExceptionProperty`, `TrxMessagesProperty`, `TrxCategoriesProperty`, `TestMetadataProperty`, `TrxWorkItemsProperty`, and `FileArtifactProperty`.
- **Duplicates**: providing more than one instance of a consumed singleton property makes report generation fail, except for `TestMetadataProperty` and `FileArtifactProperty`, which can have multiple instances. Unconsumed properties are ignored.

The `ITrxReportCapability` API documentation describes what each property contributes to the report.

## About

This package provides:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Extensions.TrxReport.Abstractions.Streaming;
using Microsoft.Testing.Extensions.TrxReport.Resources;
using Microsoft.Testing.Platform;
using Microsoft.Testing.Platform.Helpers;

Expand Down Expand Up @@ -244,7 +243,8 @@ private SummaryCounts AddResults(IReadOnlyList<TrxTestResult> testResults, strin
// The use of testResultDisplayName here is very wrong.
// We keep it as a fallback if we cannot determine the testMethodName (when TestMethodIdentifierProperty isn't present).
// This will most likely be hit for NUnit.
// However, this is very wrong and we probably should fail if TestMethodIdentifierProperty isn't present.
// We intentionally don't fail the report when TestMethodIdentifierProperty isn't present, so that test frameworks
// that don't provide the TRX-specific properties still get a usable report.
testMethod.SetAttributeValue("name", testMethodName ?? testResultDisplayName);

unitTest.Add(testMethod);
Expand Down Expand Up @@ -272,7 +272,7 @@ private SummaryCounts AddResults(IReadOnlyList<TrxTestResult> testResults, strin
return new SummaryCounts(passed, failed, skipped, timedout);
}

private (string ClassName, string? TestMethodName) GetClassAndMethodName(TrxTestResult testResult)
private static (string ClassName, string? TestMethodName) GetClassAndMethodName(TrxTestResult testResult)
{
TrxTestMethodIdentifier? methodIdentifier = testResult.TestMethodIdentifier;

Expand All @@ -281,7 +281,10 @@ private SummaryCounts AddResults(IReadOnlyList<TrxTestResult> testResults, strin
return (className, methodIdentifier?.MethodName);
}

_ = methodIdentifier ?? throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ExtensionResources.TrxReportFrameworkDoesNotSupportTrxReportCapability, _testFrameworkAdapter.DisplayName, _testFrameworkAdapter.Uid));
if (methodIdentifier is null)
{
return (FallbackClassName, null);
}

string classNameFromIdentifierProperty = RoslynString.IsNullOrEmpty(methodIdentifier.Namespace)
? methodIdentifier.TypeName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ internal sealed partial class TrxReportEngine
private const string UnitTestTypeGuid = "13CDC9D9-DDB5-4fa4-A97D-D965CCFC6D4B";
private const string UncategorizedTestListId = "8C84FA94-04C1-424b-9868-57A2D4851A1D";

// The TRX schema requires a class name. When a test node has neither a fully qualified type name nor a test method
// identifier, use an explicit placeholder so consumers can tell that the type information is unknown.
private const string FallbackClassName = "UnknownClass";

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description has been reconciled: it now identifies UnknownClass, explains why DefaultClassName/VSTest parity was rejected, and records the post-rename demo and red/green evidence on d7e51a5. The current head 1380e72 is documentation-only.


private static readonly XNamespace NamespaceUri = XNamespace.Get("http://microsoft.com/schemas/VisualStudio/TeamTest/2010");

private readonly ITestApplicationModuleInfo _testApplicationModuleInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ public async Task Session_FullLifecycleStreamsOnlyFinalResultsAndPublishesContra
Assert.AreEqual("publish", fixture.Events[^1]);
}

[TestMethod]
public async Task Session_NodeWithOnlyStateAndTimingStreamsAndReportsFallbackClassName()
{
using var fixture = new TrxBehaviorFixture();
using var cancellation = new CancellationTokenSource();
var session = new TestSessionContext(cancellation.Token);
TrxReportGenerator generator = fixture.CreateGenerator();
await generator.OnTestSessionStartingAsync(session);
TimingProperty timing = new(new TimingInfo(TrxBehaviorFixture.Start, TrxBehaviorFixture.Start.AddSeconds(2), TimeSpan.FromSeconds(2)));
TestNode node = new() { Uid = "bare", DisplayName = "bare display name", Properties = new PropertyBag(PassedTestNodeStateProperty.CachedInstance, timing) };

await generator.ConsumeAsync(null!, new TestNodeUpdateMessage(new SessionUid("fixture-session"), node), cancellation.Token);
await fixture.FinishAsync(generator, session);

XElement testMethod = fixture.Report.Descendants(Ns + "TestMethod").Single();
Assert.AreEqual("UnknownClass", (string?)testMethod.Attribute("className"));
Assert.AreEqual("bare display name", (string?)testMethod.Attribute("name"));
Assert.AreEqual("00:00:02.0000000", (string?)fixture.Report.Descendants(Ns + "UnitTestResult").Single().Attribute("duration"));
Assert.AreEqual("Completed", (string?)fixture.Report.Descendants(Ns + "ResultSummary").Single().Attribute("outcome"));
Assert.IsEmpty(fixture.Displays);
Assert.HasCount(1, fixture.Publications);
}

[TestMethod]
public async Task Session_ZeroResultsAvoidsWriterAndProducesEmptyReport()
{
Expand Down
62 changes: 62 additions & 0 deletions test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,63 @@ public async Task TrxReportEngine_GenerateReportAsync_WithAdapterSupportTrxCapab
Assert.Contains(@"className=""TrxFullyQualifiedTypeName", trxContent, trxContent);
}

[TestMethod]
public async Task TrxReportEngine_GenerateReportAsync_WithOnlyStateAndTiming_UsesFallbackClassNameAndDisplayNameAsMethodName()
{
// Arrange
using MemoryFileStream memoryStream = new();
DateTimeOffset startTime = new(2026, 9, 27, 12, 0, 0, TimeSpan.Zero);
var propertyBag = new PropertyBag(
new PassedTestNodeStateProperty(),
new TimingProperty(new TimingInfo(startTime, startTime.AddSeconds(3), TimeSpan.FromSeconds(3))));
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(memoryStream);

// Act
(string fileName, string? warning) = await trxReportEngine.GenerateReportAsync([ExtractTestResult("test()", "My display name", propertyBag)]);

// Assert
Assert.IsNull(warning);
AssertExpectedTrxFileName(fileName);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
XNamespace ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
XElement testMethod = xml.Descendants(ns + "TestMethod").Single();
Assert.AreEqual("UnknownClass", testMethod.Attribute("className")!.Value);
Assert.AreEqual("My display name", testMethod.Attribute("name")!.Value);
Assert.AreEqual("00:00:03.0000000", xml.Descendants(ns + "UnitTestResult").Single().Attribute("duration")!.Value);
}

[TestMethod]
[DataRow(null, "My.Namespace.MyType")]
[DataRow("Fqn.Namespace.FqnType", "Fqn.Namespace.FqnType")]
public async Task TrxReportEngine_GenerateReportAsync_WithTestMethodIdentifier_ClassNameIsFullyQualifiedTypeNameElseNamespaceAndType(string? fullyQualifiedTypeName, string expectedClassName)
{
// Arrange
using MemoryFileStream memoryStream = new();
var propertyBag = new PropertyBag(
new PassedTestNodeStateProperty(),
new TestMethodIdentifierProperty("assembly", "My.Namespace", "MyType", "MyMethod", 0, [], "System.Void"));
if (fullyQualifiedTypeName is not null)
{
propertyBag.Add(new TrxFullyQualifiedTypeNameProperty(fullyQualifiedTypeName));
}

TrxReportEngine trxReportEngine = GenerateTrxReportEngine(memoryStream);

// Act
(string fileName, string? warning) = await trxReportEngine.GenerateReportAsync([ExtractTestResult("test()", "My display name", propertyBag)]);

// Assert
Assert.IsNull(warning);
AssertExpectedTrxFileName(fileName);
Assert.IsNotNull(memoryStream.TrxContent);
XNamespace ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
XElement testMethod = memoryStream.TrxContent.Descendants(ns + "TestMethod").Single();
Assert.AreEqual(expectedClassName, testMethod.Attribute("className")!.Value);
Assert.AreEqual("MyMethod", testMethod.Attribute("name")!.Value);
}

[TestMethod]
public async Task TrxReportEngine_GenerateReportAsync_WithArtifactsByTestNode_TrxContainsResultFile()
{
Expand Down Expand Up @@ -1081,6 +1138,11 @@ private static TrxTestResult CreateTestNodeUpdate(string uid, string displayName
propertyBag.Add(new TrxFullyQualifiedTypeNameProperty("MyNamespace.MyClass"));
}

return ExtractTestResult(uid, displayName, propertyBag);
}

private static TrxTestResult ExtractTestResult(string uid, string displayName, PropertyBag propertyBag)
{
var message = new TestNodeUpdateMessage(
new SessionUid("1"),
new TestNode { Uid = uid, DisplayName = displayName, Properties = propertyBag });
Expand Down
Loading