From 5bb966a124a1f39c783f94483f2164831463abaa Mon Sep 17 00:00:00 2001 From: abhishkk Date: Fri, 6 Jul 2018 10:26:02 +0530 Subject: [PATCH 1/4] Trx logger class name fix --- .../ObjectModel/UnitTestElement.cs | 8 ++ .../Utility/Converter.cs | 58 ++++++------- .../TrxLoggerTests.cs | 35 -------- .../Utility/ConverterTests.cs | 84 ++++++++++++++++++- 4 files changed, 119 insertions(+), 66 deletions(-) diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/UnitTestElement.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/UnitTestElement.cs index b50a9ef1e1..aa13af40c4 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/UnitTestElement.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/ObjectModel/UnitTestElement.cs @@ -47,6 +47,14 @@ public override TestType TestType get { return Constants.UnitTestType; } } + /// + /// Gets the test method. + /// + public TestMethod TestMethod + { + get { return this.testMethod; } + } + /// /// Gets or sets the storage. /// diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs index 924a8ceb07..cfdabe2324 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs @@ -572,45 +572,46 @@ private static string GetOwner(ObjectModel.TestCase rockSteadyTestCase) return owner ?? string.Empty; } - /// - /// Gets TestMethod for given testCase name and its class name. - /// - /// test case display name - /// rockSteady Test Case - /// The - private static TestMethod GetTestMethod(string testDisplayName, string testCaseName, string source) + private static string GetTestClassName(string testName, string fullyQualifiedName, string source) { - string className = "DefaultClassName"; - if (testCaseName.Contains(".")) + var className = "DefaultClassName"; + + // In case, fullyQualifiedName ends with testName, className is checked within remaining value of fullyQualifiedName. + // Example: In case, testName = TestMethod1(2, 3, 4.0d) and fullyQualifiedName = TestProject1.Class1.TestMethod1(2, 3, 4.0d), className will be checked within 'TestProject1.Class1.' only + var nameToCheck = fullyQualifiedName.EndsWith(testName) ? + fullyQualifiedName.Substring(0, fullyQualifiedName.Length - testName.Length) : + fullyQualifiedName; + + // C# test case scenario. + if (nameToCheck.Contains(".")) { - className = testCaseName.Substring(0, testCaseName.LastIndexOf('.')); + return nameToCheck.Substring(0, nameToCheck.LastIndexOf('.')); } - else if (testCaseName.Contains("::")) + + // C++ test case scenario (we would have a "::" instead of a '.') + if (nameToCheck.Contains("::")) { - // if this is a C++ test case then we would have a "::" instaed of a '.' - className = testCaseName.Substring(0, testCaseName.LastIndexOf("::")); + className = nameToCheck.Substring(0, nameToCheck.LastIndexOf("::")); // rename for a consistent behaviour for all tests. - className = className.Replace("::", "."); + return className.Replace("::", "."); } - else + + // Ordered test, web test scenario (Setting class name as source name if FQDn doesnt have . or ::) + try { - // Setting class name as source name if FQDn doesnt have . or :: [E.g. ordered test, web test] - try + string testCaseSource = Path.GetFileNameWithoutExtension(source); + if (!String.IsNullOrEmpty(testCaseSource)) { - string testCaseSource = Path.GetFileNameWithoutExtension(source); - if (!String.IsNullOrEmpty(testCaseSource)) - { - className = testCaseSource; - } - } - catch (ArgumentException) - { - // If source is not valid file path, then className will continue to point default value. + return testCaseSource; } } + catch (ArgumentException) + { + // If source is not valid file path, then className will continue to point default value. + } - return new TestMethod(testDisplayName, className); + return className; } /// @@ -635,7 +636,8 @@ private static TestElement CreateTestElement(Guid testId, string name, string fu else { var codeBase = source; - var testMethod = GetTestMethod(name, fullyQualifiedName, source); + var className = GetTestClassName(name, fullyQualifiedName, source); + var testMethod = new TestMethod(name, className); testElement = new UnitTestElement(testId, name, adapter, testMethod); (testElement as UnitTestElement).CodeBase = codeBase; diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs index a58701d466..8ad0b14d98 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs @@ -636,41 +636,6 @@ public void GetCustomPropertyValueFromTestCaseShouldReadCategoyrAttributesFromTe CollectionAssert.AreEqual(listCategoriesExpected, listCategoriesActual); } - /// - /// Unit test for assigning or populating test categories read to the unit test element. - /// - [TestMethod] - public void ToTestElementShouldAssignTestCategoryOfUnitTestElement() - { - ObjectModel.TestCase testCase = CreateTestCase("TestCase1"); - ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); - TestProperty testProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject)); - - testCase.SetPropertyValue(testProperty, new[] { "AsmLevel", "ClassLevel", "MethodLevel" }); - - var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase); - - object[] expected = new[] { "MethodLevel", "ClassLevel", "AsmLevel" }; - - CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray().OrderByDescending(x => x.ToString()).ToArray()); - } - - /// - /// Unit test for regression when there's no test categories. - /// - [TestMethod] - public void ToTestElementShouldNotFailWhenThereIsNoTestCategoreis() - { - ObjectModel.TestCase testCase = CreateTestCase("TestCase1"); - ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); - - var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase); - - object[] expected = Enumerable.Empty().ToArray(); - - CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray()); - } - [TestMethod] public void CRLFCharactersShouldGetRetainedInTrx() { diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs index 22f1848692..e8486b48e3 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs @@ -3,14 +3,18 @@ namespace Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests.Utility { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Xml; using Microsoft.TestPlatform.Extensions.TrxLogger.Utility; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; using ObjectModel; - using System; - using System.Collections.Generic; - using System.IO; + using ObjectModel = Microsoft.VisualStudio.TestPlatform.ObjectModel; using TestOutcome = VisualStudio.TestPlatform.ObjectModel.TestOutcome; + using TrxLoggerConstants = Microsoft.TestPlatform.Extensions.TrxLogger.Utility.Constants; using TrxLoggerOutcome = Microsoft.TestPlatform.Extensions.TrxLogger.ObjectModel.TestOutcome; using UriDataAttachment = VisualStudio.TestPlatform.ObjectModel.UriDataAttachment; @@ -60,6 +64,80 @@ public void ToCollectionEntriesShouldRenameAttachmentUriIfTheAttachmentNameIsSam Directory.Delete(tempDir, true); } + /// + /// Unit test for assigning or populating test categories read to the unit test element. + /// + [TestMethod] + public void ToTestElementShouldAssignTestCategoryOfUnitTestElement() + { + ObjectModel.TestCase testCase = CreateTestCase("TestCase1"); + ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + TestProperty testProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject)); + + testCase.SetPropertyValue(testProperty, new[] { "AsmLevel", "ClassLevel", "MethodLevel" }); + + var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase); + + object[] expected = new[] { "MethodLevel", "ClassLevel", "AsmLevel" }; + + CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray().OrderByDescending(x => x.ToString()).ToArray()); + } + + /// + /// Unit test for regression when there's no test categories. + /// + [TestMethod] + public void ToTestElementShouldNotFailWhenThereIsNoTestCategoreis() + { + ObjectModel.TestCase testCase = CreateTestCase("TestCase1"); + ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + + var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase); + + object[] expected = Enumerable.Empty().ToArray(); + + CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray()); + } + + [TestMethod] + public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnEndsWithTestName() + { + var expectedClassName = "TestProject1.Class1"; + var fullyQualifiedName = expectedClassName + "." + "TestMethod1(2, 3, 4.0d)"; + var testName = "TestMethod1(2, 3, 4.0d)"; + + ObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); + + ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + + var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement; + + Assert.AreEqual(expectedClassName, unitTestElement.TestMethod.ClassName); + Assert.AreEqual(testName, unitTestElement.TestMethod.Name); + } + + [TestMethod] + public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnDoesNotEndsWithTestName() + { + var expectedClassName = "TestProject1.Class1.TestMethod1(2, 3, 4"; + var fullyQualifiedName = "TestProject1.Class1.TestMethod1(2, 3, 4.0d)"; + var testName = "TestMethod1"; + + ObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); + + ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + + var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement; + + Assert.AreEqual(expectedClassName, unitTestElement.TestMethod.ClassName); + Assert.AreEqual(testName, unitTestElement.TestMethod.Name); + } + + private static TestCase CreateTestCase(string fullyQualifiedName) + { + return new ObjectModel.TestCase(fullyQualifiedName, new Uri("some://uri"), "DummySourceFileName"); + } + private static void SetupForToCollectionEntries(out string tempDir, out List attachmentSets, out TestRun testRun, out string testResultsDirectory) { From 72d3b9c8f975948bbfa86767598c35476ee64203 Mon Sep 17 00:00:00 2001 From: abhishkk Date: Fri, 6 Jul 2018 14:02:18 +0530 Subject: [PATCH 2/4] review comments --- .../Utility/Converter.cs | 6 +++++- .../Utility/ConverterTests.cs | 15 ++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs index cfdabe2324..628f85eaaa 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs @@ -606,9 +606,13 @@ private static string GetTestClassName(string testName, string fullyQualifiedNam return testCaseSource; } } - catch (ArgumentException) + catch (ArgumentException ex) { // If source is not valid file path, then className will continue to point default value. + if (ObjectModel.EqtTrace.IsVerboseEnabled) + { + ObjectModel.EqtTrace.Verbose("Converter: GetTestClassName: " + ex); + } } return className; diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs index e8486b48e3..8b61568c52 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs @@ -106,14 +106,7 @@ public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnEndsWithT var fullyQualifiedName = expectedClassName + "." + "TestMethod1(2, 3, 4.0d)"; var testName = "TestMethod1(2, 3, 4.0d)"; - ObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); - - ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); - - var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement; - - Assert.AreEqual(expectedClassName, unitTestElement.TestMethod.ClassName); - Assert.AreEqual(testName, unitTestElement.TestMethod.Name); + ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName); } [TestMethod] @@ -123,8 +116,12 @@ public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnDoesNotEn var fullyQualifiedName = "TestProject1.Class1.TestMethod1(2, 3, 4.0d)"; var testName = "TestMethod1"; - ObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); + ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName); + } + private void ValidateTestMethodProperties(string testName, string fullyQualifiedName, string expectedClassName) + { + ObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement; From 391ffbf4c61155d9b01ecd131379858d2ac8e7ff Mon Sep 17 00:00:00 2001 From: Abhishek Kumawat Date: Fri, 6 Jul 2018 18:24:38 +0530 Subject: [PATCH 3/4] Method Comments --- .../Utility/Converter.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs index 628f85eaaa..aedac00a5f 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs @@ -572,6 +572,13 @@ private static string GetOwner(ObjectModel.TestCase rockSteadyTestCase) return owner ?? string.Empty; } + /// + /// Gets test class name. + /// + /// Test name. + /// Fully qualified name. + /// Source. + /// Test class name. private static string GetTestClassName(string testName, string fullyQualifiedName, string source) { var className = "DefaultClassName"; From 7e34bd9d99066f791d4f3a40af717ae68b7d76cd Mon Sep 17 00:00:00 2001 From: abhishkk Date: Tue, 10 Jul 2018 15:59:49 +0530 Subject: [PATCH 4/4] UTs fix --- .../Utility/ConverterTests.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs index 8b61568c52..8d4adca18a 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs @@ -12,7 +12,7 @@ namespace Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests.Utility using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; using ObjectModel; - using ObjectModel = Microsoft.VisualStudio.TestPlatform.ObjectModel; + using TestPlatformObjectModel = Microsoft.VisualStudio.TestPlatform.ObjectModel; using TestOutcome = VisualStudio.TestPlatform.ObjectModel.TestOutcome; using TrxLoggerConstants = Microsoft.TestPlatform.Extensions.TrxLogger.Utility.Constants; using TrxLoggerOutcome = Microsoft.TestPlatform.Extensions.TrxLogger.ObjectModel.TestOutcome; @@ -70,8 +70,8 @@ public void ToCollectionEntriesShouldRenameAttachmentUriIfTheAttachmentNameIsSam [TestMethod] public void ToTestElementShouldAssignTestCategoryOfUnitTestElement() { - ObjectModel.TestCase testCase = CreateTestCase("TestCase1"); - ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + TestPlatformObjectModel.TestCase testCase = CreateTestCase("TestCase1"); + TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase); TestProperty testProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject)); testCase.SetPropertyValue(testProperty, new[] { "AsmLevel", "ClassLevel", "MethodLevel" }); @@ -89,8 +89,8 @@ public void ToTestElementShouldAssignTestCategoryOfUnitTestElement() [TestMethod] public void ToTestElementShouldNotFailWhenThereIsNoTestCategoreis() { - ObjectModel.TestCase testCase = CreateTestCase("TestCase1"); - ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + TestPlatformObjectModel.TestCase testCase = CreateTestCase("TestCase1"); + TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase); var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase); @@ -121,8 +121,8 @@ public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnDoesNotEn private void ValidateTestMethodProperties(string testName, string fullyQualifiedName, string expectedClassName) { - ObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); - ObjectModel.TestResult result = new ObjectModel.TestResult(testCase); + TestPlatformObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); + TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase); var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement; @@ -132,7 +132,7 @@ private void ValidateTestMethodProperties(string testName, string fullyQualified private static TestCase CreateTestCase(string fullyQualifiedName) { - return new ObjectModel.TestCase(fullyQualifiedName, new Uri("some://uri"), "DummySourceFileName"); + return new TestPlatformObjectModel.TestCase(fullyQualifiedName, new Uri("some://uri"), "DummySourceFileName"); } private static void SetupForToCollectionEntries(out string tempDir, out List attachmentSets, out TestRun testRun,