Description
openedon Jan 18, 2018
Description
Is it possible to add an option to the DynamicDataAttribute
allowing similar functionality to the DataRowAttribute.DisplayName
. This would allow tests with similar parameters to be given more meaningful names and therefore make it easier to determine which set of parameter is producing a particular result.
A possible solution would be to set the a DisplayName
property to the name of a method, just as dynamicDataSourceName
is currently, and match the method signature of the default GetDisplayName
method. This will allow a custom display name to be generated depending on the parameter set.
Current Behaviour
With the test class:
[TestClass]
public class Tests
{
[TestMethod]
[DynamicData(nameof(Tests.TestMethodTests))]
public void TestMethod(string param1, string param2)
{
Assert.IsNotNull(param1);
}
private static IEnumerable<object[]> TestMethodTests
{
get
{
yield return new object[] { "", null };
yield return new object[] { null, "" };
}
}
}
Displays output where it is not clear which set of parameters caused the failure
Failed TestMethod (,)
Error Message:
Assert.IsNotNull failed.
Stack Trace:
...
Possible Solution
// ....
[DynamicData(nameof(Tests.TestMethodTests), DisplayName = nameof(Tests.GetTestMethodDisplayName))]
// ...
public static string GetTestMethodDisplayName(MethodInfo methodInfo, object[] data)
{
// ....
}
// ....
Current Workaround
As a work around I have been adding an unused sting parameter to identify the test e.g:
// ....
public void TestMethod(string testName, string param1, string param2)
// ....
yield return new object[] { "Param2 null", "", null };
yield return new object[] { "Param1 null", null, "" };
// ....