From c2c01f0ba07f9dd5d42aa0ba619fd0f81b739927 Mon Sep 17 00:00:00 2001 From: Abhishek Kumawat Date: Fri, 5 Oct 2018 18:05:44 +0530 Subject: [PATCH] Unstable testId for nunit tests --- .../TrxLogger.cs | 15 ++++-- .../Utility/Constants.cs | 5 ++ .../TrxLoggerTests.cs | 54 +++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs index 730c9531d2..8ee9f235c7 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs @@ -543,12 +543,19 @@ private ITestElement GetOrCreateTestElement(Guid executionId, Guid parentExecuti return testElement; } - Guid testId = Converter.GetTestId(rockSteadyTestResult.TestCase); + TestCase testCase = rockSteadyTestResult.TestCase; + Guid testId = Converter.GetTestId(testCase); // Scenario for inner test case when parent test element is not present. - var testName = rockSteadyTestResult.TestCase.DisplayName; - if (parentTestElement == null && !string.IsNullOrEmpty(rockSteadyTestResult.DisplayName)) + var testName = testCase.DisplayName; + var adapter = testCase.ExecutorUri.ToString(); + if (adapter.Contains(TrxLoggerConstants.MstestAdapterString) && + parentTestElement == null && + !string.IsNullOrEmpty(rockSteadyTestResult.DisplayName)) { + // Note: For old mstest adapters hierarchical support was not present. Thus inner result of data driven was identified using test result display name. + // Non null test result display name means its a inner result of data driven/ordered test. + // Changing GUID to keep supporting old mstest adapters. testId = Guid.NewGuid(); testName = rockSteadyTestResult.DisplayName; } @@ -559,7 +566,7 @@ private ITestElement GetOrCreateTestElement(Guid executionId, Guid parentExecuti // Create test element if (testElement == null) { - testElement = Converter.ToTestElement(testId, executionId, parentExecutionId, testName, testType, rockSteadyTestResult.TestCase); + testElement = Converter.ToTestElement(testId, executionId, parentExecutionId, testName, testType, testCase); testElements.TryAdd(testId, testElement); } diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs index 420069e373..4f54b7a0d8 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs @@ -69,6 +69,11 @@ internal static class Constants /// public const string TmiTestIdPropertyIdentifier = "MSTestDiscoverer.TmiTestId"; + /// + /// Mstest adapter string + /// + public const string MstestAdapterString = "mstestadapter"; + /// /// Ordered test type guid /// diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs index 865ee466e8..51e16e0e5b 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs @@ -324,6 +324,18 @@ public void TestResultHandlerShouldAddFlatResultsIfParentTestResultIsNotPresent( Assert.AreEqual(this.testableTrxLogger.TestResultCount, 2, "TestResultHandler is not creating flat results when parent result is not present."); } + [TestMethod] + public void TestResultHandlerShouldChangeGuidAndDisplayNameForMsTestResultIfParentNotPresentButTestResultNamePresent() + { + this.ValidateTestIdAndNameInTrx(true); + } + + [TestMethod] + public void TestResultHandlerShouldNotChangeGuidAndDisplayNameForNonMsTestResultIfParentNotPresentButTestResultNamePresent() + { + this.ValidateTestIdAndNameInTrx(false); + } + [TestMethod] public void TestResultHandlerShouldAddHierarchicalResultsIfParentTestResultIsPresent() { @@ -695,6 +707,48 @@ private void ValidateDateTimeInTrx(string trxFileName) } } + private void ValidateTestIdAndNameInTrx(bool isMstestAdapter) + { + ObjectModel.TestCase testCase = CreateTestCase("TestCase"); + testCase.ExecutorUri = isMstestAdapter ? new Uri("some://mstestadapteruri") : new Uri("some://uri"); + + ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + result.SetPropertyValue(TrxLoggerConstants.ExecutionIdProperty, Guid.NewGuid()); + if (isMstestAdapter) + { + result.DisplayName = "testDisplayName"; + } + + Mock resultEventArg = new Mock(result); + this.testableTrxLogger.TestResultHandler(new object(), resultEventArg.Object); + var testRunCompleteEventArgs = TrxLoggerTests.CreateTestRunCompleteEventArgs(); + this.testableTrxLogger.TestRunCompleteHandler(new object(), testRunCompleteEventArgs); + + this.ValidateResultAttributesInTrx(this.testableTrxLogger.trxFile, testCase.Id, testCase.DisplayName, isMstestAdapter); + } + + private void ValidateResultAttributesInTrx(string trxFileName, Guid testId, string testName, bool isMstestAdapter) + { + using (FileStream file = File.OpenRead(trxFileName)) + { + using (XmlReader reader = XmlReader.Create(file)) + { + XDocument document = XDocument.Load(reader); + var resultNode = document.Descendants(document.Root.GetDefaultNamespace() + "UnitTestResult").FirstOrDefault(); + if (isMstestAdapter) + { + Assert.AreNotEqual(resultNode.Attributes("testId").FirstOrDefault().Value, testId.ToString()); + Assert.AreNotEqual(resultNode.Attributes("testName").FirstOrDefault().Value, testName); + } + else + { + Assert.AreEqual(resultNode.Attributes("testId").FirstOrDefault().Value, testId.ToString()); + Assert.AreEqual(resultNode.Attributes("testName").FirstOrDefault().Value, testName); + } + } + } + } + private void ValidateTimeWithinUtcLimits(DateTimeOffset dateTime) { Assert.IsTrue(dateTime.UtcDateTime.Subtract(DateTime.UtcNow) < new TimeSpan(0, 0, 0, 60));