Back to Nunit

NUnit1019

docs/articles/nunit-analyzers/NUnit1019.md

latest3.1 KB
Original Source

NUnit1019

The source specified by the TestCaseSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable

TopicValue
IdNUnit1019
SeverityError
EnabledTrue
CategoryStructure
CodeTestCaseSourceUsesStringAnalyzer

Description

The source specified by the TestCaseSource must return an I(Async)Enumerable or a type that implements I(Async)Enumerable.

Motivation

To prevent tests that will fail at runtime due to improper construction.

How to fix violations

Example Violation

csharp
public class AnalyzeWhenSourceDoesProvideIEnumerable
{
    private static readonly int testCases = 42;

    [TestCaseSource(nameof(testCases))]
    public void Test(int input)
    {
    }
}

Explanation

In the sample above, the source specified by TestCaseSource - the field testCases - does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable, instead it returns an int.

However, sources specified by TestCaseSource must return an I(Async)Enumerable or a type that implements I(Async)Enumerable..

Fix

Change testCases to return an I(Async)Enumerable or a type that implements I(Async)Enumerable:

csharp
public class AnalyzeWhenSourceDoesProvideIEnumerable
{
    private static readonly int[] testCases = new int[] { 1, 2, 42 };

    [TestCaseSource(nameof(testCases))]
    public void Test(int input)
    {
    }
}
<!-- start generated config severity -->

Configure severity

Via ruleset file

Configure the severity per project, for more info see MSDN.

Via .editorconfig file

ini
# NUnit1019: The source specified by the TestCaseSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable
dotnet_diagnostic.NUnit1019.severity = chosenSeverity

where chosenSeverity can be one of none, silent, suggestion, warning, or error.

Via #pragma directive

csharp
#pragma warning disable NUnit1019 // The source specified by the TestCaseSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable
Code violating the rule here
#pragma warning restore NUnit1019 // The source specified by the TestCaseSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable

Or put this at the top of the file to disable all instances.

csharp
#pragma warning disable NUnit1019 // The source specified by the TestCaseSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
    "NUnit1019:The source specified by the TestCaseSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable",
    Justification = "Reason...")]
<!-- end generated config severity -->