From c2d3af473fcfc73b48379014e6fe0ba4c3cc257a Mon Sep 17 00:00:00 2001 From: Mike DePouw Date: Sun, 3 Jun 2018 18:46:52 -0400 Subject: [PATCH 1/8] initial attempt to put spaces in Test Explorer --- .../Discovery/TypeEnumerator.cs | 11 +++++++- .../Extensions/TestCaseExtensions.cs | 15 ++++++----- .../ObjectModel/UnitTestElement.cs | 6 +++++ .../Attributes/VSTestAttributes.cs | 25 ++++++++++++++++++- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs index 2e9260ef34..f9208dfbcc 100644 --- a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs +++ b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery @@ -154,6 +154,15 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT this.type, warnings); + // get DisplayName from TestMethodAttribute + var myAttribute = method.GetCustomAttributes(false) + .OfType() + .FirstOrDefault(); + + var testMethodAttribute = myAttribute as TestMethodAttribute; + + testElement.DisplayName = testMethodAttribute.DisplayName ?? method.Name; + return testElement; } } diff --git a/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs b/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs index 45a17a14d7..59a5abe861 100644 --- a/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs +++ b/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs @@ -23,14 +23,17 @@ internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string var isAsync = (testCase.GetPropertyValue(Constants.AsyncTestProperty) as bool?) ?? false; var testClassName = testCase.GetPropertyValue(Constants.TestClassNameProperty) as string; - TestMethod testMethod = new TestMethod(testCase.DisplayName, testClassName, source, isAsync); + // method name from fully qualified name, feels hacky + var parts = testCase.FullyQualifiedName.Split('.'); + var methodName = parts[parts.Length - 1]; + TestMethod testMethod = new TestMethod(methodName, testClassName, source, isAsync); UnitTestElement testElement = new UnitTestElement(testMethod) - { - IsAsync = isAsync, - TestCategory = testCase.GetPropertyValue(Constants.TestCategoryProperty) as string[], - Priority = testCase.GetPropertyValue(Constants.PriorityProperty) as int? - }; + { + IsAsync = isAsync, + TestCategory = testCase.GetPropertyValue(Constants.TestCategoryProperty) as string[], + Priority = testCase.GetPropertyValue(Constants.PriorityProperty) as int? + }; return testElement; } diff --git a/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs b/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs index 945fc8b57d..85d0f6dfd6 100644 --- a/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs +++ b/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs @@ -72,6 +72,11 @@ public UnitTestElement(TestMethod testMethod) /// public KeyValuePair[] DeploymentItems { get; set; } + /// + /// Gets or sets the DisplayName + /// + public string DisplayName { get; set; } + /// /// Gets or sets the compiler generated type name for async test method. /// @@ -91,6 +96,7 @@ internal TestCase ToTestCase() TestCase testCase = new TestCase(fullName, TestAdapter.Constants.ExecutorUri, this.TestMethod.AssemblyName); testCase.DisplayName = this.TestMethod.Name; + testCase.DisplayName = this.DisplayName; testCase.SetPropertyValue(TestAdapter.Constants.TestClassNameProperty, this.TestMethod.FullClassName); diff --git a/src/TestFramework/MSTest.Core/Attributes/VSTestAttributes.cs b/src/TestFramework/MSTest.Core/Attributes/VSTestAttributes.cs index 78977e1d80..232b24e264 100644 --- a/src/TestFramework/MSTest.Core/Attributes/VSTestAttributes.cs +++ b/src/TestFramework/MSTest.Core/Attributes/VSTestAttributes.cs @@ -7,7 +7,6 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; - using System.Linq; #pragma warning disable SA1402 // FileMayOnlyContainASingleType #pragma warning disable SA1649 // SA1649FileNameMustMatchTypeName @@ -50,6 +49,30 @@ public virtual TestMethodAttribute GetTestMethodAttribute(TestMethodAttribute te [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class TestMethodAttribute : Attribute { + /// + /// Initializes a new instance of the class. + /// + public TestMethodAttribute() + : this(null) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// Message specifies reason for ignoring. + /// + public TestMethodAttribute(string displayName) + { + this.DisplayName = displayName; + } + + /// + /// Gets display Name for the Test Window + /// + public string DisplayName { get; private set; } + /// /// Executes a test method. /// From bb564f8ccf24a3f2e3291438058d47ffc0bb38af Mon Sep 17 00:00:00 2001 From: Mike DePouw Date: Wed, 6 Jun 2018 08:07:49 -0400 Subject: [PATCH 2/8] make code more null ref safe --- src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs index f9208dfbcc..8d91908d63 100644 --- a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs +++ b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs @@ -161,7 +161,7 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT var testMethodAttribute = myAttribute as TestMethodAttribute; - testElement.DisplayName = testMethodAttribute.DisplayName ?? method.Name; + testElement.DisplayName = testMethodAttribute?.DisplayName ?? method.Name; return testElement; } From 6f70f22d61d72248798ab1c712ab615ddda4156e Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Sat, 24 Aug 2019 18:31:05 -0400 Subject: [PATCH 3/8] Update DisplayName assignment inside ToTestCase methd --- src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs b/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs index 7277d227e4..b39abe7763 100644 --- a/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs +++ b/src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs @@ -115,8 +115,7 @@ internal TestCase ToTestCase() this.TestMethod.Name); TestCase testCase = new TestCase(fullName, TestAdapter.Constants.ExecutorUri, this.TestMethod.AssemblyName); - testCase.DisplayName = this.TestMethod.Name; - testCase.DisplayName = this.DisplayName; + testCase.DisplayName = string.IsNullOrEmpty(this.DisplayName) ? this.TestMethod.Name : this.DisplayName; testCase.SetPropertyValue(TestAdapter.Constants.TestClassNameProperty, this.TestMethod.FullClassName); From af87e68bc928a997401d2d88c2ac5a7ac3e9a91d Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Sun, 25 Aug 2019 00:10:38 -0400 Subject: [PATCH 4/8] Added unit tests for TypeEnumerator and UnitTestElement classes --- .../Discovery/TypeEnumeratorTests.cs | 29 +++++++++++++++++++ .../ObjectModel/UnitTestElementTests.cs | 9 ++++++ 2 files changed, 38 insertions(+) diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs index d68e2655dc..70895caa5a 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs @@ -529,6 +529,35 @@ public void GetTestFromMethodShouldSetDeclaringAssemblyName() Assert.AreEqual(otherAssemblyName, testElement.TestMethod.DeclaringAssemblyName); } + [TestMethod] + public void GetTestFromMethodShouldSetDisplayNameToTestMethodNameIfNotPresent() + { + this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); + TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); + var methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType"); + + var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); + + Assert.IsNotNull(testElement); + Assert.IsNotNull(testElement.DisplayName); + Assert.AreEqual("MethodWithVoidReturnType", testElement.DisplayName); + } + + [TestMethod] + public void GetTestFromMethodShouldSetDisplayName() + { + this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); + TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); + var methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType"); + + var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); + testElement.DisplayName = "New Display Name"; + + Assert.IsNotNull(testElement); + Assert.IsNotNull(testElement.DisplayName); + Assert.AreEqual("New Display Name", testElement.DisplayName); + } + #endregion #region private methods diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/ObjectModel/UnitTestElementTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/ObjectModel/UnitTestElementTests.cs index 1143ae8967..4415f56d04 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/ObjectModel/UnitTestElementTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/ObjectModel/UnitTestElementTests.cs @@ -76,6 +76,15 @@ public void ToTestCaseShouldSetDisplayName() Assert.AreEqual("M", testCase.DisplayName); } + [TestMethodV1] + public void ToTestCaseShouldSetDisplayNameIfPresent() + { + this.unitTestElement.DisplayName = "Display Name"; + var testCase = this.unitTestElement.ToTestCase(); + + Assert.AreEqual("Display Name", testCase.DisplayName); + } + [TestMethodV1] public void ToTestCaseShouldSetTestClassNameProperty() { From 74fa86ca35c5e20d5d69ee53f76002d6f33e7707 Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Sun, 25 Aug 2019 00:41:14 -0400 Subject: [PATCH 5/8] Reverted changes on ToUnitTestElement method --- .../MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs b/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs index 996d9448ab..6edd1ce53b 100644 --- a/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs +++ b/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs @@ -24,10 +24,7 @@ internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string var testClassName = testCase.GetPropertyValue(Constants.TestClassNameProperty) as string; var declaringClassName = testCase.GetPropertyValue(Constants.DeclaringClassNameProperty) as string; - // method name from fully qualified name, feels hacky - var parts = testCase.FullyQualifiedName.Split('.'); - var methodName = parts[parts.Length - 1]; - TestMethod testMethod = new TestMethod(methodName, testClassName, source, isAsync); + TestMethod testMethod = new TestMethod(testCase.DisplayName, testClassName, source, isAsync); if (declaringClassName != null && declaringClassName != testClassName) { From adf8a20e0b4dc889e81c41a0a8cead7d107e97df Mon Sep 17 00:00:00 2001 From: nohwnd Date: Tue, 17 Dec 2019 16:02:02 +0100 Subject: [PATCH 6/8] Fix test method name Fixed tests to prove that UnitTestElement is setting the DisplayName and that it does not use the Display name as the method name. Using Display Name as method name leads to searching for methods like "Class1.Display Name" which won't be found and the test runner will fail. --- .../Extensions/TestCaseExtensions.cs | 7 ++-- .../Discovery/TypeEnumeratorTests.cs | 33 ++++++++++++------- .../Extensions/TestCaseExtensionsTests.cs | 3 +- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs b/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs index 6edd1ce53b..4f9099c265 100644 --- a/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs +++ b/src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs @@ -24,7 +24,9 @@ internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string var testClassName = testCase.GetPropertyValue(Constants.TestClassNameProperty) as string; var declaringClassName = testCase.GetPropertyValue(Constants.DeclaringClassNameProperty) as string; - TestMethod testMethod = new TestMethod(testCase.DisplayName, testClassName, source, isAsync); + var parts = testCase.FullyQualifiedName.Split('.'); + var name = parts[parts.Length - 1]; + TestMethod testMethod = new TestMethod(name, testClassName, source, isAsync); if (declaringClassName != null && declaringClassName != testClassName) { @@ -35,7 +37,8 @@ internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string { IsAsync = isAsync, TestCategory = testCase.GetPropertyValue(Constants.TestCategoryProperty) as string[], - Priority = testCase.GetPropertyValue(Constants.PriorityProperty) as int? + Priority = testCase.GetPropertyValue(Constants.PriorityProperty) as int?, + DisplayName = testCase.DisplayName }; return testElement; diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs index 70895caa5a..03ca6fa3c1 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs @@ -25,6 +25,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Discovery using TestCleanup = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute; using TestInitialize = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; using TestMethod = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; + using TestMethodV2 = FrameworkV2::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; using UTF = FrameworkV2::Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -530,32 +531,29 @@ public void GetTestFromMethodShouldSetDeclaringAssemblyName() } [TestMethod] - public void GetTestFromMethodShouldSetDisplayNameToTestMethodNameIfNotPresent() + public void GetTestFromMethodShouldSetDisplayNameToTestMethodNameIfDisplayNameIsNotPresent() { this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); - TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); - var methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType"); + TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DisplayNameTestClass), "DummyAssemblyName"); + var methodInfo = typeof(DisplayNameTestClass).GetMethod(nameof(DisplayNameTestClass.TestMethodWithoutDisplayName)); var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); Assert.IsNotNull(testElement); - Assert.IsNotNull(testElement.DisplayName); - Assert.AreEqual("MethodWithVoidReturnType", testElement.DisplayName); + Assert.AreEqual("TestMethodWithoutDisplayName", testElement.DisplayName); } [TestMethod] - public void GetTestFromMethodShouldSetDisplayName() + public void GetTestFromMethodShouldSetDisplayNameFromAttribute() { this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); - TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); - var methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType"); + TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DisplayNameTestClass), "DummyAssemblyName"); + var methodInfo = typeof(DisplayNameTestClass).GetMethod(nameof(DisplayNameTestClass.TestMethodWithDisplayName)); var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); - testElement.DisplayName = "New Display Name"; Assert.IsNotNull(testElement); - Assert.IsNotNull(testElement.DisplayName); - Assert.AreEqual("New Display Name", testElement.DisplayName); + Assert.AreEqual("Test method display name.", testElement.DisplayName); } #endregion @@ -630,5 +628,18 @@ public class DummySecondHidingTestClass : DummyOverridingTestClass } } + internal abstract class DisplayNameTestClass + { + [TestMethodV2] + public void TestMethodWithoutDisplayName() + { + } + + [TestMethodV2("Test method display name.")] + public void TestMethodWithDisplayName() + { + } + } + #endregion } diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/TestCaseExtensionsTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/TestCaseExtensionsTests.cs index 8c6ef2c73f..0b900474be 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/TestCaseExtensionsTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/TestCaseExtensionsTests.cs @@ -34,7 +34,8 @@ public void ToUnitTestElementShouldReturnUnitTestElementWithFieldsSet() Assert.AreEqual(true, resultUnitTestElement.IsAsync); Assert.AreEqual(2, resultUnitTestElement.Priority); Assert.AreEqual(testCategories, resultUnitTestElement.TestCategory); - Assert.AreEqual("DummyDisplayName", resultUnitTestElement.TestMethod.Name); + Assert.AreEqual("DummyDisplayName", resultUnitTestElement.DisplayName); + Assert.AreEqual("DummyMethod", resultUnitTestElement.TestMethod.Name); Assert.AreEqual("DummyClassName", resultUnitTestElement.TestMethod.FullClassName); Assert.AreEqual(true, resultUnitTestElement.TestMethod.IsAsync); Assert.IsNull(resultUnitTestElement.TestMethod.DeclaringClassFullName); From 025ae85e0b8bf709986d773b1cb6e40b211fe256 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Tue, 17 Dec 2019 16:16:50 +0100 Subject: [PATCH 7/8] Use ReflectHelper instead of Reflection --- src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs index 236d159e90..e74467a993 100644 --- a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs +++ b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs @@ -205,12 +205,7 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT warnings); // get DisplayName from TestMethodAttribute - var myAttribute = method.GetCustomAttributes(false) - .OfType() - .FirstOrDefault(); - - var testMethodAttribute = myAttribute as TestMethodAttribute; - + var testMethodAttribute = this.reflectHelper.GetAttribute(method); testElement.DisplayName = testMethodAttribute?.DisplayName ?? method.Name; return testElement; From 21a095a2aad5cb7a6ec0e735c6d9dde5634efaf5 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Tue, 17 Dec 2019 17:34:40 +0100 Subject: [PATCH 8/8] Use different way to get the attribute and mocks in tests The usage of ReflectHelper is a bit nuanced, the generic method I used originally uses a static method interallly which prevents it from being mocked. So in order to write the tests in the same way as the other tests are written I use the mock to act like the method is decorated with the correct attribute, and need to use a different method internally but at least the generic method is not the sigle outlier in that file. --- .../Discovery/TypeEnumerator.cs | 2 +- .../Discovery/TypeEnumeratorTests.cs | 31 ++++++++----------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs index e74467a993..11e6e30f83 100644 --- a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs +++ b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs @@ -205,7 +205,7 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT warnings); // get DisplayName from TestMethodAttribute - var testMethodAttribute = this.reflectHelper.GetAttribute(method); + var testMethodAttribute = this.reflectHelper.GetCustomAttribute(method, typeof(TestMethodAttribute)) as TestMethodAttribute; testElement.DisplayName = testMethodAttribute?.DisplayName ?? method.Name; return testElement; diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs index 03ca6fa3c1..1ff92165bc 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs @@ -534,21 +534,29 @@ public void GetTestFromMethodShouldSetDeclaringAssemblyName() public void GetTestFromMethodShouldSetDisplayNameToTestMethodNameIfDisplayNameIsNotPresent() { this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); - TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DisplayNameTestClass), "DummyAssemblyName"); - var methodInfo = typeof(DisplayNameTestClass).GetMethod(nameof(DisplayNameTestClass.TestMethodWithoutDisplayName)); + TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); + var methodInfo = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.MethodWithVoidReturnType)); + + // Setup mocks to behave like we have [TestMethod] attribute on the method + this.mockReflectHelper.Setup( + rh => rh.GetCustomAttribute(It.IsAny(), It.IsAny())).Returns(new TestMethodV2()); var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); Assert.IsNotNull(testElement); - Assert.AreEqual("TestMethodWithoutDisplayName", testElement.DisplayName); + Assert.AreEqual("MethodWithVoidReturnType", testElement.DisplayName); } [TestMethod] public void GetTestFromMethodShouldSetDisplayNameFromAttribute() { this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); - TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DisplayNameTestClass), "DummyAssemblyName"); - var methodInfo = typeof(DisplayNameTestClass).GetMethod(nameof(DisplayNameTestClass.TestMethodWithDisplayName)); + TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); + var methodInfo = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.MethodWithVoidReturnType)); + + // Setup mocks to behave like we have [TestMethod("Test method display name.")] attribute on the method + this.mockReflectHelper.Setup( + rh => rh.GetCustomAttribute(methodInfo, typeof(TestMethodV2))).Returns(new TestMethodV2("Test method display name.")); var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); @@ -628,18 +636,5 @@ public class DummySecondHidingTestClass : DummyOverridingTestClass } } - internal abstract class DisplayNameTestClass - { - [TestMethodV2] - public void TestMethodWithoutDisplayName() - { - } - - [TestMethodV2("Test method display name.")] - public void TestMethodWithDisplayName() - { - } - } - #endregion }