docs/articles/nunit-analyzers/NUnit1018.md
| Topic | Value |
|---|---|
| Id | NUnit1018 |
| Severity | Error |
| Enabled | True |
| Category | Structure |
| Code | TestCaseSourceUsesStringAnalyzer |
The number of parameters provided by the TestCaseSource must match the number of parameters in the target method.
To prevent tests that will fail at runtime due to improper construction.
public class MyTestClass
{
[TestCaseSource(nameof(Strings), new object[] { "Testing" })]
public void StringTest(string input)
{
Assert.That(input, Is.Not.Null);
}
static IEnumerable<string> Strings(string first, string second)
{
yield return first;
yield return second;
}
}
In the sample above, the method Strings expects two arguments, but the TestCaseSource only supplies one argument.
Either change Strings to only expect one argument or supply both from the TestCaseSource:
public class MyTestClass
{
[TestCaseSource(nameof(Strings), new object[] { "Testing", "TestCaseSource" })]
public void StringTest(string input)
{
Assert.That(input, Is.Not.Null);
}
static IEnumerable<string> Strings(string first, string second)
{
yield return first;
yield return second;
}
}
Configure the severity per project, for more info see MSDN.
# NUnit1018: The number of parameters provided by the TestCaseSource does not match the number of parameters in the target method
dotnet_diagnostic.NUnit1018.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit1018 // The number of parameters provided by the TestCaseSource does not match the number of parameters in the target method
Code violating the rule here
#pragma warning restore NUnit1018 // The number of parameters provided by the TestCaseSource does not match the number of parameters in the target method
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1018 // The number of parameters provided by the TestCaseSource does not match the number of parameters in the target method
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1018:The number of parameters provided by the TestCaseSource does not match the number of parameters in the target method",
Justification = "Reason...")]