docs/articles/nunit-analyzers/NUnit1022.md
| Topic | Value |
|---|---|
| Id | NUnit1022 |
| Severity | Error |
| Enabled | True |
| Category | Structure |
| Code | ValueSourceUsageAnalyzer |
The specified source must be static.
To prevent tests that will fail at runtime due to improper construction.
public class MyTestClass
{
[Test]
public void DivideTest([ValueSource(nameof(Numbers))] int n)
{
ClassicAssert.AreEqual(n, Is.GreaterThanOrEqualTo(0));
}
object[] Numbers => new int[] { 1, 2, 3 };
}
In the sample above, Numbers is not a static property.
However, sources specified by ValueSource must be static.
Make the source static:
public class MyTestClass
{
[Test]
public void DivideTest([ValueSource(nameof(Numbers))] int n)
{
ClassicAssert.AreEqual(n, Is.GreaterThanOrEqualTo(0));
}
static object[] Numbers => new int[] { 1, 2, 3 };
}
Configure the severity per project, for more info see MSDN.
# NUnit1022: The specified source is not static
dotnet_diagnostic.NUnit1022.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit1022 // The specified source is not static
Code violating the rule here
#pragma warning restore NUnit1022 // The specified source is not static
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1022 // The specified source is not static
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1022:The specified source is not static",
Justification = "Reason...")]