Search Results for

    Show / Hide Table of Contents

    NUnit1019

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

    Topic Value
    Id NUnit1019
    Severity Error
    Enabled True
    Category Structure
    Code TestCaseSourceUsesStringAnalyzer

    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

    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:

    public class AnalyzeWhenSourceDoesProvideIEnumerable
    {
        private static readonly int[] testCases = new int[] { 1, 2, 42 };
    
        [TestCaseSource(nameof(testCases))]
        public void Test(int input)
        {
        }
    }
    

    Configure severity

    Via ruleset file

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

    Via .editorconfig file

    # 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

    #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.

    #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]

    [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...")]
    
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0