From 53da99a8a1ed39d1df63429fc2d61011fb57fb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Thu, 5 Mar 2020 10:48:06 +0100 Subject: [PATCH] Fix splitting of test name from fully qualified name (#2355) * Fix splitting of test name from fully qualified name * Remove the same implementation in tests --- .../Utility/Converter.cs | 2 +- .../Utility/ConverterTests.cs | 38 +++++++++++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs index e67ca4fb9b..808feec7e2 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs @@ -684,7 +684,7 @@ private TestElement CreateTestElement(Guid testId, string name, string fullyQual { var codeBase = source; var className = GetTestClassName(name, fullyQualifiedName, source); - var testMethodName = fullyQualifiedName.StartsWith(className) ? fullyQualifiedName.Remove(0, $"{className}.".Length) : fullyQualifiedName; + var testMethodName = fullyQualifiedName.StartsWith($"{className}.") ? fullyQualifiedName.Remove(0, $"{className}.".Length) : fullyQualifiedName; var testMethod = new TestMethod(testMethodName, className); testElement = new UnitTestElement(testId, name, adapter, testMethod); diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs index 6b8cc39ed8..32dd8ea959 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/Utility/ConverterTests.cs @@ -116,30 +116,33 @@ public void ToTestElementShouldNotFailWhenThereIsNoTestCategoreis() public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnIsSameAsTestName() { var expectedClassName = "TestProject1.Class1"; - var fullyQualifiedName = expectedClassName + "." + "TestMethod1"; + var expectedTestName = "TestMethod1"; + var fullyQualifiedName = expectedClassName + "." + expectedTestName; var testName = "TestProject1.Class1.TestMethod1"; - ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName); + ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName, expectedTestName); } [TestMethod] public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnEndsWithTestName() { var expectedClassName = "TestProject1.Class1"; - var fullyQualifiedName = expectedClassName + "." + "TestMethod1(2, 3, 4.0d)"; + var expectedTestName = "TestMethod1(2, 3, 4.0d)"; + var fullyQualifiedName = expectedClassName + "." + expectedTestName; var testName = "TestMethod1(2, 3, 4.0d)"; - ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName); + ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName, expectedTestName); } [TestMethod] public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnDoesNotEndsWithTestName() { var expectedClassName = "TestProject1.Class1.TestMethod1(2, 3, 4"; - var fullyQualifiedName = "TestProject1.Class1.TestMethod1(2, 3, 4.0d)"; + var expectedTestName = "0d)"; + var fullyQualifiedName = "TestProject1.Class1.TestMethod1(2, 3, 4." + expectedTestName; var testName = "TestMethod1"; - ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName); + ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName, expectedTestName); } [TestMethod] @@ -161,13 +164,32 @@ public void ToResultFilesShouldAddAttachementsWithRelativeURI() Assert.IsTrue(resultFiles[0].Contains("abc.txt")); } - private void ValidateTestMethodProperties(string testName, string fullyQualifiedName, string expectedClassName) + [TestMethod] + public void ToTestElementShouldNotFailWhenClassNameIsTheSameAsFullyQualifiedName() + { + // the converter assumed to find 'classname' in the fqn and split it on 'classname.' + // but that threw an exception because 'classname.' is not contained in 'classname' + // (notice the . at the end) + // we should not be assuming that the fqn will have '.' in them + // seen it for example with qtest + + string expectedClassName, expectedTestName, fullyQualifiedName, source, testName; + expectedClassName = expectedTestName = fullyQualifiedName = source = testName = "test1"; + + TestPlatformObjectModel.TestCase testCase = new TestPlatformObjectModel.TestCase(fullyQualifiedName, new Uri("some://uri"), source); + TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase); + var unitTestElement = this.converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement; + + Assert.AreEqual(expectedClassName, unitTestElement.TestMethod.ClassName); + Assert.AreEqual(expectedTestName, unitTestElement.TestMethod.Name); + } + + private void ValidateTestMethodProperties(string testName, string fullyQualifiedName, string expectedClassName, string expectedTestName) { TestPlatformObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName); TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase); var unitTestElement = this.converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement; - var expectedTestName = fullyQualifiedName.StartsWith(expectedClassName) ? fullyQualifiedName.Remove(0, $"{expectedClassName}.".Length) : fullyQualifiedName; Assert.AreEqual(expectedClassName, unitTestElement.TestMethod.ClassName); Assert.AreEqual(expectedTestName, unitTestElement.TestMethod.Name);