TestCaseSource produces a IDE0052 warning. How to avoid it?

Viewed 109

I want to do several tests with a list of different inputs which are complex objects. Using NUnit, I do the following:

private static readonly IEnumerable<object> InputList = new List<object>
{
    new { aaa = 1, bbb = 2}, // Simplified example
    new { aaa = 1, bbb = 2},
    new { aaa = 1, bbb = 2},
};

[Test, TestCaseSource("InputList")]
public void Test(object testElement)
{
    // Whatever
}

However, that way I get a warning: IDE0052: Private memeber InputList can be removed as the value assigned to it is never read, which is clear because of the fact that TestCaseSource uses InputList as a string, not as an actual reference.

Do I have to suppress the warning or I am doing something wrong?

1 Answers

Write

[Test, TestCaseSource(nameof(InputList))]
public void Test(object testElement)
{
    // Whatever
}

The compiler will replace the nameof expression by the variable name. This has several advantages: 1) The compiler recognizes the the reference to the variable (and the warning will be removed) 2) You will realize when you misspell the variable name during compile time 3) It is easier to rename the variable.

Related