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..aedac00a5f 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs @@ -573,44 +573,56 @@ private static string GetOwner(ObjectModel.TestCase rockSteadyTestCase) } /// - /// Gets TestMethod for given testCase name and its class name. + /// Gets test class name. /// - /// test case display name - /// rockSteady Test Case - /// The - private static TestMethod GetTestMethod(string testDisplayName, string testCaseName, string source) + /// Test name. + /// Fully qualified name. + /// Source. + /// Test class name. + 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; - } + 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) { - // If source is not valid file path, then className will continue to point default value. + ObjectModel.EqtTrace.Verbose("Converter: GetTestClassName: " + ex); } } - return new TestMethod(testDisplayName, className); + return className; } /// @@ -635,7 +647,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..8d4adca18a 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 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; using UriDataAttachment = VisualStudio.TestPlatform.ObjectModel.UriDataAttachment; @@ -60,6 +64,77 @@ 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() + { + 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" }); + + 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() + { + 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); + + 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)"; + + ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName); + } + + [TestMethod] + public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnDoesNotEndsWithTestName() + { + var expectedClassName = "TestProject1.Class1.TestMethod1(2, 3, 4"; + var fullyQualifiedName = "TestProject1.Class1.TestMethod1(2, 3, 4.0d)"; + var testName = "TestMethod1"; + + ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName); + } + + private void ValidateTestMethodProperties(string testName, string fullyQualifiedName, string expectedClassName) + { + 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; + + Assert.AreEqual(expectedClassName, unitTestElement.TestMethod.ClassName); + Assert.AreEqual(testName, unitTestElement.TestMethod.Name); + } + + private static TestCase CreateTestCase(string fullyQualifiedName) + { + return new TestPlatformObjectModel.TestCase(fullyQualifiedName, new Uri("some://uri"), "DummySourceFileName"); + } + private static void SetupForToCollectionEntries(out string tempDir, out List attachmentSets, out TestRun testRun, out string testResultsDirectory) {