Closed BrightLight closed 2 months ago
Hi @BrightLight!
You are correct that NUnit supports generic test methods in combination with the [TestCaseSource]
attribute.
While the rule description could be enhanced for clarity, it should not trigger for generic test methods with NUnit [TestCaseSource]
.
I tested the snippet you provided and it did not raise any issue on my side.
Could you please confirm if you are indeed seeing S3433 being raised on your snippet? If not, could you provide a snippet that reproduce the false positive?
Cheers
Hello @CristianAmbrosini
apologies. I compared the provided example with the test in our codebase and noticed one crucial difference. In our codebase the test is annotated with both the TestAttribute
and the TestCaseSourceAttribute
, which is valid, but redundant. Like this:
// test method with generic argument TValue
[Test]
[TestCaseSource(nameof(SomeValues))]
public void ValueTypeMatches<TValue>(TValue value, Type typeOfValue)
{
Assert.That(value, Is.Not.Null);
Assert.That(value, Is.InstanceOf<TValue>());
Assert.That(typeof(TValue), Is.EqualTo(typeOfValue));
}
Without the TestAttribute (just the TestCaseSourceAttribute) S3433 does not report. Which in our case means to fix the warning we can simply remove the [Test]
attribute.
I've also now noted that at the very bottom of the rule description (section "Exceptions") it is mentioned that
[...]
[TestCase]
and[TestCaseSource]
test methods in nunit can be generic.
I will add a remark about this special case to the rule description of our local SonarQube server. From my side this ticket can be closed (I'm not closing it myself in case you want to use it to improve the rule description or add handling in case both attributes are used). Thanks.
Description
When using NUnit S3433 reports on generic test methods stating "Make this test method non-generic.". The rule description wrongfully explains (bold by me for emphasis):
This is not true. NUnit does support generic test methods in combination with
[TestCaseSource]
. See also Sources for generic methods using TestCaseData.Repro steps
A simple example that does work with NUnit (tested with NUnit 3.14.0 and 4.2.1):
Expected behavior
No S3433 warning.
Actual behavior
S3433 flags this method stating "Make this test method non-generic."
Known workarounds
Deactivate rule S3433.
Related information