NUnit1024
The source specified by the ValueSource does not return an IEnumerable or a type that implements IEnumerable
Topic | Value |
---|---|
Id | NUnit1024 |
Severity | Error |
Enabled | True |
Category | Structure |
Code | ValueSourceUsageAnalyzer |
Description
The source specified by the ValueSource must return an IEnumerable or a type that implements IEnumerable.
Motivation
To prevent tests that will fail at runtime due to improper construction.
How to fix violations
Example Violation
public class AnalyzeWhenSourceDoesProvideIEnumerable
{
private static readonly int testCases = 42;
[Test]
public void Test([ValueSource(nameof(testCases))] int input)
{
}
}
Explanation
In the sample above, the source specified by ValueSource
- the field testCases
- does not return an IEnumerable
or a type that implements IEnumerable
,
instead it returns an int
.
However, sources specified by ValueSource
must return an IEnumerable
or a type that implements IEnumerable
..
Fix
Change testCases
to return an IEnumerable
or a type that implements IEnumerable
:
public class AnalyzeWhenSourceDoesProvideIEnumerable
{
private static readonly int[] testCases = new int[] { 1, 2, 42 };
[Test]
public void Test([ValueSource(nameof(testCases))] int input)
{
}
}
Configure severity
Via ruleset file
Configure the severity per project, for more info see MSDN.
Via .editorconfig file
# NUnit1024: The source specified by the ValueSource does not return an IEnumerable or a type that implements IEnumerable
dotnet_diagnostic.NUnit1024.severity = chosenSeverity
where chosenSeverity
can be one of none
, silent
, suggestion
, warning
, or error
.
Via #pragma directive
#pragma warning disable NUnit1024 // The source specified by the ValueSource does not return an IEnumerable or a type that implements IEnumerable
Code violating the rule here
#pragma warning restore NUnit1024 // The source specified by the ValueSource does not return an IEnumerable or a type that implements IEnumerable
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1024 // The source specified by the ValueSource does not return an IEnumerable or a type that implements IEnumerable
Via attribute [SuppressMessage]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1024:The source specified by the ValueSource does not return an IEnumerable or a type that implements IEnumerable",
Justification = "Reason...")]