From f1b8d769db81712339698a414b0f887521df9652 Mon Sep 17 00:00:00 2001 From: Evangelink <11340282+Evangelink@users.noreply.github.com> Date: Fri, 22 May 2026 15:21:51 +0200 Subject: [PATCH] Fix flaky TimingProperty.ToString test by using an explicit format string DateTimeOffset.ToString(IFormatProvider) builds the result from the culture's GeneralLongTimePattern and then appends ' zzz' itself. Under certain runtime/ICU configurations the base pattern can already contain zzz, producing duplicated offsets like '+00:00 +00:00' and failing TimingProperty_ToStringIsCorrect. Switch TimingInfo.ToString to an explicit 'MM/dd/yyyy HH:mm:ss zzz' format so the output is fully deterministic, and remove the now-unnecessary CultureInfo.CurrentCulture mutation from the tests (which was itself a source of cross-test races and did not protect the InvariantCulture path). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Messages/TimingProperties.cs | 4 +- .../Messages/TestNodePropertiesTests.cs | 42 ++++++------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Platform/Messages/TimingProperties.cs b/src/Platform/Microsoft.Testing.Platform/Messages/TimingProperties.cs index 95176efb8c..bd0af14cec 100644 --- a/src/Platform/Microsoft.Testing.Platform/Messages/TimingProperties.cs +++ b/src/Platform/Microsoft.Testing.Platform/Messages/TimingProperties.cs @@ -42,9 +42,9 @@ public override string ToString() var builder = new StringBuilder(); builder.Append($"{nameof(TimingInfo)} {{ "); builder.Append($"{nameof(StartTime)} = "); - builder.Append(StartTime.ToString(CultureInfo.InvariantCulture)); + builder.Append(StartTime.ToString("MM/dd/yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture)); builder.Append($", {nameof(EndTime)} = "); - builder.Append(EndTime.ToString(CultureInfo.InvariantCulture)); + builder.Append(EndTime.ToString("MM/dd/yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture)); builder.Append($", {nameof(Duration)} = "); builder.Append(Duration.ToString("c", CultureInfo.InvariantCulture)); builder.Append(" }"); diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Messages/TestNodePropertiesTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Messages/TestNodePropertiesTests.cs index 0fe51764dd..56c0c53ad3 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Messages/TestNodePropertiesTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Messages/TestNodePropertiesTests.cs @@ -155,41 +155,23 @@ public void CancelledTestNodeStateProperty_WhenExceptionAndNoExplanation_ToStrin [TestMethod] public void TimingProperty_ToStringIsCorrect() { - CultureInfo originalCulture = CultureInfo.CurrentCulture; - try - { - CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; - DateTimeOffset startTime = new(2021, 9, 1, 0, 0, 0, default); - DateTimeOffset endTime = new(2021, 9, 1, 1, 2, 3, default); - TimeSpan duration = endTime - startTime; - Assert.AreEqual( - "TimingProperty { GlobalTiming = TimingInfo { StartTime = 09/01/2021 00:00:00 +00:00, EndTime = 09/01/2021 01:02:03 +00:00, Duration = 01:02:03 }, StepTimings = [] }", - new TimingProperty(new(startTime, endTime, duration)).ToString()); - } - finally - { - CultureInfo.CurrentCulture = originalCulture; - } + DateTimeOffset startTime = new(2021, 9, 1, 0, 0, 0, default); + DateTimeOffset endTime = new(2021, 9, 1, 1, 2, 3, default); + TimeSpan duration = endTime - startTime; + Assert.AreEqual( + "TimingProperty { GlobalTiming = TimingInfo { StartTime = 09/01/2021 00:00:00 +00:00, EndTime = 09/01/2021 01:02:03 +00:00, Duration = 01:02:03 }, StepTimings = [] }", + new TimingProperty(new(startTime, endTime, duration)).ToString()); } [TestMethod] public void TimingProperty_WithStepTimings_ToStringIsCorrect() { - CultureInfo originalCulture = CultureInfo.CurrentCulture; - try - { - CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; - DateTimeOffset startTime = new(2021, 9, 1, 0, 0, 0, default); - DateTimeOffset endTime = new(2021, 9, 1, 1, 2, 3, default); - TimeSpan duration = endTime - startTime; - Assert.AreEqual( - "TimingProperty { GlobalTiming = TimingInfo { StartTime = 09/01/2021 00:00:00 +00:00, EndTime = 09/01/2021 01:02:03 +00:00, Duration = 01:02:03 }, StepTimings = [StepTimingInfo { Id = run, Description = some description, Timing = TimingInfo { StartTime = 09/01/2021 00:00:00 +00:00, EndTime = 09/01/2021 01:02:03 +00:00, Duration = 01:02:03 } }] }", - new TimingProperty(new(startTime, endTime, duration), [new("run", "some description", new(startTime, endTime, duration))]).ToString()); - } - finally - { - CultureInfo.CurrentCulture = originalCulture; - } + DateTimeOffset startTime = new(2021, 9, 1, 0, 0, 0, default); + DateTimeOffset endTime = new(2021, 9, 1, 1, 2, 3, default); + TimeSpan duration = endTime - startTime; + Assert.AreEqual( + "TimingProperty { GlobalTiming = TimingInfo { StartTime = 09/01/2021 00:00:00 +00:00, EndTime = 09/01/2021 01:02:03 +00:00, Duration = 01:02:03 }, StepTimings = [StepTimingInfo { Id = run, Description = some description, Timing = TimingInfo { StartTime = 09/01/2021 00:00:00 +00:00, EndTime = 09/01/2021 01:02:03 +00:00, Duration = 01:02:03 } }] }", + new TimingProperty(new(startTime, endTime, duration), [new("run", "some description", new(startTime, endTime, duration))]).ToString()); } [TestMethod]