Skip to content
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

Dynamic data display name #373

Merged
merged 8 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added dynamic display name to DynamicDataAttribute
  • Loading branch information
Brad committed Feb 21, 2018
commit 09c30cff0e91e1a48f5e501d43fb2f7b62753754
38 changes: 37 additions & 1 deletion src/TestFramework/MSTest.Core/Attributes/DynamicDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public DynamicDataAttribute(string dynamicDataSourceName, Type dynamicDataDeclar
this.dynamicDataDeclaringType = dynamicDataDeclaringType;
}

/// <summary>
/// Gets or sets the name of method used to customize the display name in test results.
/// </summary>
public string DynamicDisplayName { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming this to "DynamicDataDisplayName"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


/// <summary>
/// Gets or sets the declaring type used to customize the display name in test results.
/// </summary>
public Type DynamicDisplayNameDeclaringType { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. "DynamicDataDisplayNameDeclaringType"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


/// <inheritdoc />
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
{
Expand Down Expand Up @@ -131,7 +141,33 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
/// <inheritdoc />
public string GetDisplayName(MethodInfo methodInfo, object[] data)
{
if (data != null)
if (this.DynamicDisplayName != null)
{
var dynamicDisplayNameDeclaringType = this.DynamicDisplayNameDeclaringType ?? methodInfo.DeclaringType;

var method = dynamicDisplayNameDeclaringType.GetTypeInfo().GetDeclaredMethod(this.DynamicDisplayName);
if (method == null)
{
throw new ArgumentNullException(string.Format("{0} {1}", DynamicDataSourceType.Method, this.DynamicDisplayName));
}

var parameters = method.GetParameters();
if (parameters.Length != 2 ||
parameters[0].ParameterType != typeof(MethodInfo) ||
parameters[1].ParameterType != typeof(object[]) ||
method.ReturnType != typeof(string))
{
throw new ArgumentNullException(
string.Format(
FrameworkMessages.DynamicDataDisplayName,
this.DynamicDisplayName,
typeof(string).Name,
string.Join(", ", typeof(MethodInfo).Name, typeof(object[]).Name)));
}

return method.Invoke(null, new object[] { methodInfo, data }) as string;
}
else if (data != null)
{
return string.Format(CultureInfo.CurrentCulture, FrameworkMessages.DataDrivenResultDisplayName, methodInfo.Name, string.Join(",", data.AsEnumerable()));
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,7 @@ Stack Trace: {4}</value>
<data name="DynamicDataValueNull" xml:space="preserve">
<value>Value returned by property or method {0} shouldn't be null.</value>
</data>
<data name="DynamicDataDisplayName" xml:space="preserve">
Copy link
Member

@jayaranigarg jayaranigarg Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please run "build.cmd -uxlf" so the string addition is localizable. You would have to commit the xlf files changes post running the script.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this and have committed the xlf files. I'm assuming someone or some magic will do the translation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please. We just needed modified xlf files. We will take care of the translation 😄

<value>Method {0} must match the expected signature: {1} {0}({2}).</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,20 @@ public void GetDisplayNameShouldReturnAppropriateName()
displayName = dataRowAttribute.GetDisplayName(this.testMethodInfo, data2);
Assert.AreEqual("DataRowTestMethod (First,,Second)", displayName);
}

[TestMethod]
public void GetDisplayNameShouldReturnAppropriateNameWithDataRowDisplayName()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider changing testmethod name to GetDisplayNameShouldReturnProvidedDisplayName() or GetDisplayNameShouldReturnSpecifiedDisplayName()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
var dataRowAttribute = new DataRowAttribute(null);
dataRowAttribute.DisplayName = "DataRowTestWithDisplayName";

this.dummyTestClass = new DummyTestClass();
this.testMethodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("DataRowTestMethod");

var data = new string[] { "First", "Second", null };

var displayName = dataRowAttribute.GetDisplayName(this.testMethodInfo, data);
Assert.AreEqual("DataRowTestWithDisplayName", displayName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void GetDataShouldReadDataFromProperty()
public void GetDataShouldReadDataFromPropertyInDifferntClass()
{
var methodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1");
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty", typeof(DummyTestClass));
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty2", typeof(DummyTestClass2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers 😄

var data = this.dynamicDataAttribute.GetData(methodInfo);
Assert.IsTrue(data is IEnumerable<object[]>);
Assert.IsTrue(data.ToList().Count == 2);
Expand All @@ -80,7 +80,7 @@ public void GetDataShouldReadDataFromMethod()
public void GetDataShouldReadDataFromMethodInDifferentClass()
{
var methodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod2");
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod", typeof(DummyTestClass), DynamicDataSourceType.Method);
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod2", typeof(DummyTestClass2), DynamicDataSourceType.Method);
var data = this.dynamicDataAttribute.GetData(methodInfo);
Assert.IsTrue(data is IEnumerable<object[]>);
Assert.IsTrue(data.ToList().Count == 2);
Expand Down Expand Up @@ -121,6 +121,41 @@ public void GetDisplayNameShouldReturnDisplayName()
Assert.AreEqual("TestMethod1 (1,2,3)", displayName);
}

[TestFrameworkV1.TestMethod]
public void GetDisplayNameShouldReturnDisplayNameWithDataRowDisplayName()
{
var data = new object[] { 1, 2, 3 };

this.dynamicDataAttribute.DynamicDisplayName = "GetCustomDynamicDataDisplayName";
var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data);
Assert.AreEqual("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters", displayName);
}

[TestFrameworkV1.TestMethod]
public void GetDisplayNameShouldReturnDisplayNameWithDataRowDisplayNameInDifferentClass()
{
var data = new object[] { 1, 2, 3 };

this.dynamicDataAttribute.DynamicDisplayName = "GetCustomDynamicDataDisplayName2";
this.dynamicDataAttribute.DynamicDisplayNameDeclaringType = typeof(DummyTestClass2);
var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data);
Assert.AreEqual("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters", displayName);
}

[TestFrameworkV1.TestMethod]
public void GetDisplayNameShouldThrowExceptionIfMethodDoesNotMatchExpectedSignature()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Tests for cases like :

  1. Method not existing i.e InvalidCustomDynamicDataDisplayName is not present in class
  2. Method has wrong signature i.e. when first parameter doesn't match, second parameter doesn't match, return type doesn't match. Write tests for each of them separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also picked up a bug where I wasn't checking that the method was static.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 👍

{
Action action = () =>
{
var data = new object[] { 1, 2, 3 };

this.dynamicDataAttribute.DynamicDisplayName = "InvalidCustomDynamicDataDisplayName";
var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data);
};

ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException));
}

[TestFrameworkV1.TestMethod]
public void GetDisplayNameShouldReturnEmptyStringIfDataIsNull()
{
Expand Down Expand Up @@ -195,6 +230,30 @@ public static IEnumerable<object[]> ReusableTestDataMethod()
return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } };
}

/// <summary>
/// The custom display name method.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetCustomDynamicDataDisplayName(MethodInfo methodInfo, object[] data)
{
return string.Format("DynamicDataTestWithDisplayName {0} with {1} parameters", methodInfo.Name, data.Length);
}

/// <summary>
/// Invalid custom display name method.
/// </summary>
public static void InvalidCustomDynamicDataDisplayName()
{
}

/// <summary>
/// The test method 1.
/// </summary>
Expand Down Expand Up @@ -242,4 +301,46 @@ public void DataRowTestMethod()
{
}
}

public class DummyTestClass2
{
/// <summary>
/// Gets the reusable test data property.
/// </summary>
public static IEnumerable<object[]> ReusableTestDataProperty2
{
get
{
return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } };
}
}

/// <summary>
/// The reusable test data method.
/// </summary>
/// <returns>
/// The <see cref="IEnumerable"/>.
/// </returns>
public static IEnumerable<object[]> ReusableTestDataMethod2()
{
return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } };
}

/// <summary>
/// The custom display name method.
/// </summary>
/// <param name="methodInfo">
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetCustomDynamicDataDisplayName2(MethodInfo methodInfo, object[] data)
{
return string.Format("DynamicDataTestWithDisplayName {0} with {1} parameters", methodInfo.Name, data.Length);
}
}
}