nunit / nunit.analyzers

Roslyn analyzers for writing unit tests with NUnit
MIT License
89 stars 32 forks source link

New diagnostic: Remove a common parameter from TestCases #763

Open Bartleby2718 opened 4 months ago

Bartleby2718 commented 4 months ago

Another pet peeve of mine.

[TestCase(1, 42)]
[TestCase(2, 42)]
public void Example(int value1, int value2)
{
    Assert.That(value1, Is.LessThan(value2));
}

can be simplified to

[TestCase(1)]
[TestCase(2)]
public void Example(int value1)
{
    Assert.That(value1, Is.LessThan(42));
}

This reduces an unnecessary variable, helping you focus on what's important. (If you don't want a literal, you can use a const as opposed to a variable.)

manfred-brands commented 4 months ago

Not sure I like this one. The first allows adding a TestCase(3,4). Once refactored with this CodeFix that is no longer possible.

Bartleby2718 commented 4 months ago

@manfred-brands Sure, but once you've added all test cases, would you still want a common parameter?

The idea of the proposed diagnostic is basically the same as Remove unused parameter (IDE0060). This may be annoying during development (as are many other style rules that are enabled in this repository), but once you're done writing tests, this diagnostic should help improve the readability of the code.

Let me know what you think!