From ad822aa3813bcb95af05cb773918a3f57f1530e0 Mon Sep 17 00:00:00 2001 From: jflepp Date: Tue, 8 Dec 2020 20:38:29 +0100 Subject: [PATCH] BugFix: WorkItemAttribute not extracted (#749) * Fixed WorkItem Id not being read bug * Added comment to trigger CI build * Removed comment to trigger CI build * Removed second newline * Removed Array Suffix from Variable Name * Fixed bug and added test Co-authored-by: Flepp Jann --- .../Discovery/TypeEnumerator.cs | 6 +++--- .../Discovery/TypeEnumeratorTests.cs | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs index 3a5e9c61dd..edf5ff1865 100644 --- a/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs +++ b/src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs @@ -192,10 +192,10 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT testElement.Description = descriptionAttribute.Description; } - var workItemAttributeArray = this.reflectHelper.GetCustomAttributes(method, typeof(WorkItemAttribute)) as WorkItemAttribute[]; - if (workItemAttributeArray != null) + var workItemAttributes = this.reflectHelper.GetCustomAttributes(method, typeof(WorkItemAttribute)).Cast().ToArray(); + if (workItemAttributes.Any()) { - testElement.WorkItemIds = workItemAttributeArray.Select(x => x.Id.ToString()).ToArray(); + testElement.WorkItemIds = workItemAttributes.Select(x => x.Id.ToString()).ToArray(); } // Get Deployment items if any. diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs index 78e307c755..fd1f9c10f8 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TypeEnumeratorTests.cs @@ -440,13 +440,26 @@ public void GetTestFromMethodShouldSetWorkItemIds() this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); var methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType"); - this.mockReflectHelper.Setup(rh => rh.GetCustomAttributes(methodInfo, typeof(UTF.WorkItemAttribute))).Returns(new UTF.WorkItemAttribute[] { new UTF.WorkItemAttribute(123), new UTF.WorkItemAttribute(345) }); + this.mockReflectHelper.Setup(rh => rh.GetCustomAttributes(methodInfo, typeof(UTF.WorkItemAttribute))).Returns(new Attribute[] { new UTF.WorkItemAttribute(123), new UTF.WorkItemAttribute(345) }); var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); CollectionAssert.AreEqual(new string[] { "123", "345" }, testElement.WorkItemIds); } + [TestMethod] + public void GetTestFromMethodShouldSetWorkItemIdsToNullIfNotAny() + { + this.SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true); + TypeEnumerator typeEnumerator = this.GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName"); + var methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType"); + this.mockReflectHelper.Setup(rh => rh.GetCustomAttributes(methodInfo, typeof(UTF.WorkItemAttribute))).Returns(new Attribute[0]); + + var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, this.warnings); + + Assert.IsNull(testElement.WorkItemIds); + } + [TestMethod] public void GetTestFromMethodShouldSetCssIteration() {