Skip to content

Fix issue #2121 DataTestMethodAttribute is missing a constructor #2125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/Discovery/TypeEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT
testElement.WorkItemIds = workItemAttributes.Select(x => x.Id.ToString(CultureInfo.InvariantCulture)).ToArray();
}

// get DisplayName from TestMethodAttribute
// get DisplayName from TestMethodAttribute (or any inherited attribute)
var testMethodAttribute = _reflectHelper.GetCustomAttribute<TestMethodAttribute>(method);
testElement.DisplayName = testMethodAttribute?.DisplayName ?? method.Name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,21 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DataTestMethodAttribute : TestMethodAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DataTestMethodAttribute"/> class.
/// </summary>
public DataTestMethodAttribute()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DataTestMethodAttribute"/> class.
/// </summary>
/// <param name="displayName">
/// Display name for the test.
/// </param>
public DataTestMethodAttribute(string? displayName)
: base(displayName)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.DataRowAttribute(object? data) -> void
Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute.DataTestMethodAttribute(string? displayName) -> void
Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome.NotFound = 9 -> Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome
virtual Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessageHandler.Invoke(string! message) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public void GetTestFromMethodShouldSetDisplayNameToTestMethodNameIfDisplayNameIs
Verify(testElement.DisplayName == "MethodWithVoidReturnType");
}

public void GetTestFromMethodShouldSetDisplayNameFromAttribute()
public void GetTestFromMethodShouldSetDisplayNameFromTestMethodAttribute()
{
SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true);
TypeEnumerator typeEnumerator = GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName");
Expand All @@ -533,6 +533,22 @@ public void GetTestFromMethodShouldSetDisplayNameFromAttribute()
Verify(testElement.DisplayName == "Test method display name.");
}

public void GetTestFromMethodShouldSetDisplayNameFromDataTestMethodAttribute()
{
SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true);
TypeEnumerator typeEnumerator = GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName");
var methodInfo = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.MethodWithVoidReturnType));

// Setup mocks to behave like we have [DataTestMethod("Test method display name.")] attribute on the method
_mockReflectHelper.Setup(
rh => rh.GetCustomAttribute<TestMethodAttribute>(methodInfo)).Returns(new DataTestMethodAttribute("Test method display name."));

var testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);

Verify(testElement is not null);
Verify(testElement.DisplayName == "Test method display name.");
}

#endregion

#region private methods
Expand Down