Skip to content

Commit

Permalink
Add StartTime and EndTime to VSTest TestResult
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jul 13, 2024
1 parent 9e3af28 commit 3ad2641
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<NerdbankGitVersioningVersion>3.6.133</NerdbankGitVersioningVersion>
<NSubstituteVersion>5.1.0</NSubstituteVersion>
<TunnelVisionLabsReferenceAssemblyAnnotatorVersion>1.0.0-alpha.160</TunnelVisionLabsReferenceAssemblyAnnotatorVersion>
<XunitAnalyzersVersion>1.15.0-pre.12</XunitAnalyzersVersion>
<XunitV2Version>2.8.2-pre.12</XunitV2Version>
<XunitV3Version>0.2.0-pre.5</XunitV3Version>
<XunitAnalyzersVersion>1.16.0-pre.2</XunitAnalyzersVersion>
<XunitV2Version>2.9.1-pre.5</XunitV2Version>
<XunitV3Version>0.2.0-pre.25</XunitV3Version>
</PropertyGroup>

</Project>
48 changes: 31 additions & 17 deletions src/xunit.runner.visualstudio/Sinks/VsExecutionSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public sealed class VsExecutionSink : TestMessageSink, IDisposable
readonly IMessageSink innerSink;
readonly MessageMetadataCache metadataCache = new();
readonly ITestExecutionRecorder recorder;
readonly ConcurrentDictionary<string, DateTimeOffset> startTimeByTestID = [];
readonly ConcurrentDictionary<string, List<TestCaseStarting>> testCasesByAssemblyID = [];
readonly ConcurrentDictionary<string, TestCaseStarting> testCasesByCaseID = new();
readonly ConcurrentDictionary<string, TestCaseStarting> testCasesByCaseID = [];
readonly ConcurrentDictionary<string, List<TestCaseStarting>> testCasesByClassID = [];
readonly ConcurrentDictionary<string, List<TestCaseStarting>> testCasesByCollectionID = [];
readonly ConcurrentDictionary<string, List<TestCaseStarting>> testCasesByMethodID = [];
Expand Down Expand Up @@ -280,7 +281,8 @@ void HandleTestCollectionStarting(MessageHandlerArgs<TestCollectionStarting> arg
void HandleTestFailed(MessageHandlerArgs<TestFailed> args)
{
var testFailed = args.Message;
var result = MakeVsTestResult(TestOutcome.Failed, testFailed);
startTimeByTestID.TryRemove(testFailed.TestUniqueID, out var startTime);
var result = MakeVsTestResult(TestOutcome.Failed, testFailed, startTime);
if (result is not null)
{
result.ErrorMessage = ExceptionUtility.CombineMessages(testFailed);
Expand Down Expand Up @@ -324,7 +326,8 @@ void HandleTestMethodStarting(MessageHandlerArgs<TestMethodStarting> args) =>
void HandleTestNotRun(MessageHandlerArgs<TestNotRun> args)
{
var testNotRun = args.Message;
var result = MakeVsTestResult(TestOutcome.None, testNotRun);
startTimeByTestID.TryRemove(testNotRun.TestUniqueID, out var startTime);
var result = MakeVsTestResult(TestOutcome.None, testNotRun, startTime);
if (result is not null)
TryAndReport("RecordResult (None)", testNotRun, () => recorder.RecordResult(result));
else
Expand All @@ -336,7 +339,8 @@ void HandleTestNotRun(MessageHandlerArgs<TestNotRun> args)
void HandleTestPassed(MessageHandlerArgs<TestPassed> args)
{
var testPassed = args.Message;
var result = MakeVsTestResult(TestOutcome.Passed, testPassed);
startTimeByTestID.TryRemove(testPassed.TestUniqueID, out var startTime);
var result = MakeVsTestResult(TestOutcome.Passed, testPassed, startTime);
if (result is not null)
TryAndReport("RecordResult (Pass)", testPassed, () => recorder.RecordResult(result));
else
Expand All @@ -348,7 +352,8 @@ void HandleTestPassed(MessageHandlerArgs<TestPassed> args)
void HandleTestSkipped(MessageHandlerArgs<TestSkipped> args)
{
var testSkipped = args.Message;
var result = MakeVsTestResult(TestOutcome.Skipped, testSkipped);
startTimeByTestID.TryRemove(testSkipped.TestUniqueID, out var startTime);
var result = MakeVsTestResult(TestOutcome.Skipped, testSkipped, startTime);
if (result is not null)
TryAndReport("RecordResult (Skip)", testSkipped, () => recorder.RecordResult(result));
else
Expand All @@ -357,14 +362,13 @@ void HandleTestSkipped(MessageHandlerArgs<TestSkipped> args)
HandleCancellation(args);
}

void HandleTestStarting(MessageHandlerArgs<TestStarting> args) =>
metadataCache.Set(args.Message);
void HandleTestStarting(MessageHandlerArgs<TestStarting> args)
{
var starting = args.Message;

//void LogError(
// TestAssemblyMessage msg,
// string format,
// params object?[] args) =>
// LogError(TestAssemblyPath(msg), format, args);
metadataCache.Set(starting);
startTimeByTestID.TryAdd(starting.TestUniqueID, starting.StartTime);
}

void LogError(
string assemblyPath,
Expand All @@ -380,13 +384,15 @@ public void LogWarning(

VsTestResult? MakeVsTestResult(
TestOutcome outcome,
XunitTestResultMessage testResult) =>
MakeVsTestResult(outcome, testResult.TestCaseUniqueID, TestDisplayName(testResult), (double)testResult.ExecutionTime, testResult.Output);
XunitTestResultMessage testResult,
DateTimeOffset? startTime) =>
MakeVsTestResult(outcome, testResult.TestCaseUniqueID, TestDisplayName(testResult), (double)testResult.ExecutionTime, testResult.Output, startTime: startTime, finishTime: testResult.FinishTime);

VsTestResult? MakeVsTestResult(
TestOutcome outcome,
TestSkipped skippedResult) =>
MakeVsTestResult(outcome, skippedResult.TestCaseUniqueID, TestDisplayName(skippedResult), (double)skippedResult.ExecutionTime, errorMessage: skippedResult.Reason);
TestSkipped skippedResult,
DateTimeOffset? startTime) =>
MakeVsTestResult(outcome, skippedResult.TestCaseUniqueID, TestDisplayName(skippedResult), (double)skippedResult.ExecutionTime, errorMessage: skippedResult.Reason, startTime: startTime, finishTime: skippedResult.FinishTime);

VsTestResult? MakeVsTestResult(
TestOutcome outcome,
Expand All @@ -405,7 +411,9 @@ public void LogWarning(
string displayName,
double executionTime = 0.0,
string? output = null,
string? errorMessage = null)
string? errorMessage = null,
DateTimeOffset? startTime = null,
DateTimeOffset? finishTime = null)
{
var vsTestCase = FindTestCase(testCaseUniqueID);
if (vsTestCase is null)
Expand All @@ -419,6 +427,12 @@ public void LogWarning(
Outcome = outcome,
};

if (startTime.HasValue && finishTime.HasValue)
{
result.StartTime = startTime.Value;
result.EndTime = finishTime.Value;
}

// Work around VS considering a test "not run" when the duration is 0
if (result.Duration.TotalMilliseconds == 0)
result.Duration = TimeSpan.FromMilliseconds(1);
Expand Down

0 comments on commit 3ad2641

Please sign in to comment.