xavier
xavier

Reputation: 2039

TestCaseSource produces a IDE0052 warning. How to avoid it?

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?

Upvotes: 2

Views: 273

Answers (1)

SomeBody
SomeBody

Reputation: 8743

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 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.

Upvotes: 2

Related Questions