From 51509a0875eb09058612e5c2dc84900147dbf448 Mon Sep 17 00:00:00 2001 From: Enji Eid Date: Mon, 9 Sep 2024 12:37:41 +0200 Subject: [PATCH] fix and added test --- .../TrxDataConsumer.cs | 8 ++++++- .../TrxProcessLifetimeHandler.cs | 4 ++-- .../TrxReportEngine.cs | 9 ++++--- .../TrxTests.cs | 24 ++++++++++++++++--- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxDataConsumer.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxDataConsumer.cs index 8c97377ef5..be5a52e878 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxDataConsumer.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxDataConsumer.cs @@ -49,6 +49,7 @@ internal sealed class TrxReportGenerator : private int _failedTestsCount; private int _passedTestsCount; private int _notExecutedTestsCount; + private int _timeoutTestsCount; private bool _adapterSupportTrxCapability; public TrxReportGenerator( @@ -121,6 +122,11 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo _tests.Add(nodeChangedMessage); _passedTestsCount++; } + else if (nodeState is TimeoutTestNodeStateProperty) + { + _tests.Add(nodeChangedMessage); + _timeoutTestsCount++; + } else if (Array.IndexOf(TestNodePropertiesCategories.WellKnownTestNodeTestRunOutcomeFailedProperties, nodeState.GetType()) != -1) { _tests.Add(nodeChangedMessage); @@ -227,7 +233,7 @@ public async Task OnTestSessionFinishingAsync(SessionUid sessionUid, Cancellatio ApplicationStateGuard.Ensure(_testStartTime is not null); TrxReportEngine trxReportGeneratorEngine = new(_testApplicationModuleInfo, _environment, _commandLineOptionsService, _configuration, - _clock, _tests.ToArray(), _failedTestsCount, _passedTestsCount, _notExecutedTestsCount, _artifactsByExtension, _artifactsByTestNode, + _clock, _tests.ToArray(), _failedTestsCount, _passedTestsCount, _notExecutedTestsCount, _timeoutTestsCount, _artifactsByExtension, _artifactsByTestNode, _adapterSupportTrxCapability, _testFramework, _testStartTime.Value, cancellationToken); string reportFileName = await trxReportGeneratorEngine.GenerateReportAsync(); diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs index 6b7141275f..f39e09df1c 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs @@ -158,7 +158,7 @@ public async Task OnTestHostProcessExitedAsync(ITestHostProcessInformation testH if (!testHostProcessInformation.HasExitedGracefully) { TrxReportEngine trxReportGeneratorEngine = new(_testApplicationModuleInfo, _environment, _commandLineOptions, _configuration, - _clock, [], 0, 0, 0, + _clock, [], 0, 0, 0, 0, artifacts, new Dictionary>(), adapterSupportTrxCapability: null, @@ -188,7 +188,7 @@ await _messageBus.PublishAsync( if (_fileArtifacts.Count > 0) { TrxReportEngine trxReportGeneratorEngine = new(_testApplicationModuleInfo, _environment, _commandLineOptions, _configuration, - _clock, [], 0, 0, 0, + _clock, [], 0, 0, 0, 0, artifacts, new Dictionary>(), false, diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportEngine.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportEngine.cs index 642eb9b504..03ecbd1941 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportEngine.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportEngine.cs @@ -95,6 +95,7 @@ internal sealed partial class TrxReportEngine private readonly int _failedTestsCount; private readonly int _passedTestsCount; private readonly int _notExecutedTestsCount; + private readonly int _timeoutTestsCount; private readonly Dictionary> _artifactsByExtension; private readonly Dictionary> _artifactsByTestNode; private readonly bool? _adapterSupportTrxCapability; @@ -105,7 +106,7 @@ internal sealed partial class TrxReportEngine private readonly IFileSystem _fileSystem; private readonly bool _isCopyingFileAllowed; - public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, Dictionary> artifactsByExtension, Dictionary> artifactsByTestNode, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, CancellationToken cancellationToken) + public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, int timeoutTestsCount, Dictionary> artifactsByExtension, Dictionary> artifactsByTestNode, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, CancellationToken cancellationToken) : this( new SystemFileSystem(), testApplicationModuleInfo, @@ -117,6 +118,7 @@ public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEn failedTestsCount, passedTestsCount, notExecutedTestsCount, + timeoutTestsCount, artifactsByExtension, artifactsByTestNode, adapterSupportTrxCapability, @@ -126,7 +128,7 @@ public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEn { } - public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, Dictionary> artifactsByExtension, Dictionary> artifactsByTestNode, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, CancellationToken cancellationToken, bool isCopyingFileAllowed = true) + public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, int timeoutTestsCount, Dictionary> artifactsByExtension, Dictionary> artifactsByTestNode, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, CancellationToken cancellationToken, bool isCopyingFileAllowed = true) { _testApplicationModuleInfo = testApplicationModuleInfo; _environment = environment; @@ -137,6 +139,7 @@ public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testAp _failedTestsCount = failedTestsCount; _passedTestsCount = passedTestsCount; _notExecutedTestsCount = notExecutedTestsCount; + _timeoutTestsCount = timeoutTestsCount; _artifactsByExtension = artifactsByExtension; _artifactsByTestNode = artifactsByTestNode; _adapterSupportTrxCapability = adapterSupportTrxCapability; @@ -300,7 +303,7 @@ private async Task AddResultSummaryAsync(XElement testRun, string resultSummaryO new XAttribute("passed", _passedTestsCount), new XAttribute("failed", _failedTestsCount), new XAttribute("error", 0), - new XAttribute("timeout", 0), + new XAttribute("timeout", _timeoutTestsCount), new XAttribute("aborted", 0), new XAttribute("inconclusive", 0), new XAttribute("passedButRunAborted", 0), diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxTests.cs index db000bc4aa..ca137802fe 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxTests.cs @@ -69,6 +69,24 @@ public async Task TrxReportEngine_GenerateReportAsyncWithNotExecutedTests_TrxExe Assert.IsTrue(trxContent.Contains(@"notExecuted=""1""")); } + public async Task TrxReportEngine_GenerateReportAsyncWithTimeoutTests_TrxTimeoutTestsCountHasIt() + { + // Arrange + using MemoryFileStream memoryStream = new(); + PropertyBag propertyBag = new(new PassedTestNodeStateProperty()); + TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream, timeoutTestsCount: 1); + + // Act + string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true); + + // Assert + AssertExpectedTrxFileName(fileName); + XDocument xml = GetTrxContent(memoryStream); + AssertTrxOutcome(xml, "Completed"); + string trxContent = xml.ToString(); + Assert.IsTrue(trxContent.Contains(@"timeout=""1""")); + } + public async Task TrxReportEngine_GenerateReportAsync_WithArgumentTrxReportFileName_FileIsCorrectlyGenerated() { // Arrange @@ -377,7 +395,7 @@ public async Task TrxReportEngine_GenerateReportAsync_FileAlreadyExists_WillRetr _ = _environmentMock.SetupGet(_ => _.MachineName).Returns("MachineName"); _ = _testApplicationModuleInfoMock.Setup(_ => _.GetCurrentTestApplicationFullPath()).Returns("TestAppPath"); TrxReportEngine trxReportEngine = new(_fileSystem.Object, _testApplicationModuleInfoMock.Object, _environmentMock.Object, _commandLineOptionsMock.Object, - _configurationMock.Object, _clockMock.Object, [], 0, 0, 0, + _configurationMock.Object, _clockMock.Object, [], 0, 0, 0, 0, _artifactsByExtension, _artifactsByTestNode, true, _testFrameworkMock.Object, DateTime.UtcNow, CancellationToken.None, isCopyingFileAllowed: false); @@ -412,7 +430,7 @@ private static void AssertExpectedTrxFileName(string fileName) => Assert.IsTrue(fileName.Equals("_MachineName_0001-01-01_00_00_00.000.trx", StringComparison.Ordinal)); private TrxReportEngine GenerateTrxReportEngine(int passedTestsCount, int failedTestsCount, PropertyBag propertyBag, MemoryFileStream memoryStream, - bool? adapterSupportTrxCapability = null, int notExecutedTestsCount = 0) + bool? adapterSupportTrxCapability = null, int notExecutedTestsCount = 0, int timeoutTestsCount = 0) { var testNode = new TestNodeUpdateMessage( new SessionUid("1"), @@ -432,7 +450,7 @@ private TrxReportEngine GenerateTrxReportEngine(int passedTestsCount, int failed _ = _testApplicationModuleInfoMock.Setup(_ => _.GetCurrentTestApplicationFullPath()).Returns("TestAppPath"); return new TrxReportEngine(_fileSystem.Object, _testApplicationModuleInfoMock.Object, _environmentMock.Object, _commandLineOptionsMock.Object, - _configurationMock.Object, _clockMock.Object, testNodeUpdatedMessages, failedTestsCount, passedTestsCount, notExecutedTestsCount, + _configurationMock.Object, _clockMock.Object, testNodeUpdatedMessages, failedTestsCount, passedTestsCount, notExecutedTestsCount, timeoutTestsCount, _artifactsByExtension, _artifactsByTestNode, adapterSupportTrxCapability, _testFrameworkMock.Object, testStartTime, cancellationToken, isCopyingFileAllowed: false); }