Skip to content

Custom display name for DynamicDataAttribute #351

Closed

Description

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, "" };
// ....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions