Skip to content

Commit

Permalink
Add extra properties for test results in dotnet test (#3932)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariam-abdulla authored Oct 11, 2024
1 parent 922ad3b commit 29f324d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled,
if (testResultMessage.Category == TestResultMessage.StandardErrorCategory)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardError", testResultMessage.Text ?? string.Empty));
testNode.Properties.Add(new StandardErrorProperty(testResultMessage.Text ?? string.Empty));
}

if (testResultMessage.Category == TestResultMessage.StandardOutCategory)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardOutput", testResultMessage.Text ?? string.Empty));
testNode.Properties.Add(new StandardOutputProperty(testResultMessage.Text ?? string.Empty));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,18 @@ protected static void WriteField(Stream stream, ushort id, string? value)
WriteStringValue(stream, value);
}

protected static void WriteField(Stream stream, ushort id, long? value)
{
if (value is null)
{
return;
}

WriteShort(stream, id);
WriteSize<long>(stream);
WriteLong(stream, value.Value);
}

protected static void WriteField(Stream stream, string? value)
{
if (value is null)
Expand Down Expand Up @@ -370,6 +382,7 @@ protected static void WriteAtPosition(Stream stream, int value, long position)
Type type when type == typeof(short) => sizeof(short),
Type type when type == typeof(bool) => sizeof(bool),
Type type when type == typeof(byte) => sizeof(byte),
Type type when type == typeof(long) => sizeof(long),
_ => 0,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
{
case TestNodeUpdateMessage testNodeUpdateMessage:

GetTestNodeDetails(testNodeUpdateMessage, out byte? state, out string? reason, out string? errorMessage, out string? errorStackTrace);
TestNodeDetails testNodeDetails = GetTestNodeDetails(testNodeUpdateMessage);

switch (state)
switch (testNodeDetails.State)
{
case TestStates.Discovered:
DiscoveredTestMessages discoveredTestMessages = new(
Expand All @@ -73,8 +73,11 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
new SuccessfulTestResultMessage(
testNodeUpdateMessage.TestNode.Uid.Value,
testNodeUpdateMessage.TestNode.DisplayName,
state,
reason ?? string.Empty,
testNodeDetails.State,
testNodeDetails.Duration,
testNodeDetails.Reason ?? string.Empty,
testNodeDetails.StandardOutput ?? string.Empty,
testNodeDetails.StandardError ?? string.Empty,
testNodeUpdateMessage.SessionUid.Value),
},
Array.Empty<FailedTestResultMessage>());
Expand All @@ -91,10 +94,13 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
new FailedTestResultMessage(
testNodeUpdateMessage.TestNode.Uid.Value,
testNodeUpdateMessage.TestNode.DisplayName,
state,
reason ?? string.Empty,
errorMessage ?? string.Empty,
errorStackTrace ?? string.Empty,
testNodeDetails.State,
testNodeDetails.Duration,
testNodeDetails.Reason ?? string.Empty,
testNodeDetails.ErrorMessage ?? string.Empty,
testNodeDetails.ErrorStackTrace ?? string.Empty,
testNodeDetails.StandardOutput ?? string.Empty,
testNodeDetails.StandardError ?? string.Empty,
testNodeUpdateMessage.SessionUid.Value),
});

Expand Down Expand Up @@ -157,13 +163,17 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
}
}

private static void GetTestNodeDetails(TestNodeUpdateMessage testNodeUpdateMessage, out byte? state, out string? reason, out string? errorMessage, out string? errorStackTrace)
private static TestNodeDetails GetTestNodeDetails(TestNodeUpdateMessage testNodeUpdateMessage)
{
state = null;
reason = string.Empty;
errorMessage = string.Empty;
errorStackTrace = string.Empty;
byte? state = null;
long? duration = null;
string? reason = string.Empty;
string? errorMessage = string.Empty;
string? errorStackTrace = string.Empty;

TestNodeStateProperty nodeState = testNodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>();
string? standardOutput = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<StandardOutputProperty>()?.StandardOutput;
string? standardError = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<StandardErrorProperty>()?.StandardError;

switch (nodeState)
{
Expand All @@ -173,6 +183,7 @@ private static void GetTestNodeDetails(TestNodeUpdateMessage testNodeUpdateMessa

case PassedTestNodeStateProperty:
state = TestStates.Passed;
duration = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<TimingProperty>()?.GlobalTiming.Duration.Ticks;
reason = nodeState.Explanation;
break;

Expand All @@ -183,34 +194,42 @@ private static void GetTestNodeDetails(TestNodeUpdateMessage testNodeUpdateMessa

case FailedTestNodeStateProperty failedTestNodeStateProperty:
state = TestStates.Failed;
duration = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<TimingProperty>()?.GlobalTiming.Duration.Ticks;
reason = nodeState.Explanation;
errorMessage = failedTestNodeStateProperty.Exception?.Message;
errorStackTrace = failedTestNodeStateProperty.Exception?.StackTrace;
break;

case ErrorTestNodeStateProperty errorTestNodeStateProperty:
state = TestStates.Error;
duration = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<TimingProperty>()?.GlobalTiming.Duration.Ticks;
reason = nodeState.Explanation;
errorMessage = errorTestNodeStateProperty.Exception?.Message;
errorStackTrace = errorTestNodeStateProperty.Exception?.StackTrace;
break;

case TimeoutTestNodeStateProperty timeoutTestNodeStateProperty:
state = TestStates.Timeout;
duration = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<TimingProperty>()?.GlobalTiming.Duration.Ticks;
reason = nodeState.Explanation;
errorMessage = timeoutTestNodeStateProperty.Exception?.Message;
errorStackTrace = timeoutTestNodeStateProperty.Exception?.StackTrace;
break;

case CancelledTestNodeStateProperty cancelledTestNodeStateProperty:
state = TestStates.Cancelled;
duration = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<TimingProperty>()?.GlobalTiming.Duration.Ticks;
reason = nodeState.Explanation;
errorMessage = cancelledTestNodeStateProperty.Exception?.Message;
errorStackTrace = cancelledTestNodeStateProperty.Exception?.StackTrace;
break;
}

return new TestNodeDetails(state, duration, reason, errorMessage, errorStackTrace, standardOutput, standardError);
}

public record TestNodeDetails(byte? State, long? Duration, string? Reason, string? ErrorMessage, string? ErrorStackTrace, string? StandardOutput, string? StandardError);

public Task<bool> IsEnabledAsync() => Task.FromResult(true);

public async Task OnTestSessionStartingAsync(SessionUid sessionUid, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Microsoft.Testing.Platform.IPC.Models;

internal sealed record SuccessfulTestResultMessage(string? Uid, string? DisplayName, byte? State, string? Reason, string? SessionUid);
internal sealed record SuccessfulTestResultMessage(string? Uid, string? DisplayName, byte? State, long? Duration, string? Reason, string? StandardOutput, string? ErrorOutput, string? SessionUid);

internal sealed record FailedTestResultMessage(string? Uid, string? DisplayName, byte? State, string? Reason, string? ErrorMessage, string? ErrorStackTrace, string? SessionUid);
internal sealed record FailedTestResultMessage(string? Uid, string? DisplayName, byte? State, long? Duration, string? Reason, string? ErrorMessage, string? ErrorStackTrace, string? StandardOutput, string? ErrorOutput, string? SessionUid);

internal sealed record TestResultMessages(string? ExecutionId, SuccessfulTestResultMessage[] SuccessfulTestMessages, FailedTestResultMessage[] FailedTestMessages) : IRequest;
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,25 @@ internal static class SuccessfulTestResultMessageFieldsId
public const ushort Uid = 1;
public const ushort DisplayName = 2;
public const ushort State = 3;
public const ushort Reason = 4;
public const ushort SessionUid = 5;
public const ushort Duration = 4;
public const ushort Reason = 5;
public const ushort StandardOutput = 6;
public const ushort ErrorOutput = 7;
public const ushort SessionUid = 8;
}

internal static class FailedTestResultMessageFieldsId
{
public const ushort Uid = 1;
public const ushort DisplayName = 2;
public const ushort State = 3;
public const ushort Reason = 4;
public const ushort ErrorMessage = 5;
public const ushort ErrorStackTrace = 6;
public const ushort SessionUid = 7;
public const ushort Duration = 4;
public const ushort Reason = 5;
public const ushort ErrorMessage = 6;
public const ushort ErrorStackTrace = 7;
public const ushort StandardOutput = 8;
public const ushort ErrorOutput = 9;
public const ushort SessionUid = 10;
}

internal static class FileArtifactMessagesFieldsId
Expand Down
Loading

0 comments on commit 29f324d

Please sign in to comment.